Laravel Has Many

[Solved] Laravel Has Many | Php - Code Explorer | yomemimo.com
Question : laravel has many with ids

Answered by : ghulam-qadir

class Post extends Model
{ public function comments() {	return $this->hasMany(Comment::class, 'foreign_key', 'local_key'); // local id is the main id Of post table such as : id // foreign id is the post table which is inside comment table such as: post_id //return $this->hasMany(Comment::class, 'post_id', 'id'); //return $this->hasMany(Comment::class, 'post_id'); }
}

Source : | Last Update : Sat, 04 Sep 21

Question : laravel how to query belongsTo relationship

Answered by : fragile-flatworm-0c0no1b5zwnz

$movies = Movie::whereHas('director', function($q) { $q->where('name', 'great');
})->get();

Source : https://stackoverflow.com/questions/23970762/eloquent-where-condition-based-on-a-belongs-to-relationship | Last Update : Mon, 19 Oct 20

Question : eloquent relationships

Answered by : energetic-emu-dmvyfcbirsbd

$roles = App\User::find(1)->roles()->orderBy('name')->get();

Source : https://laravel.com/docs/7.x/eloquent-relationships | Last Update : Mon, 22 Jun 20

Answers related to laravel has many

Code Explorer Popular Question For Php