Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel中优化查询?_Php_Laravel - Fatal编程技术网

Php 如何在laravel中优化查询?

Php 如何在laravel中优化查询?,php,laravel,Php,Laravel,嗨,我想最小化此代码: if (empty(Input::get('channelName'))) { $channels = Channel::orderBy('updated_at', 'desc')->paginate($perPage)->setPath('?limit=' . $perPage); } else { $channels = Channel::where('name', 'like', '%' . Input::get('channelName

嗨,我想最小化此代码:

if (empty(Input::get('channelName'))) {
    $channels = Channel::orderBy('updated_at', 'desc')->paginate($perPage)->setPath('?limit=' . $perPage);
} else {
    $channels = Channel::where('name', 'like', '%' . Input::get('channelName') . '%')->orderBy('updated_at', 'desc')->paginate($perPage)->setPath('?limit=' . $perPage);
}
这是一个很酷的“东西”,因为它允许你链接你需要的方法。这就提供了根据需要附加查询语句的灵活性

$channelsQuery = Channel::orderBy('updated_at', 'desc');
if (Input::has('channelName')) {
    $channelsQuery = $channelsQuery->where('name', 'like', '%' . Input::get('channelName') . '%');
}
$channels = $channelsQuery->paginate($perPage)->setPath('?limit=' . $perPage);
在if中也使用
Input::has('channelName')
,而不仅仅是
empty(Input::get('channelName'))

$channelsQuery = Channel::orderBy('updated_at', 'desc');
if (Input::has('channelName')) {
    $channelsQuery = $channelsQuery->where('name', 'like', '%' . Input::get('channelName') . '%');
}
$channels = $channelsQuery->paginate($perPage)->setPath('?limit=' . $perPage);