Undefined variable inside laravel left join - php

in my case, i want to display users and orders completed by each user, orders have status
i get $status from input form
here is my code
$orders = DB::table('user_profiles')
->leftJoin('orders', function($join){
$join->on('user_profiles.id','=','orders.id_user')
->where('orders.status','=',$status);
})
->selectRaw('user_profiles.*, count(orders.id_user) as order_try_count')
->groupBy('user_profiles.id')
->orderBy('order_try_count',$order)
->paginate(15);
but i get undefined variable status, what should i do to solve this problem ?, thank you

Yoo need to pass variables into closure using use construction like so:
->leftJoin('orders', function($join) use ($status) {
instead of
->leftJoin('orders', function($join) {
Reference: anonymous functions

You have to use use() method to user variable in closure
Try
->leftJoin('orders', function($join) use($status){
$join->on('user_profiles.id','=','orders.id_user')
->where('orders.status','=',$status);
})

Related

passed variable in nested where query in laravel 9

I am trying to use variable to nested where() query in laravel 9 but i get an error that variable Undefined
my code:
public function edit($id)
{
$category = Category::findOrFail($id);
$parents = Category::
where('status' , 'active')
->where('id' , '<>' , $id)
->where(function($query){
return $query
->whereNull('parent_id')
->orWhere('parent_id', '<>', $id);
})->get();
}
the error:
Undefined variable $id
$parents = Category::
where('status' , 'active')
->where('id' , '<>' , $id)
->where(function($query) use ($id) { <-- problem is here
return $query
->whereNull('parent_id')
->orWhere('parent_id', '<>', $id);
})->get();
in this section of your code: ->where(function($query){ you need use keyword to pass value inside this inner function as below:
->where(function($query) use ($id) { .. };
the detail for this action is:
The closure is a function assigned to a variable, so you can pass it around
A closure is a separate namespace, normally, you can not access variables defined outside of this namespace. There comes the use keyword:
use allows you to access (use) the succeeding variables inside the closure.
use is early binding. That means the variable values are COPIED upon DEFINING the closure. So modifying $id inside the closure has no external effect, unless it is a pointer like an object is.
You can pass in variables as pointers like in case of &$id. This way, modifying the value of $total DOES HAVE an external effect, the original variable's value changes.
Variables defined inside the closure are not accessible from outside the closure either.
Closures and functions have the same speed. Yes, you can use them all over your scripts.
for more info read this.

Laravel Eloquent Query -> Undefined Variable

Attached is a screenshot of the error I am facing. I have the log show the value of the $team variable. What am I doing wrong?
You don't have access to parents variables from a closure, you need to explicitly send variables to the closure with use keyword
function($query) use ($team)
Use forget to use $team variable inside your closure. It must be function($query) use ($team)
$data = User::whereHas('roles' => function($query) use ($team) {
$query->where('role_code', '=', $team)
})->get();

Inner query with use variable

I am new to laravel and I am trying to retrieve data from three tables because the user enters 3 information then I use its to determine the row to check login
so I use this way but not work!
I do not know how to send variables in SQL
DB::table('members')
->join(DB::raw('(SELECT FROM members_courses_assign WHERE referenceNumber=>$coursenum,termkey=>$semester) courseA'), function($join) {
$join->on('members.externalPersonKey', '=', 'courseA.externalPersonKey');
})->join(DB::raw('(SELECT courses FROM WHERE referenceNumber=>$coursenum,termkey=>$semester) coursecc '), function($join) {
$join->on('courseA.referenceNumber', '=', 'coursecc.referenceNumber');
})
->where(['jobID' => $jobid])->get();
Try this :
->where('jobID',$jobid)->get();
Here in Laravel Documentation you can find all you need to know about query builder.
To pass variable to inner query (Anonymous functions) you have to pass it with use keyword as described in docs
$yourVariable= "";
DB::table('members')
->join(DB::raw('(SELECT FROM members_courses_assign WHERE referenceNumber=>$coursenum,termkey=>$semester) courseA'), function($join) use($yourVariable) {
$join->on('members.externalPersonKey', '=', 'courseA.externalPersonKey');
})->join(DB::raw('(SELECT courses FROM WHERE referenceNumber=>$coursenum,termkey=>$semester) coursecc '), function($join) use($yourVariable) {
$join->on('courseA.referenceNumber', '=', 'coursecc.referenceNumber');
})
->where(['jobID' => $jobid])->get();
DB::table('members')
->join('members_courses_assign', function($join) use ($coursenum, $semester) {
$join->on('members.externalPersonKey', '=', 'members_courses_assign.externalPersonKey')
->where('referenceNumber', $coursenum)
->where('termkey', $semester);
})
->join('courses', function($join) use ($coursenum, $semester) {
$join->on('members_courses_assign.referenceNumber', '=', 'courses.referenceNumber')
->where('referenceNumber', $coursenum)
->where('termkey', $semester);
})
->where('jobID', $jobid)
->get();
You can refer to Query Builder's advanced join clauses here.

Laravel inner join saying variable undefined

I am doing a search to filter through users that are displayed on my screen. The display of the users is working without the search. However when I type in the search a name, and go to display it, I get an undefined variable for search.
$Search = $request->search_code; //print_r($Search);die(); // this works
// this next line was used for testing and it works as well
//$findTrainers = DB::table('users')->where('users.name', 'like', "%$Search%")->get();
// This is what we want to work, but the search variable is now undefined
$findTrainers = DB::table('users')
->join('bios', function ($findTrainers) {
$findTrainers->on('users.id', '=', 'bios.user_id')
->where('users.name', 'like', "%$Search%");
})
->get();
This is due to how closures and scoping work. Closures don't know about variables defined outside of their scope, so you must tell a closure to use that variable when you want to access it within the scope of the closure, like this:
$findTrainers = DB::table('users')
->join('bios', function ($findTrainers) use ($Search){
$findTrainers->on('users.id', '=', 'bios.user_id')
->where('users.name', 'like', "%$Search%");
})
->get();

Using variable in laravel 4.1 Query Builder

How can use variable in laravel query builder. Here is my code.
$role = 1;
$user = DB::table('users')
->join('assigned_roles', function($join)
{
$join->on('users.id', '=', 'assigned_roles.user_id')
->where('assigned_roles.role_id', '=', $role );
})
->get();
But it return Undefined variable: role.
How can I solve this problem. Help me plz.
You need to import variables from the local scope to the anonymous function's scope:
function ($join) use ($role) {}
See the example in the docs.

Categories