I have one search term $term.
I have 3 tables.
1st is vehicles and has id (int, autoincr., unique), vehicle_id (int) - which can be identical, vehicle_type (int) - can be 1 or 2 - 1 denotes car and 2 is for motorcycle. Example, how it could look like:
----------------------------------
| id | vehicle_id | vehicle_type |
|--------------------------------|
| 1 | 546 | 1 |
----------------------------------
| 2 | 257 | 1 |
----------------------------------
| 3 | 546 | 2 |
----------------------------------
| 4 | 368 | 1 |
----------------------------------
2nd is cars and has id (int, autoincr., unique), title - (varchar)
3nd is motorcycles and has id (int, autoincr., unique), title - (varchar)
Now, I want to use the 1st table's vehicle_id (vehicle.vehicle_id) and join it with the 2nd table's id (cars.id) but only under the condition that the vehicle_type is 1 (car). And at the same time the car's title should contain the $term using LIKE.
And at the same time I want to join 1st table's vehicle_id (vehicle.vehicle_id) with the 3rd table's id (motorcycles.id) but only under the condition that the vehicle_type is 2 (motorcycle). And at the same time the motorcycle's title should contain the $term using LIKE.
So far, I have this code:
$result = DB::table('vehicles')->join('cars', function($join) use($term)
{
$join->on('vehicles.vehicle_id', '=', 'cars.id')
->where('vehicles.vehicle_type', '=', '1')
->where('cars.title', 'LIKE', '%' . $term . '%');
})
->join('motorcycles', function($join2) use($term)
{
$join2->on('vehicles.vehicle_id', '=', 'motorcycles.id')
->where('vehicles.vehicle_type', '=', '2')
->where('motorcycles.title', 'LIKE', '%' . $term . '%');
})
->get();
The problem is that when I have a car and a motorcycle that have the same name and teh $term contains part of it, only the car is catched. If I comment the cars join part the motorcyle is shown, but I want both. How to do that?
Why is the second join being ignored?
Because this works:
$result = DB::table('vehicles')->join('cars', function($join) use($term)
{
$join->on('vehicles.vehicle_id', '=', 'cars.id')
->where('vehicles.vehicle_type', '=', '1')
->where('cars.title', 'LIKE', '%' . $term . '%');
})
->get();
And this works too:
$result = DB::table('vehicles')->join('motorcycles', function($join2) use($term)
{
$join2->on('vehicles.vehicle_id', '=', 'motorcycles.id')
->where('vehicles.vehicle_type', '=', '2')
->where('motorcycles.title', 'LIKE', '%' . $term . '%');
})
->get();
But not when used 2 joins in a row. Why?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire