简体   繁体   中英

how to select join in laravel with condition

 public function clientcmd()
    {
        $client = Commande::join('clients', 'commande.id_cli', "=", 'clients.id')
        ->select('clients.*')
        ->where('commande.id_cli', '')
        ->get();
        return response()->json($client);
    }

I want to select all the client where id client = id cli in command table but that not work they return an empty array

We don't know the data in your table. I assume you want to select fields whose id_cli field is not empty string.

$client = Commande::join('clients', 'commande.id_cli', "=", 'clients.id')
        ->select('clients.*')
        ->where('commande.id_cli','!=', '')
        ->get();

Use Query builder :

use Illuminate\Support\Facades\DB;
 
$users = DB::table('commande')
            ->join('clients', 'commande.id_cli', '=', 'clients.id')
            ->select('clients.*')
            ->where(//you can put condition here)
            ->get();

You can use Client Model to get all client records

$clients = Client::select('*')
            ->join('commande', 'commande.id_cli', '=', 'clients.id')
            ->where("clients.id",$client_id) // put your condition here
            ->get();

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