multiple addOrderBy in doctrine/symfony2 - php

I wanted to order the item pictures that I have in my database based on the shop priority that has the highest priority first, then number of likes, and the created date:
So I have the following:
$picturesQuery = $qb->select('DISTINCT p')
->from("AppMainBundle:Picture", 'p')
->innerJoin('p.shop', 'shop')
->addOrderBy('shop.priority', 'DESC')
->addOrderBy('p.numberoflikes', 'DESC')
->addOrderBy('p.created', 'DESC')
;
However it doesn't work. I have a shop which has the highest priority, but the item from that shop isn't showing on the top of the query results. Is it okay to have multiple order by? How can I achieve what I want?

You need to fetch join shop Something like this (untested).
$picturesQuery = $qb->select('DISTINCT p', 'shop')
Reference

Related

Laravel OrderBy by related table column

I have a table (A) that has a One to Many relation with another table (B).
I want to query Table A and eager load Table B with the Table A results - but I also want to sort Table A by a value in Table B.
I have tried using OrderBy in the query and also trying SortBy on the resultant collection but cannot get the Table A data to be sorted by the value found in Table B.
Example of what I have tried:
$query = ModelA::with("ModelB"])->get()->sortByDesc('ModelB.sortValue');
Keep in mind, I am only interested in the LATEST record from Table B. So I need to query Table A and sort by a value in the LATEST records of Table B.
How can I achieve this?
EDIT:
The below (as suggested by #ljubadr) works pretty close, but the issue is that there are many record in Table B which means that it doesn't reliably sort as it doesn't seem to sortby the latest records in Table B. Can I have the join return ONLY the latest record for each ID?
$query = ModelA::select('TableA.*')
->join('TableB', 'TableA.id', '=', 'TableB.col_id')
->groupBy('TableA.id')->orderBy('TableB.sortCol', 'desc')
->with(['x'])
->get();
EDIT 2:
#Neku80 answer has gotten me closest but it seems to not sort the column with the greatest accuracy.. I'm sorting a Decimal column and for the most part it is in order but in some places the items are out of order..
$latestTableB = ModelB::select(['TableA_id', 'sortByColumnName'], DB::raw('MAX(created_at) as created_at'))
->groupBy('TableA_id');
$query = ModelA::select('TableA.*')
->joinSub($latestTableB, 'latest_TableB', function ($join) {
$join->on('TableA.id', '=', 'latest_TableB.TableA_id');
})
->orderBy('latest_TableB.sortByColumnName')
->get();
For example, the ordering is like:
0.0437
0.0389
0.0247 <-- -1
0.025 <-- +1
0.0127
When I delete all rows except for the 'latest' rows, then it orders correctly, so it still must be ordering with old data...
I have found a solution:
ModelA::select('TableA.*', 'TableB.sortByCol as sortByCol')
->leftJoin('TableB', function ($query) {
$query->on('TableB.TableA_id', '=', 'TableA.id')
->whereRaw('TableB.id IN (select MAX(a2.id) from TableB as a2 join TableA as u2 on u2.id = a2.TableA_id group by u2.id)');
})
->orderBy('TableB.sortByCol')
->get();
Another alternative to order is like this:
$users = User::orderBy(
Company::select('name')
->whereColumn('companies.user_id', 'users.id'),
'asc'
)->get();
Here we are ordering in asc order by company name field.
In this article it is explained in detail.
You can simply execute a left join query:
ModelA::query()->leftJoin('model_b_table', 'model_a_table.primary_key', '=', 'model_b_table.foreign_key')->orderBy('model_a_table.target_column')->get();
This should work if you only need TableB's ID and created_at columns:
$latestTableB = ModelB::select('TableA_id', DB::raw('MAX(created_at) as created_at'))
->groupBy('TableA_id');
$query = ModelA::select('TableA.*')
->joinSub($latestTableB, 'latest_TableB', function ($join) {
$join->on('TableA.id', '=', 'latest_TableB.TableA_id');
})
->orderBy('latest_TableB.created_at')
->get();

I want to find top 10 favorite trails in laravel queries

I have two tables named 'favorites' and 'trails'. I want to show top 10 favorites trails for users. I made 'many to many' relationship between them. But, I am not sure how to make the query.What should be the right query.Would someone help me for the right one. Without relationship, I tried something like this-
$favorites = DB::table('favorites')
->join('trails', 'trails.id', '=', 'favorites.trail_id')
->select('favorites.trail_id', 'trails.name')
->get();
First one is 'trails' table and another one is 'favorites' bellow -
To get the top 10 trails you need to use aggregate function to count no of users for each trail and order your results based on the result of count and then select only 10
$favorites = DB::table('trails as t')
->select('t.id', 't.name')
->join('favorites as f', 't.id', '=', 'f.trail_id')
->groupBy('t.id')
->groupBy('t.name')
->orderByRaw('COUNT(DISTINCT f.user_id) DESC')
->limit(10)
->get();

Ordering a pagination by a relationship (Laravel 5.3)

In my app I have posts. I wish to show all the posts on the homepage, but order them higher if the post's user has uploaded an image. I can determine if the post's user has uploaded an image by checking if the user has a relationship with a row in the images table, like this:
$post->user->image
My code currently looks like this:
$posts = Post::where('subject_id', '=', $subject->id)
->approved()
->orderBy('created_at', 'desc')
->paginate(18);
Currently, I am simply ordering it by created at, but ideally all posts whose related user has an image will come first, then the rest. I've been looking for a way to do this efficiently and in a way that doesn't just work on the first page.
How should I go about this?
Try this:
$posts = Post::where('subject_id', '=', $subject->id)
->approved()
->select(['*', DB::raw('(SELECT count(images.id) FROM images INNER JOIN users ON users.image_id = images.id WHERE posts.user_id = users.id) as count_images'])
->orderBy('count_images', 'desc')
->orderBy('created_at', 'desc')
->paginate(18);
Try this:
$posts = Post::where('subject_id', '=', $subject->id)
->approved()
->with('user')
->join('users', 'users.id', '=', 'post.user_id')
->select(['post.*', 'users.avatar'])
->orderBy('image', 'desc')
->orderBy('created_at', 'desc') // ->latest() can be used for readability
->paginate(18);
This code eager loads users to reduce number of DB queries and doesn't use raw queries which should be avoided, because mistake can make your application vulnerable to SQL injection.
Explanation:
We eager load relationship using ->with('user') method, then join users table, select all fields from posts table and only image field from users table, then order results by image and paginate results.
Result should look like this:
App\Post:
id
title
...
image(from users table)
App\User (eager loaded relationship)
id
name
email
...
image
created_at

Laravel joins including where there is no match

I have a table layout like so:
products
id
productname
product_weights
id
productid
weight
product_flavours
id
productid
flavour
I am trying to generate a list of all possible product variations. Some products have no variation in size, some have no flavour variation, some have both. My current thinking is using a join. I have so far for my query:
DB::table('product_weights')
->join('products', 'products.id', '=', 'product_weights.prodid')
->select('product_weights.value', 'products.productname')
->get();
This gives something kind of useful, var_dump giving:
[{"value":"1kg","productname":"Item 1"},{"value":"2.1kg","productname":"Item 2"},{"value":"250g","productname":"Item 3"},{"value":"1kg","productname":"Item 3"}
The issue is that a product item 5 with no weight variations is not returned. And of course I need to build in flavours too.
I then want to just make an array like {'Item 1 1kg', 'Item 2 2.1kg', 'Item 3 250g', 'Item 3 1kg'}
Any ideas? I kind of feel like I am doing the join wrong. Help would be appreciated!
Update: Thanks to comments below, some progress has been made with leftJoin. I can produce the desired results for weight or flavour, but not both. The code now is:
$product_join_weights = DB::table('products')
->leftJoin('product_weights', 'products.id', '=', 'product_weights.prodid')
->select('products.productname', 'product_weights.value')
->get();
$product_join_flavour = DB::table('products')
->leftJoin('product_flavours', 'products.id', '=', 'productattributes.prodid')
->select('products.productname', 'product_flavours.value')
->get();
Any ideas how they can be combined?
Instead of combining the two arrays, why not just chain the joins together?
$product_join = DB::table('products')
->leftJoin('product_weights', 'products.id', '=', 'product_weights.prodid')
->leftJoin('product_flavours', 'products.id', '=', 'productattributes.prodid')
->select('products.productname', 'product_flavours.value', 'products.productname', 'product_weights.value')
->get();
Left Joins fetch a complete set of records from table1, with the
matching records in table2. The
result is NULL in the right side when no matching will take place.
http://www.w3resource.com/sql/joins/perform-a-left-join.php
I think you could simply double the left join...
something like this:
$product_weights_and_flavours = DB::table('products')
->leftJoin('product_weights', 'products.id', '=', 'product_weights.prodid')
->leftJoin('product_flavours', 'products.id', '=', 'productattributes.prodid')
->select('products.productname', 'product_weights.value', 'product_flavours.value')
->get();

Laravel 4 select column from another table in subquery

I am attempting to do the equivalent of this:
select p.id, p.title, b.brand,
(select big from images where images.product_id = p.id order by id asc limit 1) as image
from products p
inner join brands b on b.id = p.brand_id
Here is where I am at now, but it of course doesn't work:
public function getProducts($brand)
{
// the fields we want back
$fields = array('p.id', 'p.title', 'p.msrp', 'b.brand', 'p.image');
// if logged in add more fields
if(Auth::check())
{
array_push($fields, 'p.price_dealer');
}
$products = DB::table('products as p')
->join('brands as b', 'b.id', '=', 'p.brand_id')
->select(DB::raw('(select big from images i order by id asc limit 1) AS image'), 'i.id', '=', 'p.id')
->where('b.active', '=', 1)
->where('p.display', '=', 1)
->where('b.brand', '=', $brand)
->select($fields)
->get();
return Response::json(array('products' => $products));
}
I don't really see anything in the docs on how to do this, and I can't seem to piece it together from other posts.
In "regular" SQL, the subquery is treated AS a column, but I am not sure how to string that together here. Thanks for any help on this.
I strongly recommend you to use Eloquent, instead of pure SQL. It's one of the most beautful things in Laravel. Two models and relations and it's done! If you need to use pure SQL like that, put it all in DB::raw. It's easier, simpler and (ironically) less messy!
With the models, you could use relations between the two tables (represented by the models itself) and say (so far I understood) that Brands belongs to Products, and Images belongs to Product. Take a look at Eloquent's documentation on Laravel. Probably will be more clearly.
Once the relations are done, you can only say that you wanna get
$product = Product::where(function ($query) use ($brand){
$brand_id = Brand::where('brand', '=', $brand)->first()->id;
$query->where('brand_id', '=', $brand_id);
})
->image()
->get();
That and a better look at Eloquent's documentation should help you to do the job.
P.S.: I didn't test the code before send it and wrote it by head, but i think it works.

Categories