gpt4 book ai didi

php - 如何使用 Laravel 查询生成器

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:14 25 4
gpt4 key购买 nike

我如何使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com