// hasMany
$this->hasMany(Model::class);
// invers
$this->belongsTo(Model::class);
return $this->hasMany('AppComment', 'foreign_key');
return $this->hasMany('AppComment', 'foreign_key', 'local_key');
// function in model
public function hasManyFunction()
{
return $this->hasMany('AppModelsmodelName', 'targetedId');
}
// get data with hasMany relation in controller
$data = Post::with('hasManyFunction')->find(1)->;
<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
/**
* Get the comments for the blog post.
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
}
$users = User::where('id' ,7)->with('getAddress')->get();