php - Laravel + query from DB

Good day! :)
The problem is that I can't (don't understand, and don't know) how to specify a comparison of two tables in one query.
We have 2 tables:
- cars
- items
cars structure:
- id
- car_id
- price
inventory structure:
- id
- user_id
- car_id
$cars = \DB::table('cars')
->select('cars.id', 'cars.car_id','cars.name', 'cars.price')
->orderBy('id')
->get();
In views/pages/index.blade.php
@foreach($cars as $cars)
{{$cars->name}}<br>
Price: {{$cars->price}}<br>
You have: **?????**
@endforeach
An example for that should be:
CarName
Price: 1600
You have: 2
CarNameTwo
Price: 1400
You have: 0
CarNameMore
Price: 100
You have: 1
But, how can I include the "you have"? I can only get a list of an array of data, like:
[
{
"car_id":1,
"car_id":4
}
]
But it won't work inforeach
.
Answer
Solution:
@h3x3x you don't havecars.name
in your description of yourcars
table, but you do have that listed in your desired result set. If you did have a column namedname
in yourcars
table, you could get the result set you are looking for by using aleftJoin
, aDB::raw()
and agroupBy
statement in your query, like this:
$cars = \DB::table('cars')
->leftJoin('inventory', 'cars.car_id', '=', 'inventory.car_id')
->select('cars.id', 'cars.name', 'cars.price', \DB::raw('COUNT(DISTINCT inventory.id)'))
->groupBy('cars.car_id')
->orderBy('cars.car_id`)
->get()
This would then return the name of the car as listed incars.name
, the price of the car as listed incars.price
and the count of the cars (or lack thereof) from theinventory
table all ordered bycars.car_id
.
If this isn't what you need please let me know and I'll try to assist further.
Answer
Solution:
Good day! Laravel v. 6.20.14
This is "very light" example from my big problem :)
Ok, I'll show you in the picture "how it should be"
We need to get everything out ofcars
table, but count only those that we have
in addition to this count, there are also other lines that need to be displayed, but now not about them
I also manage to conclude ONLY the presence of those machines that are, but not all :(
It is not so important what we have in the tableinventory
If we try to conclude only by car_id?
Answer
Solution:
So, trial and error systems showed that if we take into account the sample by select, set limited of array in output :((
I absolutely dont have idea how to implement this so that there is a complete list from the cars table
$cars = Cars::
leftjoin('inventory', 'cars.car_id', '=', 'inventory.carid')
->where('inventory.user_id', $this->user->id)
->where('inventory.status', 1)
->select('cars.*', DB::raw("inventory.type as type2"), DB::raw('count(inventory.id) as count'), DB::raw("inventory.created_at as time"))
->groupBy('inventory.carid','cars.car_id')
->orderBy('car_id')
->get();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 8.0.2".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.