sql-serverlaraveleloquentrelationship

Where 0 = 1 in Laravel BelongsTo Relationship Query Log


I'm just trying to use BelongsTo relationship to retrieve the records, Here is the scheme:

roles:
- id
- title
users:
- id
- Role_ID

I'm using SQL Server for Database so the Role_ID can pointed as role_id I think :D.

and here is the code for the belongs to relationship in User Model:

public function role()
    {
        return $this->belongsTo('App\Role');
    }

I also have tried like this:

public function role()
    {
        return $this->belongsTo('App\Role', 'id', 'Role_ID');
    }

But whatever the parameters are it doesn't work and when I get the Query log, it looks like as:

bindings: []
query: "select * from [roles] where 0 = 1"
time: 1.49

Here is how I try to access the relation in the Controller:

User::with(['role'])->get($someColumns)

I'm just fighting with this problem for many times but don't know where the issue is.

Any help is appreciated.

Thanks everybody.


Solution

  • You have to include the foreign key in get() columns

    User::with(['role'])->get(Role_ID, OthersColumns)