Spread the love

BelongsTo relationship in laravel is used to create the relation between two tables. belongsTo means create the relation one to one in inverse direction or its opposite of hasOne. For example if a user has a profile and we wanted to get profile with the user details then we can use belongsTo relationship.


Database Relations are used to create the relations between the tables so that we can manage the transactions in a better way. Similar to database relations laravel itself provides relationships out of the box to create the relation between tables in the application layer.

Laravel relations do not create relations in database it creates relation in application layer and produces the output.

Laravel eloquent belongsTo accepts 3 parameters first as model name, second is optional foreign_key and third is optional owner_key .

Here is the syntax

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Owner extends Model
{
     
    public function parent()
    {
        return $this->belongsTo(Parent::class,'foreign_key','owner_key');
    }
}

In the above syntax we have used belongsTo method with 3 parameters so here Parent::class is the name of Model which we want to relate, foreign_key means column name of related table and owner_key means column name of self table.

Laravel do not create join query internally but it creates a separate query and then attach the data.

So we can use it as below:

Owner::with('parent')->get();

This will return all the owner along with parent details.

Let’s understand laravel belongsTo in eloquent query with example

Use of Belongs to method in users and profile table

Suppose we have two tables one is users and another one is profiles so profiles table contains profiles of user table so here is the schema of both tables and you can also read How to make database connection in Laravel 8 ?

Step 1 : Create User and Profile model

Create two model in app\Models folder, one is users and other is profile

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasFactory;
}

Profile Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    use HasFactory;
}

Step 2 : Create belongTo relation in profile model

Now create belongs to relation in profiles table so that we can fetch user detail using user_id

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    use HasFactory;

    public function user(){
        return $this->belongsTo(User::class);
    }
}

In the above code we have created a method user and used belongsTo method to create the relation with User Model.

Laravel assumes here table name is users and foreign key is users table id and owner key is profiles table user_id. If you have different columns name to map then you can change.

Step 3 : Use the relationship in controller

We have created the relation now we need to access the model and its relations so lets create a controller and import the necessary classes

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Profile;


class UserController extends Controller
{

    public function index(Request $request)
    {

        $profile=Profile::with("user")->first();
        dump($profile);

        dump($profile->user);

    }
}

Step 4 : Create Route

Lastly create the route in web.php

Route::get('/user',[UserController::class, 'index']); 

Leave a Reply