laravel belongsTo gives null

前端 未结 3 1915
小蘑菇
小蘑菇 2021-01-27 06:55

belongsTo relation in laravel is returning null

public function games(){
    return $this->belongsTo(\'App\\Models\\Game\');
  }

but when i

3条回答
  •  滥情空心
    2021-01-27 07:53

    The belongsTo side of the relationship builds the foreign key name based on the name of the relationship method. This behavior is different than the hasOne/hasMany side of the relationship, which uses the name of the class.

    In this case, your relationship method is named games, so it will look for the foreign key field games_id. You can either provide the foreign key field as the second parameter, as you have shown, or you can rename your relationship method to game().

提交回复
热议问题