0

세 가지 모델이 있습니다. 사용자, 알림 및 Lmo 알림. 관계는 사용자 hasMany Notifications이고 통지에는 많은 LmoNotification이 있습니다.hasMany 및 belongsTo Laravel

알림을 삽입하려면 lmoNotification 두 모델과 컨트롤러를 만들었고 별도의 데이터베이스 테이블이 있습니다.

내 문제는 데이터베이스에 알림을 입력 할 때 외래 키로 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 제어기

0,123,169,532 78,347,

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] : 무결성 제약 조건 위반 : 1,048 열 'notification_id는'널 (SQL 수 없습니다 : (lmo_notifications에 삽입 lmo_name, lmo_risk_level, lmo_quantity, lmo_volume, notification_id, updated_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);을 사용하여보기를 ' '보기 ('Notification.notification_for_lmo ') ->로 직접 다음보기로 보냈습니다. – Mill3r