【问题标题】:How to display the last inserted id in db using Laravel after creating创建后如何使用 Laravel 在 db 中显示最后插入的 id
【发布时间】:2020-08-13 11:55:47
【问题描述】:

病人控制者:

 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

这是我的患者表:

【问题讨论】:

  • $患者 = Patient::create($input); dd($患者->id);
  • 但这是创建 Trying to get property 'id' of non-object 后的错误
  • 首先只打印 $patient 并查看包含的 id 吗?
  • 是的,没有 id 我将代码更改为 dd($input)
  • array:12 [▼ "_token" => "BVFdC1JCfL55mYfRSIXzd3nKXF0clzknZrBoXWRC" "firstname" => "SAD" "middlename" => "SADAAA" "lastname" => "SADSA" "email" => "SA@GMAIL.COM" "联系人" => "asdsa" ]

标签: php laravel laravel-7


【解决方案1】:

您可以在创建 Patient 对象时将其保存在变量中:

public function store(Request $request)
    {
        $input = request()->all();
        $patient = Patient::create($input); // Save it in variable
        dd($patient->id); //Now you can access patient id here
        // return redirect()->route('medical.create',compact('patient')); //Also you can pass it to your view
       
    }

【讨论】:

  • 但这是创建后的错误尝试获取非对象的属性'id'但数据保存在数据库中
  • 您可能没有更改刀片中的变量。现在使用$patient,而不是$input'action' =&gt; ['MedicalController@store', $patient-&gt;id]
  • {!! Form::model($patient, [ 'method' => 'PATCH', 'action' => ['MedicalController@store', $patient->id] ]) !!}
  • 这样吗? @zlatan 先生
  • 是的,就这样。
【解决方案2】:

您可以像下面的代码一样使用 id,代码中的更改最少

    $input = request()->all();
    $input = Patient::create($input);
    dd($input->id);
    // return redirect()->route('medical.create',compact('input'));

【讨论】:

  • 我认为这会出错,因为您正在覆盖 $input 变量。
  • 我的想法不像你。 $input 只是一个对象。这样您就可以尽可能轻松地覆盖它
【解决方案3】:

您可以通过批量分配保存数据后检索id:

 public function store(Request $request)
    {
        $input = request()->all();
        $patient = new Patient($input); // fill model with mass assignments
        $patient->save() // save instant
        $id = $patient->id; //retrive id
       
    }

【讨论】:

    【解决方案4】:

    重定向时不能使用紧凑。试试这个:

    病人控制者:

     public function store(Request $request)
        {
        
            $input = request()->all();
            $patient = Patient::create($input);
            $patient = DB::table('patients')->get()->last();
            return redirect()->route('medical.create')->with('patient_id', $patient->id);
           
        }
    

    这是我的 medical.create 视图

    {!! Form::model($input, [
        'method' => 'POST',
        'action' => ['MedicalController@store', session('patient_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() !!}
    

    【讨论】:

    • 您好 Nurbek 先生,感谢您的回答,但错误是 $patient 未定义
    • 它是否在数据库中创建了一些东西
    • 是的,先生,我唯一的问题是在我的表单中显示最后一个 id
    • 嗨,先生,如果我 dd($patient->id) id,例如,结果是 33,所以这是正确的。问题是我想将这个 33 id 传递给我的表单中的 medical.create 视图是否可能?
    • 你可以通过 return redirect('/something')->with('patient_id', 33);在您的创建中,您可以像 {{ session('patient_id') }} 那样使用它
    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2016-06-01
    • 2016-08-25
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多