【问题标题】:Laravel How to get AI id in controller before savingLaravel如何在保存之前在控制器中获取AI id
【发布时间】:2021-08-09 20:27:53
【问题描述】:

在保存新数据之前,我曾尝试从booking 表中获取auto increment ID,但我一直有错误。

这是控制器中的代码:

public function addAppointment(Request $request) {
    $user = auth()->user();

    $booking = new Booking();
    $booking->vac_center_id = $request->get('vaccination_center');
    $booking->vac_id = $request->get('vaccination_id');
    $booking->date_of_shot = $request->get('date_of_shot');
    $booking->time = $request->get('time');
    $booking->shot_number = $request->get('shot_number');
    $booking->isDone = 0;
    $booking->isCancelled = 0;
    $booking->user_id = $user->id;

    $booking->save();

    $booking = new BookingHasVaccinationCenters();
    //here below I want to get the auto increment id
    $booking->booking_id->id;
    $booking->vac_center_id = $request->get('vaccination_center');
    $booking->save();

    return redirect('/home');
}

这是我上次尝试这样做时遇到的错误:

尝试在 null 上读取属性“id”

【问题讨论】:

    标签: php laravel eloquent laravel-8


    【解决方案1】:

    而不是这个

        $booking = new BookingHasVaccinationCenters();
        //here below I want to get the auto increment id
        $booking->booking_id->id;
        $booking->vac_center_id = $request->get('vaccination_center');
        $booking->save();
    

    使用下面的代码

       $bookingHasVaccination = new BookingHasVaccinationCenters();
        //change here 
        $bookingHasVaccination->booking_id = $booking->id;
        $bookingHasVaccination->vac_center_id = $request->get('vaccination_center');
        $bookingHasVaccination->save();
    

    注意:在进行 crud 操作时,请始终尝试定义与模型类同名的变量

    【讨论】:

    • 谢谢它的工作!也感谢您的来信!
    【解决方案2】:

    您的错误是您使用相同的名称声明变量 $booking ,当您 保存 $booking 你应该声明一个具有其他名称的对象实例,例如

    $bookingvaccine->booking_id = $booking->id;
    

    【讨论】:

      【解决方案3】:

      我知道您的问题已经得到解答,但让我分享您正在做的另一种方式,这样您就可以防止这种错误并且您的代码会更好。

      如果您在此表/模型之间有关系(已创建关系函数),那么您可以使用该关系在它们之间创建新模型,而无需共享或传递父模型的 ID。

      假设您的用户与Booking 的关系名称是bookings,而Booking -> BookingHasVaccinationCenters 的关系(奇怪的名称)是bookingHasVaccinationCenters,您应该可以这样做:

      public function addAppointment(Request $request)
      {
          $booking = $request->user()
              ->booking()
              ->create([
                  'vac_center_id' => $request->input('vaccination_center'),
                  'vac_id' => $request->input('vaccination_id'),
                  'date_of_shot' => $request->input('date_of_shot'),
                  'time' => $request->input('time'),
                  'shot_number' => $request->input('shot_number'),
                  'isDone' => false,
                  'isCancelled' => false,
              ]);
      
          $booking->bookingHasVaccinationCenters()->create([
              'vac_center_id' => $request->input('vaccination_center'),
          ]);
          
          return redirect('/home');
      }
      

      另一个超级小技巧,记得castisDoneisCancelledboolean,所以你可以使用这些字段作为boolean,所以你可以使用truefalse而不是@987654333 @ 或0

      最后一个提示,尽量遵守 Laravel 的约定:snake_case 列名isDoneisCancelled 应该是 is_doneis_cancelled

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        相关资源
        最近更新 更多