如何使用where和with来使用雄辩的模型进行破坏

时间:2017-01-09 07:05:27

标签: laravel laravel-5.3

我在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;
}   

现在如何使用用户变量获取已删除的结果 我试图把withTrashed - >之后到处都有,但注意到工作..

谢谢

2 个答案:

答案 0 :(得分:2)

请尝试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;
    }   

答案 1 :(得分:1)

这是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']);
}