Home > Software design >  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fighter.id_fighter' in 'where
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fighter.id_fighter' in 'where

Time:10-05

I'm having trouble renaming the column's foreign key to the record name which makes it easier for the user to understand (line <td> {{ $msf->id_fighter == $msf->fighter->id ? $msf->fighter->name : '' }} </td>), But it is giving me the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fighter.id_fighter' in 'where clause' SELECT * FROM fighter WHERE fighter.id_fighter = 2 AND fighter.id_fighter IS NOT NULL

  • master.php

    @foreach ($master as $msf)
        <tr>
            <td> {{ $msf->id }} </td>
            <td> {{ $msf->name}} </td>  
            <td> {{ $msf->martial_art }} </td>  
            <td> {{ $msf->nacionality }} </td>  
            <td> {{ $msf->genre->value }} </td>
            <td> {{ $msf->height }} m </td>  
            <td> {{ $msf->weight }} kg </td>
            <td> {{ $msf->id_fighter == $msf->fighter->id ? $msf->fighter->name : '' }} </td>
            <td>
                <form action="{{ url("delete-master/$msf->id") }}" method="POST">
                    <a href="{{ url("update-master/$msf->id") }}" ><i ></i>&nbsp;Update</a>
                    @csrf @method('DELETE')
                        <x-primary-button > {{ __('Delete') }} </x-primary-button>  
                </form>
            </td>  
        </tr>   
    @endforeach

  • MasterModel.php

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use App\Enums\Genre;
    
    class MasterModel extends Model
    {
        use HasFactory;
        protected $table = 'master';
        protected $primary_key = 'id';
        public $timestamps = false;
        protected $fillable = [
            'name',
            'martial_art',
            'nacionality',
            'genre',
            'height',
            'weight',
            'id_fighter',
        ];
        protected $casts = [
            'genre' => Genre::class
        ];
        public function fighter(){
            return $this->hasMany(FighterModel::class,'id_fighter','id');
        }
    }

  • FighterModel.php

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use App\Enums\Genre;
    
    class FighterModel extends Model
    {
        use HasFactory;
        protected $table = 'fighter';
        protected $primary_key = 'id';
        public $timestamps = false;
        protected $fillable = [
            'name',
            'martial_art',
            'nacionality',
            'genre',
            'height',
            'weight',
        ];
        protected $casts = [
            'genre' => Genre::class
        ];
    }

  • MasterController.php

    public function index()
    {
        $master = MasterModel::paginate(5);
        $count_masters = DB::table('master')->distinct()->count('name');
        return view('master.master', compact(['master','count_masters']));
    }

CodePudding user response:

Since id_fighter is on the Master model, then Master really doesn't have many fighters, it only has one. You need to change your fighter relationship to be a hasOne(), and switch the columns around since the second parameter is the fighter table, the third is the master table.

public function fighter(){
     return $this->hasOne(FighterModel::class,'id','id_fighter');
}
  • Related