search

Home  >  Q&A  >  body text

Learn how to write this statement using SQL in Laravel

    select count('vehicules.id')
    from vehicules
      INNER JOIN (equipements, contenirs)
         ON (contenirs.vehicule_id = vehicules.id AND contenirs.equipement_id = equipements.id)
     WHERE(vehicules.KMActuel-contenirs.dernierKM > equipements.kilometrageMax);

This is the code I want to execute in Laravel. I tried the following code:

$special = DB::select("SELECT COUNT('vehicules.id') FROM vehicules INNER JOIN (equipements, contenirs) ON (contenirs.vehicule_id = vehicules.id AND contenirs.equipement_id = equipements.id) WHERE(vehicules.KMActuel-contenirs.dernierKM > equipements.kilometrageMax)");

I called a variable in the view and got the following error:

htmlspecialchars() expects parameter 1 to be string, array given (View: C:\laragon\www\GSMV\resources\views\dash.blade.php)

P粉851401475P粉851401475502 days ago583

reply all(1)I'll reply

  • P粉099145710

    P粉0991457102023-09-08 09:19:39

    You cannot execute expressions in strings passed to regular Eloquent methods. You must use a primitive expression that can be parsed accordingly:

    DB::table('vehicules')
        ->join('contenirs', 'vehicules.id', 'contenirs.vehicule_id') 
        ->join('equipements','equipements.id','contenirs.equipement_id') 
        ->whereRaw('vehicules.KMActuel-contenirs.dernierKM > equipements.kilometrageMax')
        ->select('vehicules.*', 'contenirs.designation')
        ->count('vehicules.id');
    

    reply
    0
  • Cancelreply