如何在创建后使用 Laravel 显示数据库中最后插入的 id

患者控制器:


 public function store(Request $request)

    {

    

        $input = request()->all();

        Patient::create($input);

        dd($input->id);

        // return redirect()->route('medical.create',compact('input'));

       

    }

这是我的medical.create视图


{!! Form::model($input, [

    'method' => 'POST',

    'action' => ['MedicalController@store', $input->id]

]) !!}

        <div class="row">

                <div class="col-md-4">

                    

                        <div class="form-group">

                        {{Form::label('patient_id','Patient no:')}}

                        {{Form::text('patient_id', null, array('class' => 'form-control') )}}

                        </div>


                </div>

                

        </div>

      


             {!! Form::close() !!}

我想在存储后获取最后插入的 id,并在下一个表单中显示最后一个 id,但这是我的屏幕上出现的错误:


Trying to get property 'id' of non-object

这是我的病人表:

https://img2.mukewang.com/65193b5f00012a5706540122.jpg

慕妹3242003
浏览 132回答 4
4回答

凤凰求蛊

您可以通过Patient在创建对象时将对象保存在变量中来做到这一点:public function store(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $input = request()->all();&nbsp; &nbsp; &nbsp; &nbsp; $patient = Patient::create($input); // Save it in variable&nbsp; &nbsp; &nbsp; &nbsp; dd($patient->id); //Now you can access patient id here&nbsp; &nbsp; &nbsp; &nbsp; // return redirect()->route('medical.create',compact('patient')); //Also you can pass it to your view&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }

慕田峪9158850

您可以像下面的代码一样使用 id,并且代码中的更改最少&nbsp; &nbsp; $input = request()->all();&nbsp; &nbsp; $input = Patient::create($input);&nbsp; &nbsp; dd($input->id);&nbsp; &nbsp; // return redirect()->route('medical.create',compact('input'));

跃然一笑

重定向时不能使用紧凑。尝试这个:患者控制器:&nbsp;public function store(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $input = request()->all();&nbsp; &nbsp; &nbsp; &nbsp; $patient = Patient::create($input);&nbsp; &nbsp; &nbsp; &nbsp; $patient = DB::table('patients')->get()->last();&nbsp; &nbsp; &nbsp; &nbsp; return redirect()->route('medical.create')->with('patient_id', $patient->id);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }这是我的medical.create视图{!! Form::model($input, [&nbsp; &nbsp; 'method' => 'POST',&nbsp; &nbsp; 'action' => ['MedicalController@store', session('patient_id')]]) !!}&nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-4">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{Form::label('patient_id','Patient no:')}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{Form::text('patient_id', null, array('class' => 'form-control') )}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{!! Form::close() !!}

白猪掌柜的

您可以通过批量分配保存数据后检索 id:&nbsp;public function store(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $input = request()->all();&nbsp; &nbsp; &nbsp; &nbsp; $patient = new Patient($input); // fill model with mass assignments&nbsp; &nbsp; &nbsp; &nbsp; $patient->save() // save instant&nbsp; &nbsp; &nbsp; &nbsp; $id = $patient->id; //retrive id&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP