I have three tables as below:
users
id|name|username|password
roles
id|name
users_roles
id|user_id|role_id
These tables communicate via belongsToMany.
I would like to find a way to select all data in “users” table except ones that their user value of "role_id" is 5 in table “users_roles”.
How can I do it?
You should use whereDoesntHave() to select models that don't have a related model meeting certain criteria:
$users = User::whereDoesntHave('roles', function($q){
$q->where('role_id', 5);
})->get();
Use Laravel's Query Builder:
<?php
$users = DB::table('users')
->leftJoin('users_roles', 'user.id', '=', 'users_roles.user_id')
->where('users_roles.role_id', '!=', 5)
->get();
http://laravel.com/docs/4.2/queries
Or using Eloquent directly:
<?php
$users = User::whereHas('users_roles', function($q)
{
$q->where('role_id', '!=', 5);
})->get();
http://laravel.com/docs/4.2/eloquent#querying-relations
<?php
$users = User::whereHas('roles', function($query) {
$query->where('id', '<>', 5);
})
->orHas('roles','<', 1)
->get();
I think the correct answer is:
User::whereHas('roles', function ($query) {
$query->whereId(5)
}, '=', 0)->get();
This code should send a query that checks if the role with id=5 is related to the user or not.
Edit
While I think this should work but the #lukasgeiter answer is preferable.
In the end both methods use the has() to count the related models by using a subquery in the db query where clause but when you use the whereDoesntHave() it specifies the operator < and the count 1 itself.
You can var_dump(DB::getQueryLog()) in App::after()'s callback to see the actual query.
Related
I have two table (users and messages) .. I wrote a query to get all messages that users sent to me or I sent, using JOIN .. to get all the users I have contacted or they did.
as in the code below:
$users = Message::join('users', function ($join) {
$join->on('messages.sender_id', '=', 'users.id')
->orOn('messages.receiver_id', '=', 'users.id');
})
->where(function ($q) {
$q->where('messages.sender_id', Auth::user()->id)
->orWhere('messages.receiver_id', Auth::user()->id);
})
->orderBy('messages.created', 'desc')
->groupBy('users.id')
->paginate();
The problem here is when records grouped, I'm getting the old message not the new one according to its created_at .. So, I want to get the last record of the grouped records.
It seems like it would make more sense to make use of Eloquent's relationships here so that you can eager load the relationships instead of having to use join and group by:
$messages = Message::with('sender', 'receiver')
->where(function ($query) {
$query->where('sender_id', auth()->id())
->orWhere('receiver_id', auth()->id())
})
->orderByDesc('created') // is this meant to be 'created_at'?
->paginate();
I need some help on my query. I use multiple tables with advanced join clauses but it shows invalid count and both has same value:
$parents = DB::table('users')
->select('users.id','users.full_name', 'users.email', 'users.avatar', 'users.signup_date', (DB::raw('count(children.id) as children_no')), (DB::raw('count(invitations.id) as invitations_no')))
->leftJoin('children', function ($join) {
$join->on('users.id', '=', 'children.userid')
->where('children.is_deleted', '=', 0);
})
->leftJoin('invitations', function ($join) {
$join->on('users.id', '=', 'invitations.user_id')
->where('invitations.is_deleted', '=', 0);
})
->where('users.is_admin', '=', 0)
->groupBy('users.id')
->get();
I think you can solve this problem with basic relationships in Laravel + soft deletes on the invitations and children table.
This will make your query less complex and you have the Laravel benefits.
<?php
$users = User::where('is_admin', false)
->has(['invitations', 'children'])
->withCount(['invitations', 'children'])
->get();
This wil select all none admins, with invitations and children.
Make sure you have soft deletes setup on children and invitations.
The withCount will add a count for the related relations.
I'm having a problem with my Search functionnality on my website, I have 2 tables: user and review , In my review table, the owner column is equal to the username column in user table, I want to be able to return in the same result the username of the user table and just below the number of review which I can get with:
Review::where('owner', '=', xxx)->where('invitation_id', '')->count();
The xxx should be equal to the username in the user table
And I have to do this to get the username:
User::where('username', '=', xxx)->first();
What I would like to do (I know this is wrong):
$result = User::where('email','LIKE','%'.$search_key.'%')
->orWhere('username','LIKE','%'.$search_key.'%')
AND
Review::where('username', '=', *$result->username* )
->get();
And I would like to be able to return the search result like this in my result.blade.php:
<h3>Username: {{ user->username }}</h3>
<h3>Username: {{ review->number_review }}</h3>
I checked on the Laravel docs to make a relationship between these 2 tables but can't figure it out, I hope what I said is understandable.
You can use eloquent relationship.
// app/Review.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
public function users()
{
return $this->hasOne('App\User', 'owner', 'username');
}
}
I do not suggest two table relation with username/owner. I suggest to you relation with user primary_id
You can get user info with following code;
Review::where('owner', '=', xxx)->where('invitation_id', '')->with('users')->count();
It getting user info with ->with('users') condition in Review model.
You achieve the required matching criteria by using join and parameter grouping clause
$result = DB::table('users as u')
->join('review as r', 'u.username', '=', 'r.owner')
->where('email','LIKE','%'.$search_key.'%')
->orWhere(function ($query) {
$query->where('u.username','LIKE','%'.$search_key.'%')
->where('r.owner','LIKE','%'.$search_key.'%');
})
->get();
Which will produce where clause as
WHERE u.email LIKE '%somevalue%' OR (r.owner LIKE '%somevalue%' AND u.username LIKE '%somevalue%')
For review count
$result = DB::table('users as u')
->select('u.*',DB::raw("COUNT(*) as review_count"))
->join('review as r', 'u.username', '=', 'r.owner')
->where('u.email','LIKE','%'.$search_key.'%')
->orWhere(function ($query) {
$query->where('u.username','LIKE','%'.$search_key.'%')
->where('r.owner','LIKE','%'.$search_key.'%');
})
->groupBy('u.username')
->get();
You will need to join your user table to the review table.
Something along these lines, might need tweaking.
$result = User::query()
->join('review', 'owner', 'username')
->where('email','LIKE','%'.$search_key.'%')
->orWhere('username','LIKE','%'.$search_key.'%')
->orWhere('username', $result->username)
->orWhere('owner', $result->username)
->get();
I want to create a chat system on which i could list all the chats between specific 2 persons
I have 2 tables users and chats
my chats table have 3 columns - user_id, friend_id and chat
my User.php model file is like this
public function chats() {
return $this->hasMany('App\Chat');
}
For eg:
I want to list all the chat between user 1 and 3 without changing the order of the conversation
I can simply do it by doing $chats = Auth::user()->chats->where('friend_id', '=', $id); but this will only give the authenticated (which is user 1 or 3) users chats. But I want the conversation between both of them.
So I have found an alternate way to do that by
$first = Chat::all()->where('user_id', '=', Auth::user()->id)->where('friend_id', '=', $id);
$second = Chat::all()->where('user_id', '=', $id)->where('friend_id', '=', Auth::user()->id);
$chats = $first->merge($second);
But this way has some problems. This will not get the chats in the correct order. I think it is impossible to order it correctly.
So my question is how can I list the conversation between two persons in the correct order easily?
If you want more details about my problem you can just ask.
You should be able to do it in one query with parameter grouping, rather than executing two separate queries and then merging them.
Chat::where(function ($query) use ($id) {
$query->where('user_id', '=', Auth::user()->id)
->where('friend_id', '=', $id);
})->orWhere(function ($query) use ($id) {
$query->where('user_id', '=', $id)
->where('friend_id', '=', Auth::user()->id);
})->get();
This might also return your results in the correct order, just because without any sort criteria specified, databases will often return rows in the order they were inserted. However, without adding something to your chat table to sort by, (either a timestamp or an autoincrement id), there's no way to guarantee it.
Try like this
$first = Chat::all()->where('user_id', '=', Auth::user()->id)
->where('friend_id', '=', $id)->get();
$second = Chat::all()->where('user_id', '=', $id)
->where('friend_id', '=', Auth::user()
->id)->get();
$chats = $first->merge($second)
->sortBy('created_at');//created_at is timing added change if other
First of all, you should not do all() before filtering. This is bad because fetches all the table data and then does the filtering in PHP.
You should consider doing this:
In your migration:
Schema::create("chat", function (Blueprint $table) {
//Other creation lines
$table->timestamps();
})
Then in your chat model:
public function scopeInvolvingUsers($query, $userId,$friendId) {
return $query->where([ ["user_id",$userId],["friend_id",$friendId] ])
->orWhere([ ["user_id",$friendId],["friend_id",$userId] ]);
}
Then you can do the following:
$chats = Chat::involvingUsers(\Auth::id(),$otherId)->latest()->get();
Note that latest or earliest requires the timestamps to be present on the table.
I will add timestamps in chat table which will ensure the order.
To add timestamp into chat table just add
$table->timestamps();
and the you can select the chat related to the user and sort it by created_at.
In laravel 5.3+ use
Chats::where(['user_id', '=', Auth::id()], ['friend_id', '=', $id])->orWhere(['user_id', '=', $id], ['friend_id', '=', Auth::id()])->sortBy('created_at');
Chat::whereIn('user_id', [$id, Auth->user()->id])
->whereIn('friend_id', [$id, Auth->user()->id])->get();
I have a query that currently works, however, it returns significantly more data than what I need.
Query
$alerts = Criteria::with('coordinate', 'alerts')
->where('user_id', '=', Auth::id())
->get();
For example, if I want to only select the viewed column out of alerts, can this be achieved in this query. I've used ::with to harness Laravel's eager loading feature.
Many thanks.
Use a closure to set the SELECT clause on your query:
$alerts = Criteria::with(['coordinate', 'alerts' => function($query)
{
$query->select('id', 'coordinate_id', 'viewed');
}])
->where('user_id', '=', Auth::id())
->get();
Remember to include the foreign keys, so that Eloquent can properly map them for you.