简体   繁体   中英

How to use Laravel query builder

How can I use laravel query builder for following query($sql)? I mean I want to use like this

$sql=\\DB::table('animal)..... but I am unable to do so. Please help me

foreach ($animalsTypes as $animal)
          {
    //some code here...
             $sql ="SELECT count(*) FROM animals WHERE animal='$animal'";
              $records = \DB::select($sql);

               foreach($records as $record){
         //some code here...      
         }
    }

This query in not working, it doesnot display any results and their count

  $records = \DB::table('animals')
                 ->select(DB::raw('count(*)'))
                 ->where('animal', '=', '$animal')
                 ->get();

read out the documentation it has complete guide for select and other statements. Laravel documentation

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

foreach ($users as $user)
{
    var_dump($user->name);
}

If you want to count:

$amount_of_animal = DB::table('animals')->where('animal',$animal)->count();

If you want the results as well:

$animal = DB::table('animals')->where('animal',$animal)->get();

尝试-

DB::table('animals )->where('animal', $animal)->count();

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