Home > Mobile >  Attempt to read property "match_name" on null laravel-8
Attempt to read property "match_name" on null laravel-8

Time:04-11

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

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

CodePudding user response:

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

CodePudding user response:

You must provide key in relation because your method name is matchh and in that case relation except matchh_id but in you case it must be match_id. So:

public function matchh(): BelongsTo
{
    return $this->belongsTo(Matchh::class, 'match_id');
}

CodePudding user response:

As I see, there is problem when score has not match or match not found. First of all, you have to specify relation column, because your model is named Matchh and laravel is looking for matchh_id column.

Solution:

# Score.php model
public function matchh(){
    return $this->belongsTo(Matchh::class,'match_id');
}

Tip 1:

You can use optional function, when you're not sure if model has relation or not

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

Tip 2:

You can use whereHas function to be sure that Score has Matchh

public function index()
{
    $data=Score::query()->whereHas('matchh')->get();
    $team=Team::all();
    $match=Matchh::all();
    $player=Player::all();

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