【问题标题】:hasMany and belongsTo LaravelhasMany 和属于 Laravel
【发布时间】:2017-10-08 10:04:09
【问题描述】:

我有 3 个模型。用户、通知和 Lmo 通知。关系是一个用户有很多通知,而通知有很多 LmoNotification。

为了插入通知和 lmoNotification,我创建了两个模型和控制器,它们有单独的数据库表。

我的问题是当我将通知输入数据库时​​,它以 user_id 作为外键插入数据而没有任何问题。但是接下来当我尝试输入 lmonotification 时,它给出了一个错误。

通知模型:

class Notification extends Model
{

  protected $table = "notifications";


  protected $fillable = [
    'unit_code', 'unit_name', 'project_title', 'project_ref_number', 'storage_location', 'keeper_name', 'user_id',
    ];

  public function user(){
    return $this->belongsTo('App\User');
  }

}

LmoNotification 模型

class AddLmoNotification extends Model
{

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */

protected $table = "lmo_notifications";

protected $fillable = [
    'lmo_name', 'lmo_risk_level', 'lmo_quantity', 'lmo_volume', 'notification_id', 
    ];

public function notification(){
    return $this->belongsTo('App\Notification');
}

}

通知控制器创建通知

public function create(Request $request)
{
    $notification = Notification::create([
        'unit_code'=>$request->unit_code,
        'unit_name'=>$request->unit_name,
        'project_title'=>$request->project_title,
        'project_ref_number'=>$request->project_ref_number,
        'storage_location'=>$request->storage_location,
        'keeper_name'=>$request->keeper_name,
        'user_id'=>Auth::user()->id
        ]);

        return redirect()-> route('show.lmo_form', $notification->id);

    // return view('ApplicationForms.notifi', $notification);
}

Lmonotification 控制器

 $notification = new Notification;

    /*this loop is because im adding rows dynamically to the table*/
    $count = count($request->input('lmo_name'));

        for ($i=0; $i<$count; $i++){

        $data = AddLmoNotification::create([
        'lmo_name'=>$request->lmo_name[$i],
        'lmo_risk_level'=>$request->lmo_risk_level[$i],
        'lmo_quantity'=>$request->lmo_quantity[$i],
        'lmo_volume'=>$request->lmo_volume[$i],
        'notification_id'=>$notification->id
        ]);
        return response($data);
    }

        //return response($notification);
        return redirect()->route('show.go_to_notification');
     }

web.php

Route::prefix('notification')->group(function(){
    /*show notification main page*/
    Route::get('/', 'HomeController@getNotificationPage')->name('show.go_to_notification');
    /*route to lmo_notification_form*/
    Route::get('/personal_information_notification_form', 'HomeController@getNotificationForm');
    /*route to biohazardous material notification form*/
    Route::get('/biohazardous_material_notification_form', 'HomeController@getotherNotificationForm')->name('show.biohazardous_material_notification_form');
    /*submit lmo notification route*/
    Route::post('/personal_information_notification_form/submit', 'NotificationController@create')->name('submit.personal_Info_Notification');
    /*Rtoute to lmo form*/
    Route::get('/personal_information_notification_form/add_lmo/{notification_id}', 'HomeController@getlmoNotificationForm')->name('show.lmo_form');
    /*Route to submit lmo list for notification*/
    Route::post('/personal_information_notification_form/add_lmo', 'LmoNotificationController@create')->name('submit.lmo_list');
});

通知表

Schema::create('notifications', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');

        $table->string('unit_code')->nullable();
        $table->string('unit_name')->nullable();
        $table->string('project_title')->nullable();
        $table->string('project_ref_number')->nullable();
        $table->string('storage_location');
        $table->string('keeper_name');

        $table->timestamps();


    });

Lmo 通知表

public function up()
{
    Schema::create('lmo_notifications', function (Blueprint $table) {

        $table->increments('id');


        $table->integer('notification_id')->unsigned();
        $table->foreign('notification_id')->references('id')->on('notifications')->onDelete('cascade')->onUpdate('cascade');


        $table->string('lmo_name');
        $table->string('lmo_risk_level');
        $table->string('lmo_quantity');
        $table->string('lmo_volume');

        $table->timestamps();

    });
}

通知表包含一些个人详细信息字段,lmonotification 包含产品列表。

错误信息表明

SQLSTATE[23000]:完整性约束违规:1048 列“notification_id”不能为空(SQL:插入lmo_notificationslmo_namelmo_risk_levellmo_quantitylmo_volumenotification_id,@ 987654334@, created_at) 值(asd, medium, 2, 2, , 2017-05-09 22:35:15, 2017-05-09 22:35:15))

请帮忙。

【问题讨论】:

    标签: mysql laravel-5 routes foreign-key-relationship laravel-eloquent


    【解决方案1】:

    您的关系设置没有任何问题。问题从您将新AddLmoNotificationnotification_id 设置为实例化模型的位置开始:

    $notification = new Notification;

    请注意,只要$notification 没有持久化到数据库中:

    $notification-&gt;save()

    该通知不会有数据库分配给它的 ID,因此,访问它的 id 属性会返回 null。因此,您必须先保存 $notification 模型,然后才能创建任何依赖其 id 属性作为外键字段的模型。

    【讨论】:

    • 谢谢。我已经以另一种方式做到了这一点。我直接用`return view('Notification.notification_for_lmo')->with('notification', $notification);' 将通知发送到下一个视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2018-04-01
    • 2021-12-14
    • 2021-03-22
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多