1

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

1 Answer 1

1

If you're using laravel have you considered setting up a relationship to make this task simpler?

I assume process and maintenance will have a one to many relationship?

You could set up a many to many relationship:

in your process model:

public function maintenance() {
    return $this->belongsTo('Maintenance')
}

(where Maintenance is the name of your model class. The function name can be anything)

In your maintenance model:

public function process() {
    return $this->hasMany('Process')
}

Then your revised model retrieval is:

$maintenance = Maintenance::find($id);

UPDATED: $maintenance = Maintenance::where('car_id', '=', $id)->first();

$maintenance->load('process');

(you could chain these)

Then in your view:

@if($maintenance->process()->first())

{{$maintenance->process()->name}} //or whatever positive output

@else

"NO process found" //or your negative output

@endif

The relationship does the hard work for you - you also then have the properties of the related mode available such as name etc which may be more user friendly.

the first() method retrieves the first related model. if this is null (ie not found) then the negative response works. YOu could also use count() which may be better then use:

@if($maintence->process()->count() > 0)

but all depends what you want to do.

YOu can also explore the get() method which will return a collection, useful if you expand your relationship to a many to many eg each maintenance may have many processes/

Hope this helps

Sign up to request clarification or add additional context in comments.

11 Comments

thank you for response. It shows this error in my controller: Call to a member function load() on a non-object at this line: $maintenance->load('process');
and dd($maintenance) shows NULL and I don't have a construct, what should I put there?
if $maintenance is null then it's not found your maintenance model with the id specified. The find() method looks for the model with an id and searched the id column - just re-read your question and your searching on a different column. You'll need to change the approach - just updated the answer
now it shows Call to a member function process() on a non-object at line @if($maintence->process()->first()) in my view. Also I want to check for distinct process_id, is this included ?
there's a spelling mistake! $maintence should be $maintenance
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.