Answers for "eager load relationship laravel"

PHP
2

laravel eager loading where clause

$query = ""    

if ($request->has('country'){
$query = Post::with("country")->whereHas("country",function($q) use($request){
    $q->where("name","=",$request->country);
})->get()
}else{
    $query = Post::with("country")->get();
}
Posted by: Guest on August-16-2020
0

eager load relationships by default in the model laravel

class Post extends Model
{
    protected $with = ['category', 'author'];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function author()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
Posted by: Guest on January-11-2022

Code answers related to "eager load relationship laravel"

Browse Popular Code Answers by Language