I have some cars and I want to check whether there are other cars similar to one of them or not.
So I have:
$car = Car::where('car_id', $carId)->first();
if($car)
{
$duplicateCar = Car::where('car_code', $car->car_code)->first();
if($duplicateCar)
{
//Do sth
}
}
However my conditions for the second query are more complex.
I want to fetch all rows and check whether column1
, column2
and other columns are same as the desired car, if yes $conditionTrueCount
to increment and if $conditionTrueCount
is upper than 5 so it is duplicate.
So I have:
$car = Car::where('car_id', $carId)->first();
if($car)
{
$cars = Car::all();
foreach($cars as $car2)
{
$conditionTrueCount = 0;
if($car->car_code == $car2->car_code)
$conditionTrueCount++;
if($car->column1 == $car2->column1)
$conditionTrueCount++;
if($car->column2 == $car2->column2)
$conditionTrueCount++;
if($car->column3 == $car2->column3)
$conditionTrueCount++;
if($car->column4 == $car2->column4)
$conditionTrueCount++;
if($car->column5 == $car2->column5)
$conditionTrueCount++;
if($car->column6 == $car2->column6)
$conditionTrueCount++;
if($conditionTrueCount > 5)
{
//It is duplicate, do something!
}
}
}