繁体   English   中英

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

[英]how can i insert ( id ) url to database Laravel

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();

     }

有什么帮助吗?

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

@Edit基于Question的所有者评论:

如果您编辑此路线:

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函数是隔离的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM