gpt4 book ai didi

php - Laravel 5.2 将查询从 Controller 移至模型

转载 作者:行者123 更新时间:2023-11-29 11:37:44 25 4
gpt4 key购买 nike

嗨,我有一个针对搜索表单的大型查询,我只是想知道如何将其移动到模型中,我已经阅读了查询范围等内容,但这比仅仅复杂得多搜索 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'));
}

最佳答案

你可以这样做。接受 $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

请告诉我它是否适合您。

关于php - Laravel 5.2 将查询从 Controller 移至模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36320349/

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