Julfikar Haidar
Julfikar Haidar

Reputation: 103

When I build an Eloquent relationship it makes problem here I show my code I use tinker to see relation but it doesn't build

When I build an Eloquent relationship it makes problem here I show my code I use tinker to see relation but it doesn't build

pass.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class pass extends Model
{
    public function salman()
    {
        return $this->belongsTo(salman::class);
    }
}

salman.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class salman extends Model
{
    public function passport()
    {
        return $this->hasOne(pass::class);
    }
}

Illuminate \ Database \ QueryException (42S22) SQLSTATE[42S22]: Column not found: 1054 Unknown column 'passes.salman_id' in 'where clause' (SQL: select * from passes where passes.salman_id = 1 and passes.salman_id is not null limit 1)

Previous exceptions

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'passes.salman_id' in 'where clause' (42S22)

Upvotes: 1

Views: 53

Answers (2)

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

In your database table passes you must have a column named salman_id.

So in your migration file add:

$table->unsignedInteger("salman_id");

Upvotes: 0

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

The error happens because Laravel looks for a salman_id(for table salman) by default.

If yes, then you can specify your custom primary key like below.

Change this line:

    return $this->belongsTo(salman::class);

To:

    return $this->belongsTo(salman::class, 'id', 'id');  

Upvotes: 0

Related Questions