Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fighter.id_fighter' in 'where clause'

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") }}" class="btn btn-primary"><i class="fa fa-arrows-rotate"></i>&nbsp;Update</a>
                    @csrf @method('DELETE')
                        <x-primary-button class="ml-3"> {{ __('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']));
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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');
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading