Hi Dear Friends,
In this post We would like to share infinity knowladge My Good experience and Best solution For .
Try this it will help you and for more information click here: Laravel Examples.
We hope it can help you...
In this post We would like to share infinity knowladge My Good experience and Best solution For .
Laravel Examples
One to One Relationship
Demo details:
In this demo we have 2 models (Owner and Car), and 2 tables (owners and cars).
Business Rules:
The Owner can own one Car.
The Car can be owned by one Owner.
The Car can be owned by one Owner.
Relations Diagram:
Relationship Details:
The Cars table should store the Owner ID.
Eloquent Models:
class Owner
{
public function car()
{
return $this->hasOne(Car::class);
}
}
class Car
{
public function owner()
{
return $this->belongsTo(Owner::class);
}
}
Database Migrations:
Schema::create('owners', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('owner_id')->unsigned()->index()->nullable();
$table->foreign('owner_id')->references('id')->on('owners');
});
Store Records:
// Create relation between Owner and Car.
$owner->car()->save($car);
// Create relation between Car and Owner.
$car->owner()->associate($owner)->save();
Retrieve Records:
// Get Owner Car
$owner->car;
// Get Car Owner
$car->owner;
Read :
I hope you like this Post, Please feel free to comment below, your Any Idea, suggestion and problems if you face - I am here to Resolve your Any problems.We hope it can help you...

No comments :
Post a Comment