orderBy-querybuilder and pagination in laravel 5 - laravel

I'm trying to make an orderBy in my query and a pagination for the items from a gallery, but instead of sort the complete query, laravel order only page-by-page content, so if I order desc it only works 1 page, when I go to the next page I can find values really different (ordered, but without relationship with the first page).
Here is my query:
$product = DB::table("product")
->leftJoin("provider","product.provider_id","=","provider.id")
->orderBy("daily_sales","DESC")
->select('product.*','provider.comercial_name')
->paginate(6);

You can try:
$product = DB::table("product")
->leftJoin("provider","product.provider_id","=","provider.id")
->select('product.*','provider.comercial_name')
->orderBy("daily_sales","DESC")
->paginate(6);

Related

How to get list of ids from pagination query of the current page?

I have 1 pagination query for every:
$pagination_query = Table1::...->paginate(100);
then I have a second query that needs to use the list of ids from the pagination query:
$second_query = Table2::whereIn('id', $list_of_ids_from_pagination_query);
How can I get the list of ids only from the paginated page so that I get 100 ids and not thousands of ids which will end up in too many bound variables error?
Ty!
You can use something like this .
$ids = Table1::...->paginate(100)->pluck('id');
$second_query = Table2::wherein('id', $ids);

How to split the result of Laravel paginator

I am using the Laravel paginator on my site. I want to split the query result into 4 parts. Because my design has 2 rows of 3 items each and another 2 rows of 2 items each. So I can't use a single foreach loop for showing the items. In my controller, I have the below code.
$properties = Property::whereHas('createdBy', function ($query) {
$query->where('enabled','=',true);
})->orderBy('created_at', 'desc')->paginate(10);
And in my view, I am trying to split the result. I have tried $properties->take(3) and $properties->chunk(3). But is not working.
Below is the content of paginator.
Please help.

Eloquent: Using Model::where + with not displaying data

Im trying to create a query using Products Model (refers to Product Table) with Photo Model / Table, Features and locations.
My idea is to filter results of table products in order to display only the information of the current logged user.
This is the query:
$product = Product::where('companyId', '=', Auth::user()->companyId)
->with(['photos','features','locs']);
For some reason I cannot find why when I include
Product::where('companyId', '=', Auth::user()->companyId)
the query stop working.
its possible to use Products:where(condition) + wish?
thanks in advance.
try to use this one ...
Product::where('companyId', Auth::user()->companyId)->get();

Eloquent ORM select all with limit and column name

Hi how can I select limited number of all the item from model and the selection with certain names.
eg:
$product = Product::all()->limit(4)->select('id','name');
Most of example start with Product::find(1) but my case, I don't have id. Thanks
Use the other methods first, then call get at the end:
$products = Product::limit(4)->select('id', 'name')->get();
Limit Query Results
To limit the number of results returned from the query, or to skip a given number of results in the query, you may use the skip() and take() methods:
Example of User model which extends Eloquent:
$users = User::take(5)->skip(10)->get();
Product::all() will be used when get all data without any condition. You need to use get method.
you need to
$products = Product::select('id', 'name')->limit(4)->get();

Filtering resultset with codeigniter and datamapper

I got a little problem with datamapper, and I'd like to know if there is a quick solution.
Let's say I have this kind of data
Groups table
id | Name
1 | admin
2 | guest
3 | editor
4 | moderator
In my base controller I set a global field to see only the groups that are not admin
$this->groups_ = new Group();
$this->groups_->where('id >', 1)->get();
//so I can select the users that are not admin
$users = new User();
$users->where_related('group',$id,$this->groups_)->get();
Now in my controllers I'd like to filter the groups. For example I want to select only editors and guests (id between 1 and 4). So I would like to filter the initial result set... something like this
$this->groups_->where('id <',4)->get();
But it doesn't work. It returns ALL the group ids < 4 including admin.
What would be the right way to get this?
Well, datamapper does not query objects or groups of objects. It queries the database. So when you do the second get(), you're executing a separate query on the database.
You can do this, but it will be an additional query:
$this->groups_->where('id >', 1)->where('id <', 4)->get();
or you can loop through $this->groups_ after your first query in php and filter the set there and save as an array.
A possible solution
Maybe I found a possible workaround by cloning the datamapper object.
What I want to do is not to query the db multiple times, but I'd like to have a base resultset and refine the results.
So in my base controller I would do something like
$this->groups_ = new Group();
$this->groups_->where('id >', 1);//without getting the results
//so I can select the users that are not admin
$users = new User();
//here i'm getting the results from the clone
$users->where_related('group',$id,$this->groups_->get_clone()->get())->get();
And, as I left the object "open", without getting the results, I can use it in my controllers, as a base for my other "queries"... ok,they are actually new conditions of the first query.
//now the query return the groups between 2, as I set in the base controller
//and 4, set in the child controller
$this->groups_->where('id <',4)->get();

Resources