Function or Variable in Where Statement? - php

Is there a way to put a function or variable in the first value of a where statement?
For example,
$equipments=DB::table('equipments')
->leftjoin('orders', function ($join) {
$join->on('orders.equipment_id', '=', 'equipments.equipmentID')
->where('orders.orderUser_id','=', Auth::user()->id);
})
->leftjoin('business_users', 'business_users.id', '=' , 'equipments.user_id')
->where('equipments.equipmentArchived','=','0')
*->where($this->countOrders('equipments.equipmentID'),'<','1')*
->orderBy('equipments.equipmentListedDate','DESC')
->distinct()
->paginate(10);
The count orders function is
public static function countOrders($id)
{
//
$members=DB::table('orders')
->leftjoin('users', 'users.id', '=', 'orders.orderUser_id')
->leftjoin('equipments', 'equipments.equipmentID', '=', 'orders.equipment_id')
->where('orders.equipment_id','=', $id)
->where('orders.status','=','ACCEPTED')
->count();
return $members;
}

You can use "whereRaw" method to do that.
$equipments = DB::table('equipments')
->leftjoin('orders', function ($join) {
$join->on('orders.equipment_id', '=', 'equipments.equipmentID')
->where('orders.orderUser_id','=', Auth::user()->id);
})
->leftjoin('business_users', 'business_users.id', '=' , 'equipments.user_id')
->where('equipments.equipmentArchived','=','0')
->whereRaw('? < ?', [$this->countOrders('equipments.equipmentID'), 1])
->orderBy('equipments.equipmentListedDate','DESC')
->distinct()
->paginate(10);

Related

Laravel query id based array

I have such results
Bur when I try to get results of different table based on this data (screenshot) ids it says
Property [id] does not exist on this collection instance
Code
$result = DB::table('zone_regions')
->where('zone_regions.id', '=', $request->input('zone'))
->join('areas', function ($join) use($area) {
$join->on('zone_regions.id', '=', 'areas.zone_id')
->where('areas.id', '=', $area);
})
->join('hthree_regions', function ($join) use($city) {
$join->on('areas.id', '=', 'hthree_regions.area_id')
->where('hthree_regions.id', '=', $city);
})
->join('segments', function ($join) use($segment) {
$join->on('hthree_regions.id', '=', 'segments.hthree_id')
->where('segments.id', '=', $segment);
})
->join('links', function ($join) use($link) {
$join->on('segments.id', '=', 'links.segment_id')
->where('links.id', '=', $link);
})
->join('titik_closurs', function ($join) use($closure) {
$join->on('links.id', '=', 'titik_closurs.link_id');
})
->groupBy('titik_closurs.id')
->get();
$histories = DB::table('core_histories')->whereIn('titik_id', $result->id)->get(); //get histories of returned results (return error above)
$res = array_merge($result, ['histories' => $histories]);
Any idea?
Try this
$histories = DB::table('core_histories')->whereIn('titik_id', $result->pluck('id')->toArray())->get();
https://laravel.com/docs/7.x/collections#method-pluck

Laravel - Pass a variable with Query Builder function

Using Laravel's query builder example is it possible to pass a variable to this function:
$someVariable = 1;
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) use ($someVariable) {
$query->where('votes', '>', $someVariable)
->where('title', '<>', 'Admin');
})
->get();
It seems the function cannot access the variable outside of itself. I get an error: Undefined variable: $someVariable
You'll need to use the "use" keyword after your function, for variables outside of that function. If $someVariable is the one you want to use, this should work.
$someVariable = 1;
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) use($someVariable) {
$query->where('votes', '>', $someVariable)->where('title', '<>', 'Admin');
})->get();
use Superglobals
$GLOBALS["someVariable"] = 1;
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', $GLOBALS["someVariable"])
->where('title', '<>', 'Admin');
})
->get();

Laravel eloquent query building using OR

This eloquent query filter:
return $this->games()
->where(function ($query) {
$query->where('active_player_id', '=', $this->id)
->where('stage_name', '<>', 'setup');
})
->orWhere(function ($query) {
$query->where('active_player_id', '<>', $this->id)
->where('stage_name', '=', 'setup');
});
Builds into SQL like this:
where `games_players`.`player_id` = '1'
and (`active_player_id` = '1' and `stage_name` <> 'setup')
or (`active_player_id` <> '1' and `stage_name` = 'setup')
How do I change the eloquent code to build this query (brackets around the OR):
where `games_players`.`player_id` = '1'
and (
(`active_player_id` = '1' and `stage_name` <> 'setup')
or (`active_player_id` <> '1' and `stage_name` = 'setup')
)
You can achieve that by doing this:
->where('active_player_id',1)
->where(function($q){
$q->where([ ['active_player_id', 1],['stage_name','!=', 'setup'] ])
->orWhere([ ['active_player_id','!=', 1],['stage_name', 'setup'] ]
})->get()
I don't have an instance of Eloquent to hand to test against just now but would it now be something like this?
return $this->games()
->where(function ($query) {
$query->where(function ($query) {
$query->where('active_player_id', '=', $this->id)
->where('stage_name', '<>', 'setup');
})
->orWhere(function ($query) {
$query->where('active_player_id', '<>', $this->id)
->where('stage_name', '=', 'setup');
})
})
;
That is, you put the conditions to be grouped together into an anonymous function on where?
Try moving your orWhere condition into the first anonymous function like this,
return $this->games()
->where(function ($query) {
$query->where('active_player_id', '=', $this->id)
->where('stage_name', '<>', 'setup')
->orWhere(function ($query) {
$query->where('active_player_id', '<>', $this->id)
->where('stage_name', '=', 'setup');
});
})

Laravel function with combination in pivot table

Hello I have problem with my requests. How can I make that functions wont't return data which exist in second one. ex. user_id = 1 friend_id = 2 from sendedRequests and user_id = 2, friend_id = 1 from pendingRequests.
Here's the code :
public function showSendedRequests(){
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where('who_send', $this->id)
->where('user_id', $this->id)
->where('accepted',false)
->get();
return $request;
}
public function showPendingRequests(){
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',false],['who_send','!=',Auth::user()->id]])
->get();
return $request;
}
You can do something like this, using the <> not equal
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where('who_send', $this->id)
->where('user_id','<>', $this->id)
->where('accepted',false)
->get();
return $request;
}
//for the next function
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',false],['who_send','<>',Auth::user()->id]])
->get();
return $request;
}
For further reference

ERROR : Call to undefined method Illuminate\Database\Query\JoinClause::whereIn()

I have a method which returns a list of provinces based on some join and where clauses. The problem I have is That laravel throws an Exception that tells there is an undefined method called whereIn. But there exist whereIn method in Eloquent. How should I use whereIn method on joins?
public static function getProvinces($IDs = array()){
$query =
DB::Table('doctors')
->join('users', function($join){
$join->on('users.id', '=', 'doctors.user_id')
->whereIn('users.status_id', array(3,4,7));
})
->join('contacts', function($join){
$join->on('doctors.id', '=', 'contacts.doctor_id')
->where('contacts.type_id', '=', 1);
})
->join('provinces', 'contacts.province_id', '=', 'provinces.id')
->select('contacts.province_id as id','provinces.name as name',DB::raw("COUNT('contacts.province_id') as count"))
->groupBy('contacts.province_id');
if(!empty($IDs))
$query->whereIn('doctors.id', $IDs);
return $query->get();
}
Thanks for your help.
Currently, JoinClause doesn't support whereIn method, but you can overwrite it following way:
$query =
DB::Table('doctors')
->join('users', function($join){
$join->on('users.id', '=', 'doctors.user_id')
->where('users.status_id', '=', 3)
->orWhere('users.status_id', '=', 4)
->orWhere('users.status_id', '=', 5);
})
->join('contacts', function($join){
$join->on('doctors.id', '=', 'contacts.doctor_id')
->where('contacts.type_id', '=', 1);
})
->join('provinces', 'contacts.province_id', '=', 'provinces.id')
->select('contacts.province_id as id','provinces.name as name',DB::raw("COUNT('contacts.province_id') as count"))
->groupBy('contacts.province_id');

Categories