ELOQUENT RELATIONSHIPS
Eloquent
relationships are defined as methods on your Eloquent model classes. Since
relationships also serve as powerful query builders,
defining relationships as methods provides powerful method chaining and
querying capabilities.
ONE TO ONE RELATIONSHIP
A
one-to-one relationship is a very basic type of database relationship. When one
table refers to single row in another table that is called a one-to-one
relationship. There should only be one matching record found in another table
where you have one-to-one relationship defined.
Firstly we create tables for one to one relationship using migration method in laravel framework
To create second table additionally to add Foreign
Key for table connectivity
public function up()
{
Schema::create('phones', function (Blueprint $table) {
$table->id();
$table->string('phone');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Then insert values in that tables
Create Models for the tables, and also create
Controllers then insert using model paths on the controller
use App\Models\User;
use App\Models\Phone;
Using public function in controller to join the to
tables for relationship
//One
To One Relationship
public function join(){
$user = User::with('phone2')->whereId(1)->first();
return Response::json($user);
}
In User model using hasOne command for one to
one relationship
public function phone2(){
return $this->hasOne(Phone::class);
}
In Route directory, web.php using for url of the relationship
Route::get('/join', [Usercontroller::class, 'join']);
Output :
{"id":1,"name":"Aslam","phone":"896754231","age":21,"created_at":"2022-03-18T10:41:51.000000Z","updated_at":"2022-03-18T10:41:51.000000Z","phone2":{"id":3,"phone":"88888888","user_id":1,"created_at":null,"updated_at":null}}
ONE TO ONE INVERSE
In
the One To One Inverse Relationship (belongsTo) its like access table2
to table1
Insert the public function using belongsTo
method in Post model like below the image
public function user(){
return $this->belongsTo(User::class);
}
Then come to controller add a public function, name
phone() its same like User class command to given below the image
public function phone(){
$phone = Phone::with('user')->whereId(3)->first();
return Response::json($phone);
}
Now call the function in route
Route::get('/phone', [Usercontroller::class, 'join']);
Out put:
{"id":3,"phone":"88888888","user_id":1,"created_at":null,"updated_at":null,"user":{"id":1,"name":"Aslam","phone":"896754231","age":21,"created_at":"2022-03-18T10:41:51.000000Z","updated_at":"2022-03-18T10:41:51.000000Z"}}
UPDATE VALUES IN PHONES TABLE
Go
to the controller you would then enter command like below, you could updated
your values in phone table using update command
public function po_up(){
$user = User::find(1);
$phone = new Phone;
$phone->phone = "77777777";
$user->phone2()->update($phone->toArray());
return 'saved';
}
Then come to the route and call it using function
name
Route::get('/po_up', [Usercontroller::class, 'po_up']);
Then you will the see the web page saved word is on their because we know our updating was held or not in the table
Now you check the mysql database table the changes was happened
CHANGE USER ID IN POST TABLE
Now
we see how could change user id in the post table,
First we will find which id was change select it
using find method, then we will set the path using associate method on the
function
public function chan_ui(){
$phone = Phone::find(3);
$user = User::find(2);
$phone->user()->associate($user)->save();
return 'saved';
}
Now we generate the url for the function in route
Route::get('/chan_ui', [Usercontroller::class, 'chan_ui']);
Run the serve see web page that saved word is on the page so changes happen
Now check out the database for verification the changes was held
The changes happen
Comments