Miqayel Petrosyan
Miqayel Petrosyan

Reputation: 47

Laravel belongsto relations get error SQLSTATE[42S22]

I get an error when calling $ event-> courts

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'courts.events_id' in 'where clause' (SQL: select * from courts where courts.events_id = 1 and courts.events_id is not null)",

Events

 <?php

namespace App\Models;

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

class Events extends Model
{
    use HasFactory;

    /**
     * @var string[]
     */
    protected $fillable = [
        'user_id',
        'event_name',
        'frequency',
        'start_date',
        'end_date',
        'day',
        'session_time',
    ];

   
    public function scopeMy($query)
    {
        return $query->where('user_id', auth()->id());
    }

 
    public function courts()
    {
        return $this->hasMany(Court::class);
    }
}

Court

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;

class Court extends Model
{
    use HasFactory;

    /**
     * @var string[]
     */
    protected $fillable = [
        'event_id',
        'name',
    ];

    public function event(){
        return $this->belongsTo(Events::class);
    }
}

Migration

Events

public function up()
{
    Schema::create('events', function (Blueprint $table) {
        $table->id();

        $table->foreignId('user_id')->constrained()->cascadeOnDelete();
        $table->string('event_name')->nullable();
        $table->string('frequency')->nullable();
        $table->string('start_date')->nullable();
        $table->string('end_date')->nullable();
        $table->string('day')->nullable();
        $table->string('session_time')->nullable();

        $table->timestamps();
    });
}

Courts

public function up()
{
    Schema::create('courts', function (Blueprint $table) {
        $table->id();

        $table->foreignId('event_id')->constrained()->cascadeOnDelete();
        $table->string('name')->nullable();

        $table->timestamps();
    });
}

Upvotes: 0

Views: 79

Answers (2)

John Lobo
John Lobo

Reputation: 15319

You need to change relation. You have to pass manually foreignKey and ownerKey

  public function event(){
        return $this->belongsTo(Events::class,'event_id','id');
    }

Then in court model also you need to change

 public function courts()
    {
        return $this->hasMany(Court::class, 'event_id','id');
    }

Upvotes: 1

zahid hasan emon
zahid hasan emon

Reputation: 6233

you have to mention your foreign key in the Events model relationship for the Court Model.

Eloquent will automatically determine the proper foreign key column for the Child model. By convention, Eloquent will take the "snake case" name of the parent model and suffix it with _id.

your model name is Events and the foreign key is event_id in Court Model, singular and plural issue. so change the relationship in Events model adding the second parameter

public function courts()
{
    return $this->hasMany(Court::class, 'event_id');
}

Upvotes: 2

Related Questions