问题:如何在laravel中使用和where

Question: how to use and with where in laravel

当我想从本机数据库中获取数据时 PHP 我使用查询,但在 laravel 中,您不需要编写查询,而是使用它们的模型和函数来获取数据示例:

ex::where('name','test')->get();

原生 PHP:

select * from ex where name='test'

我的问题是我不知道如何使用和喜欢这个查询:

select * from ex where name='test' and id='5'

我搜索并查看了文档,但没有找到答案。

再放一个就可以了,真的很简单就这样放

ex::where('name', 'test')->where('id', 5)->get();
//or if you want directly the instance and not a collection
ex::where('name', 'test')->find(5);

如果你想使用 OR 运算符,你也可以这样做

ex::where('name', 'test')->orWhere('id', 5)->get();

更多in documentations

Laravel Eloquent 中没有 'AND'。 您必须根据要求的条件再写一个 'where'。

另一种方法是:运行 使用 DB facade 进行查询。 DB facade 为每种类型的查询提供了方法:select、update、insert、delete 和 statement。

例如。 DB::statement("UPDATE teachers SET price = ".$price." where id=".$id." AND status='A'");

不要忘记使用数据库