Laravel eloquent grab user with posts ordered by - laravel

I am playing around with Laravel and I have this code. It returns all of a users posts, based on the provided username.
Controller
$posts = User::whereUsername($username)->firstOrFail()->posts;
return View::make('users.index')->with($posts);
View
#foreach($posts as $post)
{{ $post->title }}
{{ $post-owner->username }}
// etc etc
#endforeach
How would I go about adding an orderBy() to my query? I would like the posts to be ordered by newest first. This was my attempt :(
User::whereUsername($username)->firstOrFail()->posts->orderBy('created_at', 'DESC');
Or is there a better way to go about doing this?
EDIT
User Model
public function posts()
{
return $this->hasMany('Post');
}
Post Model
public function owner()
{
return $this->belongsTo('User', 'user_id');
}

Your code is slightly missing the () brackets on the relationship!
Try this
User::whereUsername($username)
->firstOrFail()
->posts()
->orderBy('created_at', 'DESC')
->get();
Note the brackets on posts()->

Related

Paginate Laravel Relation

I have two tables let's say posts and comments with Post and Comment Model.
Post.php
public function comments(){
return $this->hasMany(Comment::class);
}
Comment.php
public function post(){
return $this->belongsTo(Post::class);
}
Now I want to load Post with comments with pagination on both Post and comments.
What I tried so far,
Post::with('comments')->pagenate();
The above code only paginates on Post Model. So I tried the following code but no luck.
In Post.php
public function comments(){
return $this->belongsTo(Comment::class)->paginate(5);
}
Post::with('comments')->pagenate();
In the controller you must put the Post variable and for each post the Comments variable with it paginate, then you return the variables.
$posts = Post::orderBy('id', 'desc')->paginate(10);
foreach ($posts as $post)
$comments = $post->comments()->paginate(2);
return view('post.index', ['posts' => $posts, 'comments' => $comments]);
Now, in the blade view, you only need to put the #foreach of the comments variable in the post and add the links to page it for you.
#foreach( $comments as $comment )
<p> {{ $comment->desc }}</p>
#endforeach
{{ $comments->links() }}

Relationship with paginate

I need to create a dynamic link to take the user to the last page of the laravel page. For example, if there is a pagination, I need to include lastPage () at the end of the url www.site.com/forum/slug?page=2
otherwise it goes to the normal page.
But I need to do this logic on another controller, which in turn is not working. See my relationships and how I'm doing:
public function grupoPerfil($slug) {
$grupo = Grupo::where('slug', $slug)->first();
$comments = Comment::whereHas('relato', function ($query) use ($grupo) {
return $query->where('grupo_id', $grupo->id);
})->paginate(10);
if($grupo){
$rels = $grupo->relatos()->with('user')->paginate(20);
return view('grupo', compact(['grupo', 'rels','comments']));
}
abort(404);
}
my blade:
#foreach($rels->sortByDesc('updated_at') as $r)
<card-relatos
url="{{localized_route('relatoUser', [$r->grupo->slug, $r->slug])}}"
getpaginate="{{ $comments->lastPage() }}"
</card-relatos>
#endforeach
But getpaginate always returns 2, as it is not related to relato_id. How do I solve this?
This is what I have done:
return Product::whereHas('category', function ($query) use ($slug) {
$query->where('slug', $slug);
})->paginate(15);

Laravel: Passing data to view through route after querying

I recently asked a question about defining many to many relationships (using belongsToMany) and it was a huge help. So now in my models I have:
Users model
public function subjects()
{
return $this->belongsToMany('App\Subject', 'users_subjects', 'user_id', 'subjects_id');
}
Subjects model
public function users()
{
return $this->belongsToMany('App\User', 'users_subjects', 'subject_id', 'user_id');
}
This way I establish a relationship between users and subjects via the users_subjects table. My next step was to create a controller, SubjectsController, which ended up looking like this:
class SubjectsController extends Controller
{
// returns the view where subjects will be displayed
public function index()
{
return view('profiles.professor.prof_didactic_subjects');
}
// get users with subjects
public function getSubjects()
{
$subjects = User::with('subjects')->get();
}
// get a single user with a subject
public function getSubject($id)
{
$materia = User::where('id', '=', $id)->with('subjects')->first();
}
}
I'm not very sure about the code in the controller though.
The final step is where it gets tricky for me, even after reading the docs: I want to pass each result to the view, so I can have multiple tiles, each populated with data from subjects the user is associated with:
#foreach ($subjects as $subject)
<div class="tile is-parent">
<article class="tile is-child box">
<p class="title">{{ $subject['name'] }}</p>
<div class="content">
<p>{{ $subject['description'] }}</p>
</div>
</article>
</div>
#endforeach
I tried many different route configurations, but kept getting either the undefined variable error or trying to access non-object error.
What's the proper course of action here? I feel I'm missing something very basic. Thanks in advance for any help.
The answer
The solution provided below by #Sohel0415 worked perfectly. My index() method on the controller now looks like this:
public function index()
{
// temporary value while I figure out how to get the id of the current user
$user_id = 6;
$subjects = Subject::whereHas('users', function($q) use ($user_id){
$q->where('user_id', $user_id);
})->get();
return view('profiles.professor.prof_didactic_subjects')->with('subjects', $subjects);
}
My route looks like this:
Route::get('/professor', 'SubjectsController#index');
I was pretty lost, so this absolutely saved me, thanks again :)
You need to pass $subjects to your view. You can use compact() method for that like -
public function index()
{
$subjects = Subject::with('users')->get();
return view('profiles.professor.prof_didactic_subjects', compact('subjects'));
}
Or using with() method like -
public function index()
{
$subjects = Subject::with('users')->get();
return view('profiles.professor.prof_didactic_subjects')->with('subjects', $subjects);
}
If you want to get Subject for a particular user_id, use whereHas() -
$subjects = Subject::whereHas('users', function($q) use ($user_id){
$q->where('user_id', $user_id);
})->get();

OrderBy on Eloquent whereHas relationship

I have a simple page which lists counties and there related items under headings. These items need to be approved, hence the whereHas method.
I currently have the following eloquent query;
$counties = County::whereHas('items', function ($query) {
$query->where('approved', 1);
})->get();
The items returned are currently ordered by their primary field id (it would seem), however I want to list these items alphabetically by their name field.
I have tried the following query, but this does change anything. Any advice would be appreciated?
$counties = County::whereHas('items', function ($query) {
$query->where('approved', 1)->orderBy('name');
})->get();
$counties = County::whereHas('items', function ($query) {
$query->where('approved', 1);
})->orderBy('name')->get();
I don't think you can order on the subquery, it should be before the ->get
when you want to display the result , try this :
#foreach($counties as $county)
#foreach($county->items->orderBy('name') as $item)
{{ $item->name }}
#endforeach
#endforeach
Or in your County Models :
public function approvedItems(){
return $this->hasMany(Item::class)->where('approved', 1)->orderBy('name');
}
and then :
controller :
$counties = County::whereHas('approvedItems')->get();
view :
#foreach($counties as $county)
#foreach($county->approvedItems as $item)
{{ $item->name }}
#endforeach
#endforeach
Try to work with your models and relationships for having the lightest controller you can, you will gain in lisibility
To keep it eloquent, you can put it in the relation, in the Model class:
public function reviews()
{
return $this->hasMany(Review::class)->orderBy('id','desc');
}
https://laravel.io/forum/09-14-2015-ordering-a-collection-by-the-related-items
Might be late, but hopefully someone stumbles on this (it's the first in google search)
$users = Topic::with('latestPost')->get()->sortByDesc('latestPost.created_at');

Return relationship only if user is logged in?

I have a Posts and a Comments table, where each post can have multiple comments.
I want to make a query that gets all posts, and also all of the logged in user's comments.
Here is what I have so far:
$posts = Post::select('posts.*')
->with(['comments' => function($query) {
if (Auth::check()) {
$query->where('user_id', Auth::user()->id);
}
}])
->get();
And my Post.php model class looks like this:
class Post extends Model
{
public function comments()
{
return $this->hasMany('App\Comment');
}
}
The query returns the correct results when the user is logged in.
But when the user is not logged in, it returns the comments of ALL users, instead of returning nothing (because the user is logged in and therefore they have no comments).
How can I fix this?
you can do a little trick in your post model:
class Post extends Model
{
public function comments()
{
if(Auth::check()) {
return $this->hasMany('App\Comment')->where('user_id', Auth::user()->id);
}else{
return $this->hasMany('App\Comment')->where('user_id',-1);
}
}
}
and then simply :
$posts = Post::select('posts.*')->with('comments')->get()
so if user is not logged in it will return all comments with user_id of "-1" which will be nothing
There are two ways I can think of to this.
First you can only load the comments if the user is logged in:
$posts = Post::select('posts.*');
if(Auth::check()) {
$posts->with(['comments' => function($query) {
$query->where('user_id', Auth::user()->id);
}]);
}
$posts = $posts->get();
Or you could load all comments but set the user_id to null if the user isn't logged in. Since every comment should have a user_id no comments will be returned.
$posts = Post::select('posts.*')
->with(['comments' => function($query) {
$query->where('user_id', Auth::check() ? Auth::id() : null);
}])
->get();
The code in the second one looks cleaner IMO, but the first one will prevent an unnecessary query from being executed.
You can separate it, more legible and practical:
$posts = Post::all();
At your Post model create a function that will return all user's comments:
public function userComments()
{
return $this->comments->where('user_id', Auth::user()->id);
}
And i guess at your view you have a foreach to iterate all posts, inside your foreach you load post's comments, so you can do that:
#foreach($posts as $post)
$post->userComments()
#endforeach

Resources