簡體   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