我試圖讓2和表之間的關係:關係在laravel空5.5
我的模型:
class Modele extends Model
{
public function shoe()
{
return $this->hasMany('Shoe');
}
}
class Shoe extends Model
{
public function modele()
{
return $this->belongsTo(Modele::class, 'IdModele','id');
}
}
我的Controler:
class shoeController extends Controller
{
public function index()
{
$shoesList= \App\Shoe::with('modele')->orderBy('idModele')->get();
return view('shoe.index',compact('shoesList'));
}
}
When I dd($shoeList) , I have this:
#relations: array:1 [▼
"modele" => null
]
和如果我嘗試像這樣使用刀片中的參數:
<p>{{$shoe->modele->idGender}}</p>
IHAVE此錯誤:
ErrorException thrown with message "Trying to get property of non-object (View: C:\laragon\www\ipepsShoes2017\resources\views\shoe\index.blade.php)
我有做桌子之間的其他關係,在這個項目中使用相同的方法和they'r工作的罰款。
我不明白爲什麼它不起作用。
謝謝。
我不確定,但是在做'hasMany'時,你必須寫下表名,我不知道這個表是否是shoe的'shoe'。你能告訴我們你的表格結構嗎? – matiaslauriti
return $ this-> belongsTo(Modele :: class,'idModele','id');我認爲我必須是小字母nto capse – iCoders
這裏是我的表架構:https://photos.app.goo.gl/aGj9ATqhd8qxsMt92對不起,我不能上傳照片.... Modele和其他表格上的關係完美的作品... –