I have two query result set for $users and $logged_action and merged both.If I use all() displayed all value.I want to paginate.If I include paginate(10) instead of all.I caught a error
BadMethodCallException in Macroable.php line 74: Method paginate does
not exist.
$users = DB::table('request')->select('asset_request.*')->get();
$logged_action= DB::table('status_tracker')->select('status_tracker.*')->get();
$users = $users->merge($logged_action);
$users=$users->all();
return view('layouts.approval_view',['users'=>$users]);
Maybe you can join the 2 tables by the same key that merge works and then paginate the result?
Something like that:
$users = DB::table('request')->
join('status_tracker','on' .... )->select ('asset_request.*', ....)->paginate (10);
also did not tested but you can try this:
$users = DB::table('request')->select('asset_request.*')->paginate(10);
$logged_action= DB::table('status_tracker')->select('status_tracker.*')->paginate(10);
$users = $users->merge($logged_action);
$users=$users->all();
return view('layouts.approval_view',['users'=>$users]);
Hope it helps.
Related
After adding Paginate function I'm getting this error
$sub_categories = SubCategory::where('id', $id)->first();
$products = Products::where('subcategory_id', $sub_categories->id)->get()->paginate(10);
$sub_categories = SubCategory::where('id', $id)->first(), this will only give you null or one record, not a collection, so you can not use ->paginate(10); chain it. You will only get one record at the most, why you need to paginate?
Update
so first for the sub_categories you do not need to paginate, as you just want one record. so the code should be like this.
$sub_categories = SubCategory::where('id', $id)->first();
second, if you want to paginate $products you should do this,
if ($sub_categories)
{
$products = Products::where('subcategory_id', $sub_categories->id)->paginate(10);
}
I'm retrieving the rows from user table and I'm using below code.
<?php $user = User::get(); ?>
I want to add array limit for $user data. I don't want to use paginate();. To add limit I'm using below code but it's not working
$users = array_slice($users, 0,2);
But it's showing below error message
exception 'ErrorException' with message 'array_slice() expects
parameter 1 to be array, object given' in........
How to I add limit to $user?
<?php
$user = User::get();
$user = $user ->limit(10);
?>
try limit or
<?php
$user = User::get();
$user = $user ->take(10);
?>
In recent Laravel versions you can also use:
User::limit(10)->offset(0)->get();
Note that User model must extend Eloquent.
Do you mean to use:
$users = array_slice($user, 0,2);
You are getting the error because $users is a collection, not an array.
You could use the take method
$users = User::get()->take(3);
or the slice method
$users = User::get()->slice(3);
Here are some ways you can achieve this.
Applying the offset and limit directly to the query builder allows you to fetch only the needed rows.
$users = User::skip(5)->take(10)->get();
$users = User::offset(5)->limit(10)->get();
If you insist on working with complete result, then this approach is what to you need. You need to use collection slice method not array_slice since the result is a collection.
$users = User::all();
// Collection slice(offset, limit)
$users = $users->slice(5, 10);
I've some problem i can't understand:
I have many-to-many relationship model, if i use ::find(x) it's working alright, but if i use ::where() i get
Undefined property: Illuminate\Database\Eloquent\Collection::$dlists
Here's my code:
$employee = Employee::whereUsername('xyz')->get();
$lists = $employee->dlists;
returns the error.
$employee = Employee::find(1);
$lists = $employee->dlists;
returns the needed output.
what am i mising?
It's because the query returns a Eloquent Collection, if the usernames are unique you can call whereUsername('xyz')->first() then you get an Employee Object where you will have access to dlists.
I have a Poll table, a Students table, and a pivot table between them that includes a token and their three votes.
public function students()
{
return $this->belongsToMany('Student', 'polls_students')->withPivot('token','first','second','third');
}
While working out saving the poll results, I came across some odd behavior that I don't quite understand. I'm hoping somebody can explain what it is I'm missing:
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students()->where('students.id', '=', Input::get('student_id'))->get() as $student){
var_dump($student->pivot->token);
}
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->get();
var_dump($student->pivot->token);
In the above code, the foreach loop will successfully display the token, where the second one throws the exception Undefined property: Illuminate\Database\Eloquent\Collection::$pivot
What am I missing? Are these two calls not logically creating the same object? How is 'pivot' working on the first and not the latter?
You first example:
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students()->where('students.id', '=', Input::get('student_id'))->get() as $student){
var_dump($student->pivot->token);
}
Here $poll->students() retrieves a collection and because of foreach loop you get a single object in your $student variable and you can use $student->pivot->token
You second example:
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->get();
var_dump($student->pivot->token);
Here you are doing same thing, using $poll->students() you are getting a collection but this time you are not using a loop and trying to do same thing using $student->pivot->token but it's not working because you didn't define any index from which you want to get the pivot->token, if you try something like this
$student->first()->pivot->token
Or maybe
$student->get(1)->pivot->token
Or maybe you can use first() instead of get() like this
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->first();
Then you can use
$student->pivot->token
Remember that, get() returns a collection even if there is only one record/model.
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students as $student){
var_dump($student->pivot->where('student_id',$student->id)->where('poll_id',$poll->id)->first()->token);
}
It seems like Laravel 4 cannot get an relation when I use the ::where() syntax. So this is what I have:
<?php
// works fine
$questions = Auth::user()->questions;
// error
$questions = User::where('profilename', '=', $username)->questions;
// error
$questions = User::where('profilename', '=', $username)->get()->questions;
?>
The first method just works fine, but the second an third not.
These give the following error:
"Undefined property: Illuminate\Database\Eloquent\Builder::$questions "
Any idea on how I can make this work? Thanks.
Try:
$questions = User::where('profilename', '=', $username)->first()->questions;
Relationships only works from a model and not an array of models which you have when you use get()