【问题标题】:Laravel 5.2 Moving Query to Model from ControllerLaravel 5.2 将查询从控制器移动到模型
【发布时间】:2016-07-19 03:16:28
【问题描述】:

嗨,我有一个用于搜索表单的大型查询,我只是想知道如何将它移到模型中,我已经阅读了查询范围等等,但这比仅仅复杂得多为 1 个字段搜索 1 个表,我很迷茫,感谢任何帮助!

public function index()
{
    $input = Request::all();
    $date = \Carbon\Carbon::now()->subMinute(30);
    $query = User::rightJoin('user_profiles', 'users.id', '=', 'user_profiles.user_id');

    if (isset($input ['minAge']) && $input['minAge']) {
        $minAge = $input['minAge'];
        $maxDate = \Carbon\Carbon::today()->subYears($minAge)->endOfDay();
    }

    if (isset($input ['maxAge']) && $input['maxAge']) {
        if ($input['maxAge'] < $input['minAge']) {
            $maxAge = $input['minAge'];
        }
        else {
            $maxAge = $input['maxAge'];
        }
        $minDate = \Carbon\Carbon::today()->subYears($maxAge + 1);
    }

    if (isset($input['u']) && $input['u'])
        $query->where('users.username', '=', $input['u']);
    if (isset($input['p'])  && $input['p'])
        $query->where('user_profiles.postcode', '=', $input ['p']);
    if (isset($input['o1']) && $input['o1'])
        $query->where('users.last_online','>=',$date);
    if (isset($input['o2']) && $input['o2'])
        $query->whereNotNull('user_profiles.avatar');
    if (isset($input ['o3']) && $input['o3'])
        $query->orderBy('users.type', 'ASC');
    if (isset($input ['minAge']) && $input['minAge'])
        $query->whereBetween('user_profiles.dob', [$minDate, $maxDate]);
    if (isset($input ['g']))
        $query->whereIn('user_profiles.gender',$input ['g']);

    $query->orderBy('users.last_online', 'DESC');
    $users = $query->paginate(1);
    return view('view', compact('users'));
}

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    你可以这样做。在 User 模型的函数中接受 $input 作为参数。

    在用户模型中,添加以下代码。

    public static function allUsers($input)
    {
        $date = \Carbon\Carbon::now()->subMinute(30);
        $query = User::rightJoin('user_profiles', 'users.id', '=', 'user_profiles.user_id');
    
        if (isset($input ['minAge']) && $input['minAge']) {
            $minAge = $input['minAge'];
            $maxDate = \Carbon\Carbon::today()->subYears($minAge)->endOfDay();
        }
    
        if (isset($input ['maxAge']) && $input['maxAge']) {
            if ($input['maxAge'] < $input['minAge']) {
                $maxAge = $input['minAge'];
            }
            else {
                $maxAge = $input['maxAge'];
            }
            $minDate = \Carbon\Carbon::today()->subYears($maxAge + 1);
        }
    
        if (isset($input['u']) && $input['u'])
            $query->where('users.username', '=', $input['u']);
        if (isset($input['p'])  && $input['p'])
            $query->where('user_profiles.postcode', '=', $input ['p']);
        if (isset($input['o1']) && $input['o1'])
            $query->where('users.last_online','>=',$date);
        if (isset($input['o2']) && $input['o2'])
            $query->whereNotNull('user_profiles.avatar');
        if (isset($input ['o3']) && $input['o3'])
            $query->orderBy('users.type', 'ASC');
        if (isset($input ['minAge']) && $input['minAge'])
            $query->whereBetween('user_profiles.dob', [$minDate, $maxDate]);
        if (isset($input ['g']))
            $query->whereIn('user_profiles.gender',$input ['g']);
    
        $query->orderBy('users.last_online', 'DESC');
        $users = $query->paginate(1);
        return $users;
    }
    

    然后在您的 Controller 中,只需更新以下内容。

    带静态的,

    public function index()
    {
        $users = User::allUsers(Request::all());
        return view('view', compact('users'));
    }
    

    或者,

    public function index()
    {
    
        $user = new User;
        $user->allUsers(Request::all());
        return view('view', compact('users'));
    }
    

    一旦你这样做了,你可以很容易地开始使用更小的范围。要使用示例范围,您可以查看此Laravel. Use scope() in models with relation

    让我知道它是否适合你。

    【讨论】:

    • 啊我只是得到一个错误非静态方法 App\Search::allUsers() 不应该被静态调用,假设 $this 来自不兼容的上下文(我已经把它放在它自己的搜索模型和控制器中自从我上次发帖以来,搜索。)
    • 再次感谢伙计,它成功了,只是一个快速修复,你有 $users = User::allUsers(Request::all);毕竟它缺少 () (Request::all());
    • 很高兴它对你有用!谢谢指正。我还更新了主要帖子以供其他人将来参考。干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多