samedi 27 octobre 2018

laravel one to one relationship find column and update

I use laravel 5.6

I have 2 table Cartstatus and Order with one to one relationship .

Order migration :

Schema::create('orders', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('cartstatus_id')->default(0);
        $table->integer('invoice');
        $table->timestamps();
    });

Cartstatus migration :

Schema::create('cartstatuses', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('order_id')->nullable();
        $table->integer('designer')->default(0);
        $table->integer('status')->default(0);
        $table->timestamps();
    });

Order Model :

public function cartstatus()
{
    return $this->hasOne(Cartstatus::class);
}

Cartstatus Model :

protected $fillable = [
    'order_id', 'designer', 'status'
];

public function order()
{
    return $this->belongsTo(Order::class);
}

now in cartstatus controller ، I want to create a cartstatus by adding the cartstatus_id in the cartstatus_id column on the Order table :

Cartstatus Controller :

public function store(Request $request)
{
    $user_id = Auth::user()->id;

    $status = new Cartstatus();

    $status->order_id = $request->order_id;
    $status->designer = $user_id;
    $status->status = 1;

    $status->save();

    return redirect(route('myProjects.index'));
}

Thanks for help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire