How to paginate in Laravel when using Modell::find(array()) - laravel

I'm grabbing some data in Laravel using the find method and only grabbing the id's I want:
$my_ids = array(1,4,5,10);
$results = Model::find($my_ids);
However, if I try to paginate via Model::find($my_ids)->paginate(10), it throws the error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate().
How can I query to only get specific model Id's back from the database while also using pagination?

Turns out I can use this syntax:
$results = Model::whereIn('id', $my_ids)->paginate(10);

I suggest use the eloquent query builder:
$results = Model::whereIn('id', $my_ids)->paginate(10);

Related

Laravel / Lumen $users = \App\User::find$Uid)->get();

normally i use this:
$users = \App\Users::where('age', '>=', '20')->paginate(20);
But if I use raw querys or use the get(); method from querybuilder, I got an array not a collection or a paginator instance as result.
How can I get my ->links(); from that array result?
Hope all is explained well. :-)
KR,
Marcel
Write{{ $users->links() }} in your view page. According to your query, pagination will show while your data return more than 20 rows.
For better understand Read this doc

Where Clause in eloquent is responding with `No Properties`

I am new to laravel. I am trying to keep a where clause for the get method like this.
$employees = newdb::where('status', 'Active');
$response = $employees;
return response()->json($response,200);
But i am getting No Properties as output
In PHP I used this
"SELECT * FROM newdb WHERE status = 'Draft'";
What am i doing wrong? I tried different suggestions But none worked correctly. How do i do this? What is wrong with my code?
where method returns Builder object. So, you have to call get method on it to fetch data.
Try this
$employees = newdb::where('status', 'Active')->get();
return response()->json($employees, 200);

Laravel Lumen - Eloquent Query Log

I'm using Laravel Lumen to build an API.
I've come to a point where I need to find out what SQL query is being generated by Eloquent. I know how to do this in Laravel 4 and Laravel 5 but i've tried the same code in Lumen and the query is blank?
$queries = DB::getQueryLog();
$last_query = end($queries);
echo 'Query<pre>';
print_r($last_query);
exit;
The above code, when run in Laravel works fine - in Lumen the query is blank?
To get the query log in Laravel Lumen working you need to enable it:
DB::connection()->enableQueryLog();
You can add that code in to your controller, middleware, etc then use:
$queries = DB::getQueryLog();
$lastQuery = end($queries);
dd($lastQuery)
To print your query.
You can also use the following with eloquent:
$myModel = Users::where('active', true);
dd($myModel->getSql(), $myModel->getBindings());
You must run the getSql() and getBindings() before you call ->first() or ->get(), etc
Just call this after the query to keep it quick and simple:
echo $query->toSql();

Laravel: Call to undefined method save()

I want to update all my records where the name is like the one im referencing it.
However im getting an error saying Call to undefined method save()
Here's my code
$section = Section1::where('name', 'like', 'ss123')->get();
$section->name = Input::get('name');
Please help :(
$section = Section1::where('name', 'like', 'ss123')->get() returns a collection (think of an array).
Therefore you should loop through the collection using foreach and apply the save to each object in the collection, as such:
foreach($section as $s)
{
$s->name = Input::get('name');
$s->update();
}
Then again this ain't very DB-friendly.
Therefore, the most appropriate action in your case will be a mass update:
Section1::where('name','like','ss1213')->update(['name'=>Input::get('name')]);
try dd($section) and find the available method to prevent call the undefined method.
I assume you are using $section->save();
if you want to update,
use $section->touch();
or $section->update(array('name' => Input::get'name'));

Laravel Pagination with appends() Error

I am new to Laravel and am using version 4.1.
I am attempting to query database using pagination and then run the results through the appends() function to add additional parameters to my URL.
Here is the code I am using
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
And that runs as desired. Now when I attempt to create the pagination list (Bootstrap default) and run the following code I get an error.
$pagination = $results->appends(array('key' => 'value'))->links();
This is the error I receive.
Call to a member function appends() on a non-object
I know I'm doing something wrong, I just can't figure out what...
Thanks in advance,
SC
I'm not familiar with the appends function, but you do have an error I can see. Try changing
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
To...
$query = DB::table('tableName');
if(Input::has('someinput')) {
$query->where('someinput', Input::get('someinput'));
}
if(Input::has('otherinput')) {
$query->where('otherinput', Input::get('otherinput'));
}
$results = $query->paginate(50);
Once you run paginate(), an instance of Paginator is returned. I don't see a get() method for Paginator though so I'm not sure how you weren't getting an error there.

Resources