如何使用 laravel 查询生成器进行以下查询($sql)?我的意思是我想像这样使用
$sql=\DB::table('animal).....
但我无法这样做。请帮助我
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...
}
}
此查询不起作用,它不显示任何结果及其计数
$records = \DB::table('animals')
->select(DB::raw('count(*)'))
->where('animal', '=', '$animal')
->get();
最佳答案
阅读文档,它有 select 和其他语句的完整指南。 Laravel documentation
$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}
关于php - 如何使用 Laravel 查询生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668675/