[英]How to use Laravel query builder
How can I use laravel query builder for following query($sql)? 如何使用laravel查询生成器进行以下查询($ sql)? I mean I want to use like this 我的意思是我想这样使用
$sql=\\DB::table('animal).....
but I am unable to do so. $sql=\\DB::table('animal).....
但我无法这样做。 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. 阅读文档,其中包含关于select和其他语句的完整指南。 Laravel documentation Laravel文档
$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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.