laravel 如何使用 where 和 with
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41542648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get trashed with eloquent model using where and with
提问by Awar Pulldozer
I have users model in laravel
我在 Laravel 中有用户模型
public static function searchScoop($keyword)
{
$users = User::where('username','like','%'.$keyword.'%')->
orwhere('email','like','%'.$keyword.'%')->
orwhere('phone','like','%'.$keyword.'%')->
with('user_permission','user_division')->
get(['id','username','email','phone','division','permission']);
return $users;
}
now how can i get trashed results with users variable i tried to to put withTrashed -> before where after get everywhere but noting works ..
现在我怎么能用我试图放置 withTrashed -> 之前的用户变量获得垃圾结果 - > 在到处之后但注意工作..
thank you
谢谢你
回答by Arun Code
Please try with withTrashed()
which will give only the soft deleted records.
请尝试使用 withTrashed()
它只会给出软删除的记录。
public static function searchScoop($keyword)
{
$users = User::withTrashed()
->where('username','like','%'.$keyword.'%')
->orwhere('email','like','%'.$keyword.'%')
->orwhere('phone','like','%'.$keyword.'%')
->with('user_permission','user_division')
->get(['id','username','email','phone','division','permission']);
return $users;
}
回答by Alexey Mezenin
This is correct syntax:
这是正确的语法:
public static function searchScoop($keyword)
{
return User::withTrashed()
->where('username', 'like' ,'%'.$keyword.'%')->
->orwhere('email', 'like' ,'%'.$keyword.'%')->
->orwhere('phone', 'like' ,'%'.$keyword.'%')->
->with('user_permission', 'user_division')->
->get(['id', 'username', 'email', 'phone', 'division', 'permission']);
}