Answer the question
In order to leave comments, you need to log in
BelongsTo hasOne or how to get data from related table?
I’m stuck, I can’t get off the ground, please explain with my example:
There are two tables Districts (structure id, district) and Announcements (structure id, district_id, text)
Conditionally in the Announcements controller I write the following:
public function allads(){
$adsall=Ads::orderBy('id','desc')->paginate(10);
return view('test2', compact('adsall'));
}
Answer the question
In order to leave comments, you need to log in
class District extends Model {
public function ads () {
$this->hasMany(Ad::class);
}
}
class Ad extends Model {
public function district () {
$this->belongsTo(District ::class);
}
}
$adsall = Ad::with(['district'])->latest()->paginate(10);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question