laravel 5.2 nested Where clause - php

How to perform a where clause using laravel eloquent with the following query:
select * from stats where payed = 1 AND WHERE (user_id = 1 OR owner_id = 1)
I don't know how to modify this code for this:
$payouts = Stat::where('payed', '1')
->???(function ($query) {
$query->where('owner_id', Auth::user()->id)
->where('user_id', Auth::user()->id);
})->get();

Change like this
$payouts = Stat::where('payed', '1');
$payouts->where(function ($payouts) {
$payouts->where('owner_id', Auth::user()->id)
->orWhere('user_id', Auth::user()->id);
})->get();

Try this
$payouts = Stat::where('payed', '1')
->where(function ($query) {
$query->where('owner_id', Auth::user()->id)
->orwhere('user_id', Auth::user()->id);
})->get();

You're close, this should do the trick:
$payouts = Stat::where('payed', '1')
->where(function ($query) {
$query->where('owner_id', Auth::user()->id)
->orWhere('user_id', Auth::user()->id);
})->get();
This is looking for where payed = 1 AND (owner_id is the current user ID OR the user_id is the current user ID)

Related

Laravel 8 Eloqunet: How to set OR condition to where clause in Laravel

I want to show all the Orders from orders table where the status field of that table, equals to canceled OR ignored.
Currently my query goes like this:
$canceled = Order::where('status', 'canceled')
->where('user_id', $uid)
->latest()
->paginate(2);
So how can I add ignored as well to the where clause here?
I think whereIn will do the trick
$canceled = Order::whereIn('status', ['canceled','ignored'])
->where('user_id', $uid)
->latest()
->paginate(2);
Try this query -
$canceled = Order::where(function($q) {
$q->where('status', 'canceled')
->orWhere('status', 'ignored');
})
->where('user_id', $uid)
->latest()
->paginate(2);

How to use Eloquent model property in subquery?

I have two tables (users, posts) and I'm trying to order users according to recent post's date.
But unfortunately I couldn't find a way to use User model's firebase_id to use in subquery. I don't want to use raw query or DB directly if it's possible to use Eloquent for such query.
How can I use User model's firebase_id as subquery parameter?
Here is my non-working code to give an idea about what I try to do:
User::select('users.*', DB::raw("SELECT MAX(id) FROM posts WHERE posts.firebase_id = users.firebase_id as recent_post_date"))
->whereHas('posts', function ($q) {
$q->withCount('comments');
})
->with(['posts' => function ($q) {
$q->withCount('comments')
->orderBy('created_at', 'ASC');
}])
->orderBy('recent_post_date', 'DESC')
->get();
You can use selectSub to add a subquery.
Try the following
$recentPostQuery = Post::select('created_at')
->whereColumn('firebase_id', 'users.firebase_id')
->latest()
->limit(1)
->getQuery();
User::select('users.*')
->selectSub($recentPostQuery, 'recent_post_date')
->whereHas('posts', function ($q) {
$q->withCount('comments');
})
->with(['posts' => function ($q) {
$q->withCount('comments')
->orderBy('created_at', 'ASC');
}])
->orderBy('recent_post_date', 'DESC')
->get();
You can do with QueryBuilder and a join:
$users = DB::table('users')
->join('posts.firebase_id ', '=', 'users.firebase_id ')
->orderBy('posts.recent_post_date', 'DESC')
->get();
UPDATE
You asked for Eloquent. You can do it with whereHas.
$users = User::whereHas('posts', function (Builder $query) {
$query->orderBy('posts.recent_post_date', 'DESC');
})->get();

How to use leftJoin in Laravel for case?

I have the following code:
$registrations = User::where(function ($query) use ($request) {
if (!is_null($request->region) && $request->region !== '0') {
$query->where('region', $request->region);
}
})->get();
How to join User result with another model Clients with Left Join condition?
I tried this way:
$registrations = User->join('clients', 'clients.id', '=', 'user.id')->where(...
Read the documentation first: https://laravel.com/docs/5.5/queries#joins.
$registrations = User::where(....)
->join('clients', 'clients.id', '=', 'user.id')
->get();
You could catch it by reading docs
$users = DB::table('users')
->leftJoin('clients', 'users.id', '=', 'clients.user_id')
->where('','')
->get();

Laravel: How to write two query in one query and get result from database?

I have sample code for get all trashed messages current user:
$query1 = $inbox->where('recipient', $user_id)
->where('trashed_in_recipient', 1)
->where('show_in_recipient', '!=', 1)
->orderBy('created_at', 'desc')
->paginate($paginate);
$query2 = $inbox->where('sender', $user_id)
->where('trashed_in_sender', 1)
->where('show_in_sender', '!=', 1)
->orderBy('created_at', 'desc')
->paginate($paginate);
How can be writed two query in one and get finally result?
Table structure:
You can use functions to define WHERE groups:
$query = $inbox->where(function($query) use ($user_id) {
$query->where('recipient', $user_id);
$query->where('trashed_in_recipient', 1);
$query->where('show_in_recipient', '!=', 1);
})->orWhere(function($query) use ($user_id) {
$query->where('sender', $user_id);
$query->where('trashed_in_sender', 1);
$query->where('show_in_sender', '!=', 1);
})->orderBy('created_at', 'desc')
->paginate($paginate);
I hope this helps you

Laravel Eloquent, return rows where both conditions are True

I have a table of Weeks that is joined to a property table, Weeks table looking like this:-
PropID, WeekDate, Available
1 , 2015-07-04, Yes
1 , 2015-07-11, Yes
1 , 2015-07-18, No
2 , 2015-07-04, Yes
2 , 2015-07-11, No
2 , 2015-07-18, No
I want to select properties where both the weeks of the 4th and 11th are available. In the example above, I want to return two rows with PropID 1 as both are available and no rows from PropID 2 as only one of the weeks are available.
I've tried various ways, but either get nothing or always return the 1st, 2nd and 4th rows.
I think this is close, but it's still missing something as it is looking for dates that are <= AND >=
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where(function($sub)
{
$sub->where('WeekDate', '>=', '2015-07-04');
$sub->where('WeekDate', '<=', '2015-07-11');
});
$q->where('Available', '=', 'Yes');
})
->get();
Not sure this helps, but the Property table is simply
PropID, PropName
1 , Property 1
2 , Property 2
Just found that this SQL works.
SELECT PropID FROM tblweeks WHERE WeekDate IN ('2015-07-04', '2015-07-11') AND Available = 'yes' GROUP BY PropID HAVING COUNT(*) = 2
This will give your result as Property 1 only:
$weeks = Property::whereHas('Week', function ($q) {
$q->where(function ($sub) {
$sub->whereIn('WeekDate', array('2015-07-04', '2015-07-11'));
$sub->where('Available', '=', 'y');
});
$q->groupBy('property_id');
$q->having('count(*)', '=', '2');
})->get();
There are some changes i have did in your query :
Please check this solution
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '=', '2015-07-04');
$q->orWhere('WeekDate', '=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
This should work and will return desire output
There are some changes, the query might be using group by query :
Please check this solution
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '=', '2015-07-04');
$q->orWhere('WeekDate', '=', '2015-07-11');
$q->where('Available', '=', 'Yes');
$q->group('WeekDate');
})
->get();
This should work and will return desire output
You actually need two whereHas for this:
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '>=', '2015-07-04');
$q->where('Available', '=', 'Yes');
})
->whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '<=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
I believe you do not need the second nested query.
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '>=', '2015-07-04');
$q->where('WeekDate', '<=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
Updated
Have you looked into whereBetween.
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->whereBetween('WeekDate', '2015-07-04', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();

Categories