Relationships in laravel
Get data from multiple tables
we have two tables profiles
And users
Add a new function in model app\Models\User.php
SYNTAX:
return $this->hasOne(Phone::class, 'foreign_key', 'local_key');
//FOREIGN KEY MEANS key of Phone model,
public function profile(){
return $this->hasOne(profile::class); //each user has only one profile OR
// return $this->hasOne(profile::class, 'user_id'); //forigen key specify
//return $this->hasOne(profile::class, 'user_id', 'phone'); //multiple foregin keys
}
In HomeController.php add below code and add use App\Models\User;
public function getRelationship(){
$data = User::with('profile')->get(); //profile function in user model
return $data;
}
profile is method in User Model
Web.php
Route::get('getRelationship', [App\Http\Controllers\HomeController::class, 'getRelationship'])->name('getRelationship');
Visit URL http://127.0.0.1:8000/getRelationship you will get
[
{
"id": 1,
"name": "Rizwan",
"email": "rizwan@gmail.com",
"email_verified_at": "2022-01-15T21:42:35.000000Z",
"created_at": "2022-01-15T21:10:18.000000Z",
"updated_at": "2022-01-15T21:45:23.000000Z",
"profile": {
"id": 1,
"user_id": 1,
"phone": "923461234567",
"address": "kmw",
"created_at": null,
"updated_at": null
}
},
{
"id": 2,
"name": "muhammad yaser",
"email": "filereal@live.com",
"email_verified_at": "2022-01-15T23:29:27.000000Z",
"created_at": "2022-01-15T23:29:09.000000Z",
"updated_at": "2022-01-15T23:29:27.000000Z",
"profile": null
}
]
INVERSE RELATION:
INVERSE RELATIONSHIP >> IN Profile MODEL USE
public function user()
{
return $this->belongsTo(User::class);
}
Then you can get profile with user details
$data = Profile::with('user')->get(); //profile function in user model
user is method in profile model,
___________________________
You can also get records by profiles relationship
profile.php model
public function user(){
return $this->belongsTo(user::class); //each user has only one profile
}
Controller.php with use App\Models\Profile;
public function getRelationship(){
$data = Profile::with('user')->get(); //profile function in user model
return $data;
}
Similarly You can make one to many relationships
For details visit Eloquent: Relationships - Laravel - The PHP Framework For Web Artisans
______________________________________
A user has many phones, Get all phone numbers of a user that have id 1
public function phone()
{
return $this->hasMany(PhoneNumber::class);
}
and then call in User controller
$user = \App\Models\User::find(1)->phone->toArray();
For pagination
ONE TO MANY
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
then call it
$comments = Post::find(1)->comments;
ONE TO MANY INVERSE
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
}
Then in controller
$comment = Comment::find(1);
return $comment->post->title;
Relationships:
ONE TO ONE:
$this->hasOne(Phone::class,); //a user have one phone
One To Many
$this->hasMany(Comment::class); // A post contain many comments
Will get one post and its all comments but comments will pagination of 5 per page
$post_details= \App\Models\Post::with(['Comment' => function ($query) {
$query->paginate(5);
}])
->take(1)
->first();
Has One Of Many
$this->hasOne(Order::class)->latestOfMany(); //latest order
$this->hasOne(Order::class)->oldestOfMany(); //old order
$this->hasOne(Order::class)->ofMany('price', 'max'); //expensive order
Has one through: hasOneThrough
3 models , ie car, owner and mechanic
mechanics: id,name
cars: id,model,mechanic_id
owners: id, name, car_id
class Mechanic extends Model
{
public function carOwner()
{ // Get the car's owner. using card in between mechanic and owner
return $this->hasOneThrough(Owner::class, Car::class);
}
}
Has Many Through: hasManyThrough
projects: id, name
environments: id, project_id, name
deployments: id, environment_id, commit_hash
class Project extends Model
{
// Get all of the deployments for the project.
public function deployments()
{
return $this->hasManyThrough(Deployment::class, Environment::class);
}
}
Many to Many: ?