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();
Related
I have problem with my eloquent query, i need to use data of my base model into whereRelation.
I tried this query bottom, but results was not what i except. The query return me all users who have one city relation, not only user who have city updated between my last user sync.
$users = People::whereRaw('TIMESTAMPDIFF(SECOND, people.latest_sync, people.updated_at) > 20')
->orWhereRelation('city', 'updated_at', '>', 'people.updated_at')
->get();
I'v tried people.updated_at and latest_sync in value of my Where Relation
Do i need to make pure SQL Raw query with classic join ?
PS: the first whereRaw is ok, and work (i really need)
I am trying to retrieve additional data from a relation. But so far I have not been able to do this. It is about a group that contains products. I want to retrieve data related to these products. So far I have not found a solution to realise this.
Tables with fields:
lager_stock_booking_groups
id,datetime
lager_stock_booking
id, lagerProductId, stockBookingGroupId(id from Table lager_stock_booking_groups)
lager_products
id(lagerProductId from Table lager_stock_bookings)
$bookingGroup = LagerStockBookingGroup::with('stockBookingProducts')->where('id', '=', $id)->orderBy('dateTime', 'desc')->get();
This code get me the groups and Products liek this:
Group[ID 23]
Product 1
Product 2
Product 3
I have only the Product-ID's. How can get additional Data of Products from Table lager_products?
Thank you for your help.
you can get with relation :
$bookingGroup->stockBookingProducts
or use join
in a Laravel application, I have a list of companies. Some of those are related to a subscriptions table in a 1 x n relation.
I want to order the companies in a way that the ones which have a subscription appear before the ones which have no subscription using just one db query.
Any idea is much appreciated!
thanks!
Laravel uses separate queries to eager load relationships. So if you want to do this you will need to join the tables and order by...
Something in the form...
$companies = App\Companies::join('subscriptions as sub', 'sub.company_id', '=', 'company.id')
->orderBy('sub.somefield', 'DESC')
->select(what you need)
->with('if you need the relation data');
You know you can also only query those records that have a relationship with
$companies = App\Companies::has('subscription')->get();
Maybe that is all you need... and
$companies = App\Companies::doesntHave('subscription')->get();
... returns the opposite where the company has no subscription...
Let's consider the image above. I would like to show duplicated entries as one entry and also I want to show the sum of the "stock" column. In this case it should be 5722.
Is it possible to do it using Eloquent? Or what are the best ways to do it?
Not sure how your database / query is built but you could maybe use something like that:
Item::groupBy('item_name')
->selectRaw('*, sum(stock) as sum')
->get();
It will return a collection of Item with an additional “sum” field
This will group the result with same medicine and supplier name and sum up the stock.
$result = Model_Name::groupBy('medicine_name','supplier_name')
->selectRaw('*, sum(stock) as sum')
->get();
Try this. Hope it might help you.
Hi i have tables with one to many relation
sectors
id
name
position
seat_plans
id
name
sector_id
I just want to select all seat plans order by sectors.position. I tried
$seat_plans = SeatPlan::with(['sector' => function($q){
$q->orderBy('position');
}
])->get();
but it is not working. when i check The SQL it is generating query like
select * from seat_plans
can anybody please tell me how to do this?
I don't think you need a custom function for your use case. Instead try this:
$users = DB::table('seat_plans')
->join('sectors', 'seat_plans.sector_id, '=', 'sectors.id')
->select('seat_plans.*')
->orderBy('sectors.position')
->get();