简体   繁体   English

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

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

R : how can i insert ( id ) url to database Laravel R:如何将(id)url插入数据库Laravel

I have this url : 我有这个网址

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

I need to save the id to database 我需要将ID保存到数据库

Route 路线

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

Controller 控制者

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

     }

any help pls ? 有什么帮助吗?

Your question is not very clear. 您的问题不是很清楚。 I understood the function of @storeData , it will create a new question based on the request it receives. 我了解@storeData的功能,它将根据收到的请求创建一个新问题。 As far as I understand, your /question/{id} route serves to "pick" the type of question before posting the question itself. 据我了解,您的/question/{id}路线用于在发布问题本身之前“挑选”问题类型。 The correct thing is you submit the question type along with the new question POST. 正确的做法是您提交问题类型以及新的问题POST。 In your Front, you must make a GET request, receiving all the available question types in your DB, and along with the requisition to create a new question, you send the type of question, previously chosen from the ones available in your DB. 在前台,您必须发出GET请求,接收数据库中所有可用的问题类型,并连同创建新问题的要求一起,发送先前从数据库中可用的问题类型中选择的问题类型。

@Edit based in Question's Owner comment: @Edit基于Question的所有者评论:

If you edit your Route to this: 如果您编辑此路线:

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

In your controller you can do this: 在您的控制器中,您可以执行以下操作:

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

}

Anyway, your route /question/{id} are isolated from the storeData function. 无论如何,您的路线/question/{id}storeData函数是隔离的。

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

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