how to fetch commented name on posts in laravel 5.4 - laravel

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

Related

Laravel display latest comment of a post

I have a table of threads, and on the index page I want to display the latest comment of the thread. So far I have selected the latest comment and try to display it on the page, but it only displays the latest comment overall, not the latest for the specific post
So my ThreadsController looks like this right now, selecting all the comments and displaying the latest one first.
public function index()
{
$threads = Thread::latest()->paginate(10);
$latestComment = Comment::latest()->first();
return view('threads.index', compact('threads', 'latestComment'));
}
Thread model
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
Comment Model
public function user() {
return $this->belongsTo(User::class);
}
public function thread()
{
return $this->belongsTo(Thread::class);
}
public function commentable() {
return $this->morphTo();
}
So how do I select the latest comment from the specific thread and display it on the index?
Edit:
Controller:
public function index()
{
$threads = Thread::latest()->with('comments')->paginate(10);
return view('threads.index', compact('threads'));
}
Index blade:
#foreach($threads as $thread)
{{ $thread->latestComment->user->name }}
#endforeach
Comments table migration
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('body');
$table->unsignedBigInteger('commentable_id');
$table->string('commentable_type');
$table->timestamps();
});
}
You have defined your relationship between a Thread and Comment correctly, but you're not making use of that relationship in the index function of your ThreadsController. You're just grabbing the latest 10 Threads and the most recent Comment, but not the comment related to your Thread.
What you're wanting is to get all your Threads and pass them to your view with their latest Comment.
You can use eager loading to attach your Comment relationships to your models.
Thread::latest()->with('comments')->paginate(10);
That will grab your 10 latest Threads along with their Comments. So in your view you could do something like:
#foreach ($threads as $thread)
{{ $thread->comments()->latest('id')->first()->comment }}
#endforeach
Whilst this works, it's a bit verbose. So what you can do is piggy back off your comments function to return just the most recent Comment as a relationship.
class Thread extends Model
{
use HasFactory;
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
public function latestComment()
{
return $this->morphOne(Comment::class, 'commentable')->latest('id');
}
}
This gives you shorter method for accessing the latest Comment for a Thread.
So going back to your scenario;
In your ThreadsController.php
public function index()
{
$threads = Thread::latest()->with('latestComment')->paginate(10);
return view('threads.index', compact('threads'));
}
Then in your index.blade.php file
#foreach ($threads as $thread)
{{-- Access whatever properties you have on your comment --}}
{{ $thread->latestComment->id }}
#endforeach
You can create a relation between tread and comment.
And then in blade you cam do something like this:
$thread->comments->latest()->first()
Comment::all()->orderBy('id', 'DESC')->get()

laravel one to many relationship return no error

I try to create a relationship between user and post. 1 user have many posts and 1 post has 1 user. I already write a code relationship between model User.php and Post.php and I want to store user data in database. my question is how to save data in database? btw I use postgresql. when I click button there is no error and cannot redirect to admin.post.index.
PostController.php
public function store(Request $request)
{
$user = auth()->user();
$post = $user->posts()->create($request->only('title','body'));
return redirect()->route('admin.post.index');
}
Post.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function posts()
{
return $this->hasmany('App\Post');
}
Change User.php
public function posts()
{
return $this->hasMany('App\Post');
}
you spelled incorrect
You can try to Change User.php
public function posts()
{
return $this->hasMany('App\Post','user_id');
}

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();

Why Eloquent HasMany return an empty collection?

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

Counting total posts by a user in the blade view

I have sent a collection of all posts in my blog to my index view and then used the following code to count the total posts made by each user.
<p class="joined-text">Posts: {{count(App\Posts::where('user_id', $post->user->id)->get())}}</p>
Is this bad practice to do this from within the blade view? If it is how would I achieve this?
Models
class Posts extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany(Comments::class, 'post_id');
}
}
class User extends Authenticatable
{
public function posts()
{
return $this->hasMany('\App\Posts::class');
}
public function comments()
{
return $this->hasMany(Comments::class);
}
}
Simple solution:
<p class="joined-text">Posts: {{ App\Posts::where('user_id', $post->user_id)->count() }}</p>
Updated
Complete and better solution:
Post.php:
public function user(){
return $this->belongsTo(App\User::class);
}
User.php:
public function posts(){
return $this->hasMany(App\Post::class);
}
public function getPostsCountAttribute(){
return $this->posts()->count();
}
blade:
<p class="joined-text">Posts: {{ $post->user->posts_count }}</p>
Yes it is bad.
You could use relationships if have the user_id field in posts table
class User extends Model
{
public function posts()
{
return $this->hasMany('App\Post');
}
}
In controller
return view('sth')->with(['posts'=>$user->posts]);
Then in view
$posts->count();
Or just getting counts if you don't need posts
$postCount = $user->posts()->count();

Resources