Trying to get property 'name' of non-object in commentary Laravel system - php

I'm trying to get the Username of author to display that near comments which they wrote, when displaying a comments on page. I've got an error "Trying to get property 'name' of non-object"
Controller:
public function index(Site $site)
{
$comments=Comments::where('siteId', $site->id)->get();
return view('admin.comments.show', compact('comments'));
}
View:
#foreach($comments as $comment)
{{$comment->user->name}}
#endforeach
User model:
public function comments()
{
return $this->hasMany(Comments::class);
}
Comment model:
public function comments()
{
return $this->belongsTo(User::class);
}
I want to use relations. Thank you for help! :)

Change the relationship in the Comment model to this:
public function user()
{
return $this->belongsTo(User::class);
}
That should work.

In user model:
public function comments()
{
return $this->hasMany(Comment::class); // change Comments to Comment here
}
In Comment model:
public function user()
{
return $this->belongsTo(User::class);
}

Related

How to use Multiple relationship in laravel 5.4

In my app i have define relationship (profile, user, level) but when I fetch data it is showing an error (Trying to get property 'email' of non-object) how can i solve this thank in advance.
this is User Model
public function profile()
{
return $this->hasOne(Profile::class, 'user_id');
}
Profile Model
public function user()
{
return $this->belongsTo(User::class, 'id');
}
public function level()
{
return $this->belongsTo(Level::class, 'id');
}
Level Model
public function profile()
{
return $this->hasOne(Profile::class, 'level_id');
}
This is Controller ProfileController
$users = Profile::with(['user', 'level'])->where('is_bd_partner', 'Yes')->get();
foreach ($users as $key => $value)
{
echo $value->first_name.'<br>';
echo $value->last_name.'<br>';
echo $value->user->email.'<br>';
echo $value->level->level.'<br>';
}
Note that belongsTo takes foreign_key as the first parameter.
So you should change the Profile Model as
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function level()
{
return $this->belongsTo(Level::class, 'level_id');
}
Read more here

Controller method issue in Laravel Eloquent

This is my Controller method wherein I want to fetch posts by user id.
I also have relationship defined in User model
public function posts()
{
return $this->hasMany('Post','user_id','id');
}
UserController.php
use App\Post;
public function findPostsByUser($id){
$user = User::findOrFail($id);
$posts = $user->posts()->get();
return $posts;
}
But it is giving me 'Post' not found error. Can someone help on this.
In your Post model make sure you have the relation defined like this:
public function user()
{
return $this->belongsTo('App\User');
}
And change your User model to this:
public function posts()
{
return $this->hasMany('App\Post','user_id','id');
}
Change relation method to
use App\Post;
....
public function posts()
{
return $this->hasMany(Post::class,'user_id','id');
}
or
public function posts()
{
return $this->hasMany('App\Post','user_id','id');
}
If you wont to get only posts that case use this
public function findPostsByUser($id){
$posts = Post::where('user_id', $id)->get();
return $posts;
}
The second solution has 1 query, but first solution has 2 query in db.
Also you can do it this way
public function findPostsByUser($id){
$posts = Post::whereUserId($id)->get();
return $posts;
}

How to paginate for hasMany relationship in laravel (App\News::user must return a relationship instance)

I am not able to paginate in laravel in this situation
return $this->hasMany('App\News','category_id')->orderBy('id','desc')->paginate(20);
but says error. in controller I have also tried
$byCategories=Category::findOrFail($id)->paginate(20);`
it also says error.
help me.
My Model is
class Category extends Model
{
public function news()
{
return $this->hasMany('App\News','category_id')->orderBy('id','desc');
}
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
public function user()
{
return $this->belongsTo('App\User');
}
public function getRouteKeyName()
{
return 'slug';
}
}
another model one is
class News extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function category()
{
return $this->belongsTo('App\Category');
}
}
my controller code is
public function byCategory($id)
{
$byCategories=Category::findOrFail($id);
return view('back-end/news/byCategory', compact('byCategories'));
}
Thank you so much.
Pagination is not done in the Model
it is to be done in the controller, For example remove (->paginate(20);) from here
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
keep it only as
public function newsMany()
{
return $this->belongsToMany('App\News');
}
and call the pagination in controller when returning the view
$news=Category::findOrFail($id)->newsMany()->paginate(20);
return view('view name', compact('news'));

Laravel- 5.5 App\Comment::user must return a relationship instance.

I want to display products comments. But When I do that, it gives me above error. How can I fix that ?
I'm using One To Many relationship beetwen product-comments and user-comments
Product model;
public function comments(){
return $this->hasMany('App\Comment','product_id','id');
}
User Model;
public function comments() {
return $this->hasMany('App\Comment','user_id','id');
}
Comment Model;
public function user(){
$this->belongsTo('App\User');
}
public function product(){
$this->belongsTo('App\Product');
}
Blade file
<figcaption class="text-center">{{$comment->user->username}}</figcaption>
You need to return relationship. So add return to the user() relationship definition method:
public function user()
{
return $this->belongsTo('App\User');
}
The same is with the product() relationship:
public function product()
{
return $this->belongsTo('App\Product');
}

Deleting a post on laravel 4

I tried this approach but no luck, I keep getting an error.
Call to a member function delete() on a non-object
Post::find($id)->delete();
Relationships:
public function posts()
{
return $this->hasMany('Post');
}
public function user()
{
return $this->belongsTo('User');
}
Solved using this.
{{ Form::open(array('method'=>'DELETE', 'route'=>array('post.delete', $post->id))) }}

Categories