Laravel pluck with paginate - laravel

So, I've a ERM with two database connected in a same laravel app. I'm trying to pull up all the IDs with pluck which is working fine but not being able to paginate. For an instance,
$newsTagSaved = NewsTag::latest()->pluck('post_id');
This works just as fine. But this pulls up way more information than I need. I'm looking for something like:
->pluck('id')->paginate(12);
This way I'm not loading data in ajax that I don't need since it will query unnecessary data on every ajax call.
Thank you!

Related

Is there a way to convert pagintation to collection/array/json Laravel

Is there a way to convert a pagination to a collection/array to get all items like:
Users::where(...)->paginate(15); //paginated
Output: 15 per page
Users::where(..)->get(); //not paginated
Output: all Users per page
So i is it possible to do something like that:
Users::where(...)->paginate(15)->removePagination();
Wanted output: all Users per page
You have 2 choices:
either retrieve the complete query result by chaining ->get()onto your query.
call ->paginate() to retrieve a pagination instance, which automatically retrieves the first page, and it automatically links the example.com?page=1 url param.
(there are also some other functions that complete the query like ->pluck() or ->value() which might not be applicable to you)
As far as I'm aware you cannot revert to the complete collection instance when you have the pagination retrieved (\Illuminate\Contracts\Pagination\LengthAwarePaginator), nor does it make sense to do so since the idea is to retrieve as little data as possible such that your application stays fast.
Maybe you should explain what you're trying to do; for instance you can retrieve the total count in the paginator using SomeModel::query()->paginate()->total(), among other things. You might not need the complete query after all.

what is difference between model->name and model() in laravel?

I want to know the difference between laravel eloquent conisgnment->runsheet->name and $consignment->runsheet()->name
i notice that there is performance difference but why? also I notice that when I try to get relationship property like $consignment->runsheet->name it work fine but $consignment->runsheet()->name doesn't work
The first one
$consignment->runsheet
retrieve a collection (so it's already made a query to DB)
while the second
$consignment->runsheet()
is a query builder (hasn't made a database query) you can chain it like any query builder instance
so if you want to get a property from the second one you can do it like:
$consignment->runsheet()->first()->name;
OR
$consignment->runsheet()->value('name');

Will laravel reconnect to the database for any where on returned model collection?

If we have a find like this:
$returnedModel = Flight::
with("passengers")
->find(1);
Now if I add a where on returnedModel like this:
$returnedModel->passengers()->where("name" , "hamid")->first();
Will Laravel reconnect to the database for any where on returned model collection?
I added DB::getQueryLog() and there is a query with this where! how can I get this without reconnect to database?
You need to access your passengers as a property, not a method, by the way you get a Laravel collection already loaded from the DB (thanks to the with eager-loading).
$returnedModel->passengers->firstWhere('name', 'hamid');

How to improve Angular2 rendering performances when accessing a backend server?

I have a service which is responsible for getting the data from a backend.
The data is shown via a component in its nginit method. It is shown in a table (html table).
I would like to be able to manipulate data without having to reload it each time in my table : ex : delete a row, add a row ... I want all of this very fluid. The data is coming from a database.
So my question is :
Should my service return an observable or an array ?
Is nginit the best place to show my table ?
How should I do things maybe to manage everything in memory? I do not know.
Thanks for any advice.
I would save the data into an array then use lodash to manipulate it.
You could edit, delete, and add rows using lodash's built in functions. When you want to save the data to the backend you would then post the array back to the server. This will keep backend requests to a minimum which will help your performance.
For example you could delete elements from the array using the remove function, https://lodash.com/docs/4.17.4#remove
Lodash can be setup like this https://medium.com/#gtsopour/importing-lodash-into-angular-2-typescript-application-94590365f46d#.9mdcdjnac
Of course you could do this all using raw js though but I find Lodash very rich and easy to use.

Yii CGridView pagination

I'm using CArrayDataProvider (which is basically a customized query i've created) that returns all the results (over 1000) from the database.
I'm using the results in the view but when i'm using the pagination it's going back to the controller for another query.
my question is: is there any way to move on the the next set of results (already part of the result array) without going to the controller and model again.
*My controller has a fairly advanced function which requires variables and parameters which i dont have in the view when trying to use standard AJAX request for the next page.
thanks,
Danny
my question is: is there any way to move on the the next set of
results (already part of the result array) without going to the
controller and model again
Then my answer would be NO if you was using CGridview's pagination. In your situation, you have to make the pagination by yourself instead. You have already selected all of records, and would like to manipulate them on your client side, you really don't need the pagination of CGridview at all.
Pushing all of records into a page on first load is not good idea, but maybe your requirement has asked, I just say that.

Resources