简体   繁体   中英

how to show records that are different from the where clause

I have a query that I am doing, what I need is to show only the records that do not match the where clause.

mechanic_client

id user_id mechanic_id
13 31 13
16 34 1
26 61 1

users

id name
1 lucas
31 mauricio
34 pedro
61 carlos

The user admin lucas created Pedro and Carlos, they are created both in the users table and in the mechanic_client table, so what I want is not to show Pedro and Carlos, only show the other users, but the function does not work for me it continues to show me to all users.

function index

 $users_mechanics = DB::table('mechanic_client')->get();

        foreach ($users_mechanics as $user_mechanic) {
            
            $users = User::orderBy('id', 'DESC')->with('roles')->where('id', '<>', $user_mechanic->user_id)->paginate(10);


            return [
                'pagination' => [
                    'total'         => $users->total(),
                    'current_page'  => $users->currentPage(),
                    'per_page'      => $users->perPage(),
                    'last_page'     => $users->lastPage(),
                    'from'          => $users->firstItem(),
                    'to'            => $users->lastItem(),
                ],
                'users' => $users
            ];
        
            
        } 

query return

id name
1 lucas
31 mauricio

it should look like this

Thanks for the help..

MechanicClient Model
<?php

 namespace App\Model;

 use Illuminate\Database\Eloquent\Model;

class MechanicClient extends Model {

  public $table = 'mechanic_client';
  protected $primaryKey = 'id';
  protected $fillable = [
    'user_id',
    'mechanic_id',
 ];
}

In User Model

public function mechanic() {
    return $this->hasone('App\Model\MechanicClient', 'user_id', 'id');
}

And check In Controller

User::doesntHave('mechanic')->orderBy('id', 'DESC')->paginate(10);

Hope fully it will help to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM