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

Attempt to read property "match_name" on null laravel-8

I have

ErrorException
Attempt to read property "match_name" on null (View: C:\xampp\htdocs\Cbangla\resources\views\admin\manage\score\index.blade.php)
Error

I want to fetch all data from my score tables This is my scores table database view

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

To fetch, all data from the scores table This is what I have in my ScoreController.php

public function index()
{
    $data=Score::all();
    $team=Team::all();
    $match=Matchh::all();
    $player=Player::all();

    return view('admin.manage.score.index',compact('data','team','match','player'));
}

This is my Score.php model

protected $fillable = ['score_name','score_slug','team_id','match_id','player_id'];

    public function team(){
        return $this->belongsTo(Team::class);
    }
    public function matchh(){
        return $this->belongsTo(Matchh::class);
    }
    public function playre(){
        return $this->belongsTo(Player::class);
    }

This is my index.blade.php

@foreach ($data as $key => $row)
   <tr>
       <td>{{ $key + 1 }}</td>
       <td>{{ $row->score_name }}</td>
       <td>{{ $row->score_slug }}</td>
       <td>{{ $row->matchh->match_name }}</td>
       <td>{{ $row->team->team_name }}</td>
       <td>{{ $row->player->player_name }}</td>
       </tr>
@endforeach

>Solution :

Please add match_id to matchh relationship case the name of your relationship is different than the match id you stored in database

 public function matchh(){
        return $this->belongsTo(Matchh::class,'match_id');
    }
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