Skip to content

Instantly share code, notes, and snippets.

@marcoszf
Last active March 22, 2016 16:54
Show Gist options
  • Select an option

  • Save marcoszf/5680189c16549fc6b16d to your computer and use it in GitHub Desktop.

Select an option

Save marcoszf/5680189c16549fc6b16d to your computer and use it in GitHub Desktop.
mod Banner - emidcms - Laravel - Repository pattern
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Banner extends Model {
protected $table = "tab_banners";
protected $primaryKey = "id";
public $timestamps = false;
public function scopeBetweenDts(){
return $this->where('inicio', '<=', date('Y-m-d H:i:s'))
->where('fim', '>=', date('Y-m-d H:i:s'));
}
public function scopeAtivo($query){
return $query->where('status','A');
}
}
<?php
namespace App\Repositories;
interface BannersRepositoryInterface{
public function all();
}
<?php
namespace App\Services;
use App\Services\ImagesService;
use App\Repositories\BannersRepositoryInterface;
use Image;
use Config;
use File;
class BannersService
{
private $bannerRepository;
private $imgsService;
protected $moduleName;
public function __construct(BannersRepositoryInterface $bannerRepository, ImagesService $imgsService)
{
$this->bannerRepository = $bannerRepository;
$this->imgsService = $imgsService;
$this->moduleName = 'banners';
}
public function all(){
$banners = $this->bannerRepository->all();
$rowsBanners = array();
foreach ($banners as $key => $value) {
$idRow = $banners[$key]->id;
$rowsBanners[$key]['path'] = $this->imgsService->imgCrop($idRow, $this->moduleName, 1650, 411);
$rowsBanners[$key]['id'] = $idRow;
$rowsBanners[$key]['ban_link'] = isset($banners[$key]->link) ? $banners[$key]->link : null;
}
return $rowsBanners;
}
}
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Config;
use File;
use Input;
use Mail;
use Cache;
use App\Services\BannersService;
class HomeController extends Controller {
private $bannerService;
public function __construct(BannersService $bannerService){
$this->bannerService = $bannerService;
}
public function index()
{
/* Banners */
$rowsBanners = $this->bannerService->all();
return view('home')->with(['rowsBanners'=>$rowsBanners]);
}
}
<?php
namespace App\Repositories\Eloquent;
use App\Repositories\BannersRepositoryInterface;
use App\Banner;
class MysqlBannersRepository implements BannersRepositoryInterface
{
public function all(){
return Banner::betweenDts()->ativo()->get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment