How to Syntax OrderBy whit value in eloquent - laravel

I need to add a by order with value in eloquent, how can I do it?
example
Order By
c.id = 23 DESC
enter image description here

Is this a joined table if yes show model and controller?
if it not joined table then try this:
->orderBy('c.id','DESC'), or ->orderByCId('DESC') or ->orderByDesc('c.id')

Related

Inner Join in laravel missing P

I needed to combine the two table product_price and trade_channels, when I use inner join,
the ID of product_price is remove.
Here is my code
DB::table('product_price')->where('product_id',$id)->where('action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
A good practise is to select a column that you actually want to use no need of all columns.
Suppose in this case you require all column of product_price table and only customer id from customer_price table then you can do something like this:
DB::table('product_price')
->select(['product_price.*','customer_price.id AS customer_id'])
->where('product_price.product_id',$id)
->where('product_price.action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
You can select any column but it's good to take alias of join table column in this case it is customer_price so it's not getting confusion if both table has same name column.
Good Luck
try
DB::table('product_price')
->select('*','product_price.id AS product_price_id')
->where('product_id',$id)
->where('action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
the product_price id would be replace with customers id, so just print out the product_price id with other name.
hope it is help

Aggregate function in laravel 5.4 with joining

I have two tables :
users table
{id, name}
payments table
{id, user_id, payment}
Here I want to join two tables and want to use SUM(payment) function group by id.
please give me a solution.
You can do join like this way:
$payments = DB::table('users')->join('payments','users.id','=','payments.user_id')->groupBy('users.id')->sum('payment');
//use DB to in you controller
You can use a queryBuilder for make de custom query.

Laravel Eloquent select function cause empty relation

Following is my query
$user = User::select(['uuid','name','about'])->with(['education','work'])->first();
this returns empty data for relationship education and work,
but if I remove select function from query I am getting data in relationship and it also returns all columns of user table which I don't want.
how can solve this problem
The problem is that relationships (with(...)) execute an additional query to get the related results. Let's say you have one to many relationship where users have many works. User::with('work')->find(1) will then execute these 2 queries:
select user where id = 1 and select works where user_id = 1.
So basically in order to be able to execute the second query (fetch relationship data) you need to include id (or whichever column you're referencing) in you select statement.
Fix:
$user = User::select(['uuid','name','about', 'id'])->with(['education','work'])->first();
Same principle in different forms applies to all relationships. For example in the inverse of hasMany which is belongsTo you would need to select the foreign key (for example user_id).

Newest items and GROUP By with Eloquent

I have the following prices-table:
shop_id (int)
product_id (int)
price (float)
created (DateTime)
Every hour a cronjob checks the shops and inserts new entries (current prices) into these price-table.
Now I want to display the newest price for a product. I have to GROUP BY the shop_id because I only want one price per shop but I only want the newest entry (created).
Can I solve this with Eloquent Query-Builder or do I have to use raw SQL? Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
You can try it as:
Price::select('*', DB::raw('MAX(created_at) as max_created_at'))
->groupBy('shop_id')
->get()
Assuming model name is Price
Eloquent (purist) approach:
Price::orderBy('created', 'desc')->groupBy('shop_id')
->get('shop_id', 'price');
References:
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_orderBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_groupBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_get
*untested though
Q: Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
A: you could pass it to Model's contructor - but it might need model's field to be fillable - or hydrate a model. Alternatively, just access it like an keyed-array, ie. $something[0]['price'] <-- assuming an array of prices with price column.
I solved the problem without QueryBuilder. Instead I use a raw SQL-statement and generating the models with the hydrateRaw()-function of the Model-class.
$prices = Price::hydrateRaw( 'SELECT p.*
FROM prices p
INNER JOIN (
SELECT shop_id, max(created_at) AS max_ca
FROM prices p1
GROUP BY shop_id
) m ON p.shop_id = m.shop_id AND p.created_at = m.max_ca');

ORACLE Select list inconsistant with group by

I can't create my view and I don't know how to fix it.
when trying to create the view it tries to add all the columns from select to the group by, which isn't what I want. and doesn't work anyways
CREATE OR REPLACE FORCE VIEW CustomerSaleHistoryView AS
Select SALE.SaleID,
SALE.SaleDate,
CUSTOMER.LastName,
CUSTOMER.FirstName,
SALE_ITEM.SaleItemID,
SALE_ITEM.ItemID,
ITEM.ItemPrice,
SUM(SALE_ITEM.ITEMPRICE),
AVG(SALE_ITEM.ITEMPRICE)
from customer
join sale on customer.CUSTOMERID = Sale.CUSTOMERID
join sale_item on sale_item.saleid = sale.saleID
join item on sale_item.itemID = item.itemID
group by CUSTOMER.LastName, CUSTOMER.FirstName, SALE.SaleID;
In order for you query to work you MUST group by any columns that are not being aggregated with a formula like SUM() or AVG(). In your case GROUP BY SALE.SaleID, SALE.SaleDate, CUSTOMER.LastName, CUSTOMER.FirstName, SALE_ITEM.SaleItemID, SALE_ITEM.ItemID, ITEM.ItemPrice
What are you trying to accomplish that you don't believe this GROUP BY is appropriate?
When using group by clause the list of columns that you are selecting should be either part of the group by clause or they should be part of an aggregate function. In your case SALE.SaleDate, SALE_ITEM.SaleItemID, SALE_ITEM.ItemID and ITEM.ItemPrice doesn't satisfy that rule. You need to include these to your group by clause. Once you fix the select statement and when it returns the desired output convert that into a view.

Resources