Why Eloquent HasMany return an empty collection? - laravel

Hi why i get an empty collection when i use $user->posts as property. However i do a get collection when i use $user->posts() as function.
public function user(){
return $this->belongsTo(User::class);
}
public function posts(){
return $this->hasMany(Post::class);
}
public function index(User $user){
dd($user->posts);
return view('home', compact('user'));
}
#foreach($user->posts as $post)
<p>$post->title</p>
<p>$post->body</p>
#endforeach
result
Collection {#1102 ▼
#items: []
}

The first method (user) should be in your post model.
The second method (posts) should be in your user model.
Check your database and see if the posts table has a column named user_id.
Check your database and see if the user_id column has a valid value (according to the users table.
Also make sure your route has {user} in it. Something like this:
Route::get('home/{user}', 'Cotroller#method')

User model:
public function posts(){
return $this->belongsTo(User::class);
}
Post model:
public function user(){
return $this->hasMany(Post::class);
}
And make sure you have user_id column on your post table

Related

Laravel Jetstream (Livewire) not returning user through eloquent relationship

I'm trying to return the user name who submitted an article, but unable to access this via an eloquent relationship.
Models/Thing
public function users()
{
return $this->belongsTo(User::class);
}
with user_id stored in the things table
However this dump isn't returning user data: dd($this->thing->users->name);
Error:
Attempt to read property "name" on null
try this
return $this->belongsTo(User::class, 'user_id', 'id');
in my view you are using users with belongTo. it will work if you will use.
public function user(){
return $this->belongsTo( User::class );
}
and then
dd( $this->thing->user->name );

Laravel Eloquent Relationship has the same foreign key on the same table

I'm a beginner in Laravel and currently trying to create a ticketing system. My tickets table has two columns which both reference id from the users table namely user_id and handled_by. I am trying to eager load the full_name of handled_by but it is throwing an error "Trying to get property 'full_name' of non-object". I would like to know how to properly create an eloquent relationship between handled_by and tickets so I could display the handled_by's full_name. Below are snippets of my code.
User Model:
public function tickets(){
return $this->hasMany('\App\Ticket');
}
public function handledBy(){
return $this->hasMany('\App\User', 'handled_by', 'id');
}
Ticket Model:
public function user(){
return $this->belongsTo('\App\User');
}
public function handledBy(){
return $this->belongsTo('\App\User', 'handled_by', 'id');
}
View:
#foreach($tickets as $ticket)
<td class="align-middle text-center">{{ $ticket->handled_by->full_name}}</td>
#endforeach
Controller:
$tickets = Ticket::all()->where('status_id', '1');
return view('admin.open_tickets', compact('tickets'));
In your User model, change \App\User to \App\Ticket, and change the method name from handlerBy to handlers:
public function handlers(){
return $this->hasMany('\App\Ticket', 'handled_by', 'id');
}
And call it by method's name instead of underline case name:
{{ $ticket->handledBy->full_name}}

one to may relationship, Display all post along with there users

//Post table
user()
`belongsTo(User::class);`
//user table
posts()
hasMany(post::class);
//I want to get all the post available in the DB, Display along with the users associated with the post.
Use foreign key and primary key in relationships to get the results.
In Post
public function user()
{
return $this->belongsTo('App\User','users_id','users_id') //first parameter-foreign key,second parameter-local key
}
In User
public function posts()
{
return $this->hasMany('App\Post','users_id','users_id')
}
Now get all the posts using
$posts = Post::get();
return view('your-view')->with('posts',$posts);
You can retrieve user information in view using
#foreach($posts as $post)
$user = $post->user->name;
#endforeach
Hope it helps..
Guessing your relations look like this:
User:
public function posts()
{
return $this->hasMany('App\Post');
}
Post:
public function user()
{
return $this->belongsTo('App\User')
}
You could get all the posts in your controller, like this:
$posts = Post::get();
return view('your view', $posts);
And in your blade, to display posts and their users, do it like this:
#foreach($posts as $post)
$post->user->name
#endforeach
$posts = Post::with('user')->get();

how to fetch commented name on posts in laravel 5.4

i followed the convention to avail the required data with compact method to view
Schema User Table
id,name,email,password
Schema post Table
id,user_id,title,body,photo
Schema comment Table
id,post_id,user_id,comments,photo
in user model
public function post()
{
return $this->hasMany(post::class);
}
public function comments()
{
return $this->hasMany(comment::class);
}
in post model
Public function User()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->hasMany(comment::class);
}
in comment model
public function posts()
{
return $this->belongsTo('App\post');
}
in controller
$list = User::with('post','comments')->paginate(1);
return view('dynamic',compact('list'));
in view
#foreach($lists->comments as $comments)
{{$comments->name}}
#endforeach
post showing successfully according to users
and comments showing successfully according to posts
but commented name not showing up // please help
how to make relationship for fetch comments with commented user name
please help thanks in advance
you have to change your code as below
#foreach($lists->comments as $comments)
{{$comments->posts->user->name}}
#endforeach
or
you need to add relationship to your comment model as below
Public function User()
{
return $this->belongsTo('App\User');
}
after that you need to change your code as below
#foreach($lists->comments as $comments)
{{$comments->user->name}}
#endforeach
change code as below in your view:
#foreach($lists->comments as $comments)
{{$comments->User->name}}
#endforeach

Laravel Eloquent all with relationship

How can I get all data from my tables with relationship in Laravel?
So, I have table like:
*users*
id
username
password
...
*address*
id
address
user_id
...
If I call my eloquent model with Address::all() I get response with user_id, and I want to make relationships for getting username.
I try to add:
public function users(){
return $this->belongsTo('Users', 'user_id');
}
And call with Address::with('users')->get(), but response is empty.
Is your user class called Users or User? Unless you override it, the default way that Eloquent deals with names can be found in the docs (source):
Note that we did not tell Eloquent which table to use for our User model. The lower-case, plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the User model stores records in the users table. You may specify a custom table by defining a table property on your model
So I believe if you change your code in the Address model to:
public function users(){
return $this->belongsTo('User', 'user_id');
}
it will work since we have changed Users to User
You need to change
public function users(){
return $this->belongsTo('Users', 'user_id');
}
to
public function users(){
return $this->hasOne(Users::class, 'user_id');
}
or
public function users(){
return $this->hasMany(Users::class, 'user_id');
}
Alternative way!
class Users extends Model {
//override table
protected $table = 'users';
protected $primaryKey = 'id';
//Your other code here
}
and in Address Model add like this
public function users(){
return $this->belongsTo('App\Users', 'user_id');
}

Resources