Relationship eloquent in laravel?

In user :

    public function posts()
{
return $this->hasMany(Post::class);
}

In post

    public function users()
{
return $this->belongsTo(Users::class);
}

I handle in controller :

$user = Users::find('1')->posts;

Then I get an array. And the result returned is exactly what I need


But when I query this way, the result is an empty array. What did I do wrong? Because I need to get a lot of data.

In UserController.php

$listUser = Users::with('posts')->select('name', 'title')
->where('type', 1)
->get(); // It returns posts as an empty array

Please give me any comments. Thanks



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3lfePAa
via IFTTT

تعليقات

المشاركات الشائعة من هذه المدونة

5 Free Black and White Social Media Icons Sets with CSS and HTML

How to Create Treeview with Bootstrap and jQuery

How to Change Apache and MySQL Port Number in XAMPP Localhost