Laravel 5.2 eloquent model order by their relation model attribute - laravel

I have a question. I have an user model and a post model. A user can have many posts. Now I need to get posts with pagination. And I need to sort posts by their related user's id desc. Per page number is 10. I do not know how to write this code, someone can help me? Thanks.

Since Post belongs to User the posts table has user_id key. Use it:
Post::orderBy('user_id', 'desc')->paginate(10);

Related

Laravel get relation by custom value

Model - User
id [999]
name [foo]
Model - Post (without User Foreign Key)
id [1]
unique_key [USR_00000999]
data [bar]
I would like to get all user with related posts (one to many) by using "custom key" value, is this possible with build in eloquent?
I only manage to looping using foreach one by one with
Post::query()
->where('unique_key', sprintf('USR_%08d', $user_id))
->count();
That is not built-into Laravel by default... if you want to know why it's because it's not a common thing that everyone does...
BUT, you can use scope query so you don't need to do the sprintf everytime...
Laravel Query Scopes Documentation
But I want to ask, why wouldn't you just add user_id on your post table and just have an Accessors on your post model for generating the unique_key? That would be much easier on my perspective...
Laravel Accessor Documentation
UPDATE: (See Comment)
Is it possible to have an empty user_id on Posts table?
then populate it when the user is created?
say you have a posts with key of USR_00000999... and you have a user with an id of 999... when you create that user you'll just have to update all posts with key of USR_00000999 to have a user_id of 999...

Multi categories posts

I'm using laravel 5.6 and i have two tables categories and posts
I have created category_id in posts table which looks like
{id, category_id, title, description, created_at, updated_at}
I created a drop-down on the post create and edit form to select the category which works fine.
Now I am looking for something more advanced where a post can have multiple categories. I have changed belongTo to HasMany categories in post model.
I feel I am doing it the wrong way. Do i need to create another table i.e.,
post_categories
{id, category_id, post_id}
The reason i want to do this is because i have multiple posts which belong to multiple categories and my route is like this
site.com/categoryname/post-slug
So few posts appear in multiple categories.
You probably will need to use the pivot table. That way you can data mine. Even if you don't have a multi-select, you'll easily be able to collect posts linked to categories and vice versa. laraveldaily-good example. They use the sync method, one of my favs. when you save the form data you can just do something like App\Post::find($id)->categories()->sync(request()->input(categories')) and laravel will handle the rest for you.
Your relationships look like they are thought out. to me, it looks like your on the right track.
Just use the belongsToMany relationship instead of HasMany
Laravel has great documentation on this: laravel many to many

Laravel Find the Query

I am beginner of laravel
i have makes two table
1: users
2:Survey
user post the survey with some questions.
tell me the query how we can find the user who post the more survey then other users
If you're looking for Eloquent solution:
$user = User::withCount('surveys')->latest('surveys_count')->first();

Model Querying and Match laravel

I have this table articles and users and I already made model on them and made some relationships with them thru MODEL and some foreign keys. How will I display it in a view like this article is created by this user. How will I query it ? like
$article->user() bla bla.
$query=article::where(['id'=>1])->with('user')->first();
$email=$query->user->email;
print_r($email);
this gives the email of the user with article id 1, if proper models and relationships.

How to get related fields in another model from current model, using datamapper in CI?

I am a newbie user to CodeIgniter and DataMapper. I have a User model and User Group model. Both of this model has an one-to-many relationships. Is there any way I could get user_group_name in User Group using the User model?
Start by reading the Datamapper user guide, on http://datamapper.exitecms.org.
You can include columns from a related model into the result by using include_related().
You should familiarize yourself with codeigniter first.
As for your question, I think it would be better for you to get help from the datamapper forums or the codeigniter forums.
Wanwizard and Thorpe,
Thanx for both of you. I have got the solution. I only call Group model from User model. The following code would explain it.
<?php
foreach($users->all as $user)
{
$user->group->get();
}
?>

Resources