$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.
Related
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
Hello guys,
$filterArray = explode("_", $filters);
$data['articles'] = \DB::table('products')->join('product_category', function ($q) {
$q->on('product_category.product_id', '=', 'products.id');
})->where('product_category.category_id', '=', $id)
->select('products.*')
->whereBetween('price_retail_1', array($priceFrom, $priceTo))
->whereHas('filters', function ($query, $filterArray) {
$query->whereIn('filter_id', $filterArray);
})
->orderBy('products.' . $sort, $sortOrder)
->get();
}
I have the following query and I'm having some issues on the whereHas method. I'm getting an error
Unknown column 'has' in 'where clause
most likely because the $filterArray variable is out of scope for the function ( or at least that is what I am guessing. Any help on how to solve the issue is appreciated.
You cannot use whereHas method in the Query Builder context. The whereHas method is only for Eloquent Query Builder that is comming from the Eloquent models and their relationships.
What you can do is to use joins. So you can try like this:
$filterArray = explode("_", $filters);
$data['articles'] = \DB::table('products')->join('product_category', function ($q) {
$q->on('product_category.product_id', '=', 'products.id');
})->where('product_category.category_id', '=', $id)
->select('products.*')
->whereBetween('price_retail_1', array($priceFrom, $priceTo))
->join('filters', 'products.filter_id', '=', 'filters.filter_id')
->whereIn('filter_id', $filterArray);
->orderBy('products.' . $sort, $sortOrder)
->get();
I don't know how you connecting these two tables so here is only the example data:
->join('filters', 'products.filter_id', '=', 'filters.filter_id')
I have a table called instructor_class: user_id, class_id and I have another table classes: id, time, active.
I would like to show classes for a single user but only those classes that active is 0 or 1.
My current code looks like this:
return InstructorClass::with('classes.session')->where('user_id', '=', $userId)->get();
This code is displaying me everything, then I tried the following code:
$active = 1;
return InstructorClass::with(['classes' => function ($q) use ($active) {
$q->where('active', '=', $active); // '=' is optional
}])
->where('user_id', '=', $userId)
->get();
This again returns me same records, but of course the class property is null for each record, which at some point looks correct, but my point is if the 'active' field does not corresponds at the classes table do not show the record, seems like the where() stm within with() is optional..
I am kinda stuck here...
Would appreciate your help, opinions!
You can use ::has('classes') to only return the models that have related classes
return InstructorClass::has('classes')->with(['classes' => function ($q) use ($active) {
$q->where('active', $active);
}])
->where('user_id', '=', $userId)
->get();
Never thought it could be this simple:
return InstructorClass::with('classes.session')
->join('classes', 'classes.id', '=', 'instructor_class.class_id')
->where('classes.active', '=', 1)
->where('user_id', '=', $userId)
->get();
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")