I am trying to build little complex query where everything wan OK until I tried to use a advanced where condition.
In the code below I am getting an error saying
ErrorException: Undefined variable: user in file D:...**\app\Http\Controllers\ApiAuthController.php on line 285
and the line 285 is $query->where("tbl_transactions.from_user_id", "=", $user->id) inside the orWhere()
public function loadTransactions()
{
$token = JWTAuth::getToken();
$user = JWTAuth::toUser($token);
return DB::table('tbl_transactions')
->join('tbl_users as fromu', 'fromu.id', '=', 'tbl_transactions.from_user_id')
->join('tbl_users as tou', 'tou.id', '=', 'tbl_transactions.to_user_id')
->select('tbl_transactions.id', 'tbl_transactions.from_user_id', 'tbl_transactions.to_user_id', 'tbl_transactions.amount', 'tbl_transactions.created_at', 'tbl_transactions.status', 'fromu.userid', 'tou.userid', 'tou.phone as tophone', 'fromu.phone as fromphone')
->orWhere(function ($query) {
$query->where("tbl_transactions.from_user_id", "=", $user->id)// here the problem is (undefined **$user**)
->where('tbl_transactions.to_user_id', '=', $user->id);
})
->orderBy('tbl_transactions.id', 'desc')
->take(50)
->get();
}
I am new to laravel.Anyone here could help me would be great.
You need to use the use clause to make the variable accessible in the closure scope. So, change this:
->orWhere(function ($query) {
To this:
->orWhere(function ($query) use($user) {
Im too lazy to link the other 100s of posts that are duplcates of this.
PHP manual - anonymous functions Example #3
Related
I need your help. I try to do this query:
public function future_trips($cat_id, $city_id)
{
$trips=Events::where('start_date','>',Carbon::now()->subDay(5))
->where(function ($q)
{
$q->where('category_id',$cat_id)->orWhere('city', $city_id);
})
->get();
}
ErrorException: Undefined variable: cat_id in file C:\x......
Undefined variable $cat_id less... (Ctrl+F1) The inspection can
produce two types of warnings: The definition of the variable can not
be found anywhere. ("Undefined variable") There are one or more paths
to reach the line with the variable usage without defining it.
("Variable might have not been defined")
Can somebody explain me why ?
You need to add these variables to the function closure with use. Php does not automatically include variables outside the closure.
public function future_trips($cat_id, $city_id)
{
$trips = Events::where('start_date', '>', Carbon::now()->subDay(5))
->where(function ($q) use ($cat_id, $city_id)
{
$q->where('category_id',$cat_id)->orWhere('city', $city_id);
})
->get();
}
$page = Question::paginate(10);
dd($page);
Here the pagination working very well but when i use pagination with other models relations then its produce the paginate result but pagination links not appear because its produce error
$questions = Course::with(['questions' => function($query){
$query->paginate(10);
},'questions.subjects','questions.years'])
->where("status",1)
->where(function ($query) use ($course) {
$query->orWhere('course', '=', $course)
->orWhere('slug', '=', $course);
})->get();
ERROR :
BadMethodCallException in Macroable.php line 81:
Method render does not exist.
What is missing here.
Paginate should be used at the end of a query, and not in relations:
$questions = Course::with(['questions','questions.subjects','questions.years'])
->where("status",1)
->where(function ($query) use ($course) {
$query->orWhere('course', '=', $course)
->orWhere('slug', '=', $course);
})->paginate(10);
Since you are not paginating the main result set, you get the Method render does not exist error.
In my controller I have wrote this following code
$usersCount = User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->count();
$locations_array_result = explode(",",$locations_array_result);
foreach ($locations_array_result as $param)
{
$usersCount = $usersCount->whereHas('location', function($q) use($param){
$q->where('location_id', '=', $param );
});
}
This code giving following error
Call to a member function whereHas() on a non-object
Can anyone help me to find out what i have done wrong!!!
$usersCount is already a number from the 1st line of your sample.
You want instead to replace $usersCount->whereHas with User::whereHas in your foreach loop.
Taking a very wild guess here, I would think you need to get all users with these requirements
->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)
plus having a location_id value which exists on an array named $locations_array_result
If this is the case, this is all you need:
User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->whereIn('location_id', $locations_array_result)->get();
EDIT
Following your comment below, I assume user has many to many relation with locations (defined in your model), so eager loading and then using a condition with a callback should do the job:
$users = User::where('activated', '=', 1)
->where('group_id', '=', 1)
->where('availability_date', '<=', $opportunity_date)
->with(array('location' => function($query) use ($locations_array_result)
{
$query->whereIn('location_id', $locations_array_result);
}))->get();
I am new to laravel 4 and keep getting the same error trying to learn about some methods in DB class.
Call to undefined method Illuminate\Database\Query\Builder
I get same erros trying to use "->or_where", "->order_by".
another problem is parsing the dynamic methods:
->where_name("test")
turns into
`users` where `_name` = test)
but if i try to do
->wherename("test")
then everything is fine.
You're using an incorrect syntax for orWhere and orderBy.
This is the correct syntax for orWhere:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
And this for orderBy:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
Query Builder - Advanced Wheres - Laravel
All methods in Laravel 3 have changed from snake case
->where_name("test")
to camel case in Laravel 4
->whereName("test")
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.