Rafael Alencar
Rafael Alencar

Reputation: 17

Check if a data is inside a other table

I am using a method to compare information in different tables but I am not sure if this method really is the best, I would like some options...

Example:

My system its about scales of work for employees, and I have a table with all the employees and a table of scales, and I wanna check all the employees that is appointed to that scale, I would do like this...

foreach($employees as $em) {
    $continue = false;
    foreach( $employees_scales as $es) {
        if ($em->id == $ec->em_id && $ec->date == $date_example) {
            $continue = true;
        }
    }
    if(!$continue){
         // List the employee;
    }
}

Upvotes: 1

Views: 40

Answers (1)

Buglinjo
Buglinjo

Reputation: 2076

I highly recommend you to use Eloquent or DB for getting such information.

  • You should create Model (Employee) where will be many to many relationship with second Model (Scale)
  • After that you will only have to do like this:

    $scale->employees; // this will get all employees which has this scale...

For additional info how to create Many to Many relationship please read this documentation: https://laravel.com/docs/5.4/eloquent-relationships#many-to-many

Upvotes: 1

Related Questions