1
votes

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)

2
Please can you show the table structures or migration files for the models? Can you also show the query that is causing the error?Rwd
can you add the "....create_pass**_table.php" migration file as well?Ravisha Hesh
Schema::create('passes', function (Blueprint $table) { $table->increments('id'); $table->integer('number')->unsigned(); $table->integer('na_id')->unsigned(); $table->timestamps();Julfikar Haidar
Schema::create('salmen', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps();Julfikar Haidar
Please paste your $fillable also in model codeNatvarsinh Parmar - bapu

2 Answers

0
votes

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');  
0
votes

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

So in your migration file add:

$table->unsignedInteger("salman_id");