I have 2 queries and I want to check if values of one queries exists in array of other query. I have "maintenance" which has "process_id" and "processes" (array of process id's). I want to check for each process_id if exists in processes. in my controller:
$processes = DB::select('select id from processes');
$maintenance = DB::select('select process_id from maintenances where car_id ="' . $id . '" group by process_id');
$result = array();
foreach ($processes as $key => $value) {
$result[] = $value->id;
}
In my Helper.php
public static function array_value($value, $array) {
$results = 0;
$str = "$value";
if (in_array($str, $array)) {
$results += 5;
} else {
$results += 1;
}
return $results;
}
In my view
@foreach ($maintenance as $m)
<tr> @if (Helpers\Helper::array_value($m->process_id, $processes)== 5)
<td> {{ $m->process }} </td>
@elseif (Helpers\Helper::array_value($m->process_id, $processes)== 1)
<td>Missing</td>
@endif
</tr>
@endforeach
And it displays only values in the first if. It doesn't displays Missing when process_id isn't found in array processes
Can I get a dump of the $processes and $maintenance?
I did a quick script of what I believe you're trying to achieve and it works for me. I get an output of 55155 which shows that the 3 is missing from the $processes array.
$processes = [1, 2, 4, 5];
$maintenance = [1, 2, 3, 4, 5];
$array_value = function ($value, $array) {
$str = "$value";
if (in_array($str, $array)) {
echo 5;
} else {
echo 1;
}
};
foreach ($maintenance as $m) {
$array_value ($m, $processes);
}
Does this work : ####Logic
$processes = DB::table('services')->get();
$process_id_all = array_pluck($processes, 'id');
$maintenances = DB::table('maintenances')
->select('id')
->whereCarId($id)
->groupBy('category_id')->get();
$process_id_maintainance = array_pluck($maintenance, 'process_id');
####View
@foreach ($maintenances as $maintenance)
Id : {{ $maintenance->id }}
Process :
@if (in_array($process_id_maintainance, $process_id_all))
{{ $maintenance->process }}
@else
Record missing;
@endif
@endforeach
could you use the array_diff or array_intersect methods?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community