Laravel Find the Query - laravel

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

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...

Facing issue in laravel orderby

I am facing a issue with laravel order by function.
i want to fetch the records according to country and the result should start from the current user's country.
For example.
if auth user belong to india then list should start from india.
My Current code is
$users = User::where('status', '=',1)->with(['profile'])->latest()->orderBy('last_login', 'desc')->paginate(12);
Thanks
Ok, whatever I've understood, you are looking to sort the data through the country attributes from profiles table, which is in relation with User model, for this you need to use join I'm not sure about your structure so I'm just giving you an example. Suppose you want to sort by profiles => id then you can write something:
Edit: As per the requirement given in comments I would recommend to have something like this:
First of all lets have authenticated user by
$AuthUser = Auth::user();
Then lets call the relation
$users = User::->leftJoinRelations('profile')
->orderByRaw("FIELD(country , ". $AuthUser->profile->country ."), ASC")
->paginate();
So in this way you can have user sorted by authenticated user country. Hope this helps.

Laravel 5.2 eloquent model order by their relation model attribute

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

Translate query to Laravel Eloquent Model

How would I translate the following query into a format that Laravel can parse?
select posts.id,title from posts,albums where posts.id != albums.post_id
Now it has this ORM relations
a Post hasOne Album
an Album Belongs to a Post
So with this statement I want to know which Posts dont have Albums binded.
so far I have
Post::with('album')->select('id', 'title')->get();
I don't know how to declare the condition
Please do read docs again, I think you may look for polymorphic relationship or One to Many. If you use Eloquent ORM do use relationships its the biggest why "YES".
As for the query you can do http://laravel.com/docs/4.2/queries#raw-expressions
There is an example in a link just fill in your query and you are done.

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