Php Laravel如何干掉这段代码?
我在多个文件中有以下代码。我想把它擦干 代码的目的是返回当前周的值,该值可以是1到17 model Schedule.phpPhp Laravel如何干掉这段代码?,php,laravel,laravel-5,dry,laravel-5.1,Php,Laravel,Laravel 5,Dry,Laravel 5.1,我在多个文件中有以下代码。我想把它擦干 代码的目的是返回当前周的值,该值可以是1到17 model Schedule.php public function scopeCurrentWeekGames($query) { return $query->where('week', '=', $this->currentWeek()); } public function currentWeek() { $currentWeek = Schedule::distinc
public function scopeCurrentWeekGames($query) {
return $query->where('week', '=', $this->currentWeek());
}
public function currentWeek()
{
$currentWeek = Schedule::distinct()
->where('gameTime', '<', Carbon::now()->addHours(50))
->orderBy('gameTime', 'desc')
->lists('week')
->first();
return $currentWeek;
}
公共函数作用域CurrentWeekGames($query){
返回$query->where('week','=',$this->currentWeek());
}
公共功能当前周()
{
$currentWeek=计划::distinct()
->其中('gameTime','我认为,您可以使用PHP traits来实现这一点,或者更好地创建Helper类,并从中使用您的代码
例如,我正在使用存储在app/libraries/
中的Laravel 4.2和my Helper.php:
<?php
namespace App\Libraries;
class Helper
{
/**
* @param $string
*
* @return mixed
*/
public static function stringToBool($string)
{
return filter_var($string, FILTER_VALIDATE_BOOLEAN);
}
}
对于Laravel 5.1:
在app/Http目录中,创建helpers.php文件并添加函数
在composer.json中的自动加载块中,添加“文件”:[“app/Http/helpers.php”]
运行composer转储自动加载
你可以阅读更多信息
祝你好运!在Laravel中,使用存储库在DB上创建一个抽象层是一个很好的做法:
class ScheduleRepository
{
public function getCurrentWeek()
{
$currentWeek = Schedule::distinct()
->where('gameTime', '<', Carbon::now()->addHours(50))
->orderBy('gameTime', 'desc')
->lists('week')
->first();
return $query->where('week', '=', $currentWeek);
}
/*
Here other methods to access the Schedule model, i.e:
public function getAll()
{
//return all schedule models...
}
*/
}
使用这种方法,您可以使代码保持干燥,因为您只在存储库类中编写查询,并且可以通过调用存储库方法随时随地访问查询
您也可以考虑使您的存储库实现一个接口,并且您将获得灵活性,因为将来您可以使用存储库的另一个实现(例如访问MangoDB数据库),但是您不必更改应用程序
中的方法调用,如果需要从多个库中获取数据呢?
<?php
namespace App\Libraries;
class Helper
{
/**
* @param $string
*
* @return mixed
*/
public static function stringToBool($string)
{
return filter_var($string, FILTER_VALIDATE_BOOLEAN);
}
}
section:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/services",
"app/models",
"app/parsers",
"app/libraries", // Check if isset or add.
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
class ScheduleRepository
{
public function getCurrentWeek()
{
$currentWeek = Schedule::distinct()
->where('gameTime', '<', Carbon::now()->addHours(50))
->orderBy('gameTime', 'desc')
->lists('week')
->first();
return $query->where('week', '=', $currentWeek);
}
/*
Here other methods to access the Schedule model, i.e:
public function getAll()
{
//return all schedule models...
}
*/
}
class PicksController extends Controller {
protected $schedule;
//inject your repository where you need it
public function __construct( ScheduleRepository $schedule )
{
$this->schedule= $schedule;
}
public function index()
{
//call a method on the repository
$week = $this->schedule->getCurrentWeek();
//do whathever you want with week...
}
}