mardi 9 mai 2017

hasMany and belongsTo Laravel

I have 3 models. User, Notification and Lmo Notifications. The relation is a User hasMany Notifications and Notification has many LmoNotification.

to insert the notification and lmoNotification i created two models and controllers and they have separate database tables.

My problem is when i enter notifications into the database, it inserts data without any problem with user_id as the foreign key. But next when i try to enter the lmonotification, it gives an error.

Notification Model:

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 Model

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');
}

}

Notification controller to create the 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 controller

 $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');
});

Notification table

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 Notification table

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();

    });
}

the notification table contains some personal details fields and the lmonotification contains list of products.

the error message says that

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'notification_id' cannot be null (SQL: insert into lmo_notifications (lmo_name, lmo_risk_level, lmo_quantity, lmo_volume, notification_id, updated_at, created_at) values (asd, medium, 2, 2, , 2017-05-09 22:35:15, 2017-05-09 22:35:15))

Please help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire