0

私は3つのモデルを持っています。ユーザー、通知、およびLmo通知。その関係は、ユーザhasMany通知であり、通知には多くのLmoNotificationがあります。hasManyとbelongsTo Laravel

通知を挿入するとlmoNotification私は2つのモデルとコントローラを作成し、それらは別々のデータベーステーブルを持っています。

私の問題は、データベースに通知を入力するときに、外部キーとしてuser_idに問題なくデータを挿入することです。しかし、次に、私は悔しさを入力しようとすると、エラーが発生します。

通知モデル:

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('/', '[email protected]')->name('show.go_to_notification'); 
    /*route to lmo_notification_form*/ 
    Route::get('/personal_information_notification_form', '[email protected]'); 
    /*route to biohazardous material notification form*/ 
    Route::get('/biohazardous_material_notification_form', '[email protected]')->name('show.biohazardous_material_notification_form'); 
    /*submit lmo notification route*/ 
    Route::post('/personal_information_notification_form/submit', '[email protected]')->name('submit.personal_Info_Notification'); 
    /*Rtoute to lmo form*/ 
    Route::get('/personal_information_notification_form/add_lmo/{notification_id}', '[email protected]')->name('show.lmo_form'); 
    /*Route to submit lmo list for notification*/ 
    Route::post('/personal_information_notification_form/add_lmo', '[email protected]')->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(nullにすることはできません。(lmo_notificationslmo_namelmo_risk_levellmo_quantitylmo_volumenotification_idupdated_atを挿入、created_at)の値(ASD、メディア、2、2、2017年5月9日22時35分15秒、2017年5月9日22時35分15秒))

助けてください。

答えて

0

リレーション設定に問題はありません。

$notification->save()

:限り $notificationを使ってデータベースに永続化されていないようこと

$notification = new Notification;

注:あなたが使ってインスタンス化モデルのオフあなたの新しいAddLmoNotificationnotification_idを設定するという問題が開始さ

通知ではデータベースによってIDが割り当てられないため、id属性にアクセスするとnullが返されます。したがって、外部キーフィールドのid属性に依存するモデルを作成する前に、$notificationモデルを保存しておく必要があります。

+0

ありがとうございました。私はすでに別の方法でこれを行っています。 私は直接通知を次のビューに送信しました '( 'Notification'、$ notification); – Mill3r

関連する問題