【问题标题】:how can i insert ( id ) url to database Laravel我如何将( id ) url 插入数据库 Laravel
【发布时间】:2019-09-04 00:23:01
【问题描述】:

R : 我如何将 (id) url 插入数据库 Laravel

我有这个网址

http://127.0.0.1:8000/admin/question/1

我需要将 id 保存到数据库中

路线

Route::get('/question/{id}', 'questionController@choseType');
Route::post('/question/createone', 'questionController@storeData');

控制器

    public function storeData(Request $request)
    {
        $this->validate($request,[
            'name'=>'required',
            'department_id'=>'required',
            'nameChoose.*'=>'required',
        ],[],[
            'name'=>'question',
            'department_id'=>'department name',
            'nameChoose'=>'nameChoose',
        ]);

        //here i want to save the id of this table to the question table 
        // which exist in URL
        $question_type = question_types::pluck('id')->first();

        $question = new questions();
        $question->name = $request->input("name");
        $question->department_id = $request->input("department_id");
        $question->question_type_id = $question_type;
        $question->save();

     }

有什么帮助吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你的问题不是很清楚。 我了解@storeData 的功能,它会根据收到的请求创建一个新问题。 据我了解,您的 /question/{id} 路线用于在发布问题本身之前“选择”问题类型。正确的做法是您将问题类型与新问题 POST 一起提交。在您的前面,您必须发出 GET 请求,接收数据库中所有可用的问题类型,并连同创建新问题的申请一起发送问题类型,之前从您的数据库中可用的问题类型中选择。

    @Edit 基于问题的所有者评论:

    如果您将路线编辑到此:

    Route::post('/question/createone/{id}', 'questionController@storeData');
    

    在您的控制器中,您可以这样做:

    public function storeData(Request $request, $id) //the Id from the route
    {
        $this->validate($request,[
            'name'=>'required',
            'department_id'=>'required',
            'nameChoose.*'=>'required',
        ],[],[
            'name'=>'question',
            'department_id'=>'department name',
            'nameChoose'=>'nameChoose',
        ]);
    
        $question = new questions();
        $question->name = $request->input("name");
        $question->department_id = $request->input("department_id");
        $question->question_type_id = $id; //The id from the route
        $question->save();
    
    }
    

    无论如何,您的路由/question/{id}storeData 函数是隔离的。

    【讨论】:

    • 问题是:我有两个表,其中一个命名为(问题类型),第二个命名为(问题),并且表(问题)中有一个名为 question_type_id 的列,我该如何保存id(问题类型表的id)在问题表的url中:::希望现在清楚
    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 2017-12-20
    • 2020-07-05
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    相关资源
    最近更新 更多