iyi günler;
Hobi amaçlı laravel ile ilgilenmekteyim. Php orta seviyedeyim. Mustafa abi uyardıktan sonra olabildiğince standartlara uymaya çalışıyorum. (ingilizce hariç 😃 )
Controllerim şişmesin birde birden fazla iş yaptığından servis sınıfı yazıyorum. Paket kullanmayı sevmediğimden ve özelleştirmek benim için önemli olduğundan dolayı basitte olsa işimi görüyor. Genel olarak servis sınıfı bu şekildemi kullanıyor? Hatam var mı bakabilirmisiniz ?
appserviceprovider dosyası
public function register()
{
$this->app->bind(TabloServiceInterface::class, TabloService::class);
}
app\interface klasörü TabloServiceInterface.php
namespace App\Interfaces;
interface TabloServiceInterface {
public function bilgiler($sorgu);
public function kolonlar($array);
public function filtre(callable $ozelFiltre);
public function toplamFiltre();
public function sorguLimit();
public function siralama($kolon, $durum);
public function islem();
}
app\Services klasörü TabloService.php
namespace App\Services;
use App\Interfaces\TabloServiceInterface;
use Request;
class TabloService implements TabloServiceInterface
{
public $sorgu = null;
public $aranan = null;
public $siralama = null;
public $baslangic = 0;
public $bitis = 1;
public $toplam_sayi = 0;
public $toplam_sayi_filtre = 0;
public $arama_durum = false;
public function bilgiler($sorgu)
{
$this->sorgu = $sorgu;
$this->aranan = optional(Request::get('search')["value"]);
$this->siralama = optional(Request::get('order'));
$this->baslangic = (int)Request::get('start');
$this->bitis = (int)Request::get('length');
$this->toplam_sayi = $this->sorgu->count();
$this->toplam_sayi_filtre = $this->toplam_sayi;
return $this;
}
public function kolonlar($array)
{
if (!is_null($this->aranan['value'])) {
$i = 0;
foreach ($table_kolonlar as $kolon) {
if ($i == 0) {
$this->sorgu->where($kolon, 'LIKE', '%' . $this->aranan . '%');
} else {
$this->sorgu->orWhere($kolon, 'LIKE', '%' . $this->aranan . '%');
}
$i++;
$this->arama_durum = true;
}
}
return $this;
}
public function filtre(callable $ozelFiltre)
{
$this->arama_durum = true;
call_user_func($ozelFiltre, $this);
return $this;
}
public function toplamFiltre()
{
if ($this->arama_durum) {
$this->toplam_sayi_filtre = $this->sorgu->count();
}
return $this;
}
public function sorguLimit()
{
if ($this->bitis != -1) {
$this->sorgu->offset($this->baslangic)->limit($this->bitis);
}
return $this;
}
public function siralama($kolon, $durum)
{
# Sıralama
if (!is_null($this->siralama['value'])) {
# Tablode ilk numaralar olduğundan asıl column-1.
$this->sorgu->orderBy($table_kolonlar[Request::filter($this->siralama['0']['column'] - 1)], Request::filter($this->siralama['0']['dir']));
} else {
$this->sorgu->orderBy($kolon, $durum);
}
return $this;
}
public function islem()
{
$kayitlar = [
'toplam_sayi' => $this->toplam_sayi,
'toplam_sayi_filtre' => $this->toplam_sayi_filtre,
'kayitlar' => $this->sorgu->get(),
];
return (object)$kayitlar;
}
}
herhangi bir controller içinde ajax ile post ediyorum örnek datatable methoduna.
controller içinde en başında
use App\Interfaces\TabloServiceInterface;
use App\Services\TabloService;
protected $TabloService;
public function __construct(TabloServiceInterface $TabloService)
{
$this->TabloService = $TabloService;
}
sonrada ilgili methodda işlem yapıyorum.
public function datatable()
{
$model_sorgu = \App\Models\Uye::query();
$kayitlar = $this->TabloService
->bilgiler($model_sorgu)
->kolonlar(array('uye_adi', 'uye_soyadi', 'adres', 'adres_il', 'adres_ilce'))
->filtre(function ($ozelFiltre) {
if (!empty(Request::get('adres_il'))) {
$ozelFiltre->sorgu->where('adres_il', Request::get('adres_il'));
}
if (!empty(Request::get('adi'))) {
$ozelFiltre->sorgu->where('uye_adi', 'LIKE', '%' . Request::get('adi') . '%');
}
if (!empty(Request::get('soyadi'))) {
$ozelFiltre->sorgu->where('uye_soyadi', 'LIKE', '%' . Request::get('soyadi') . '%');
}
})
->sorguLimit()
->siralama('uye_id', 'DESC')
->toplamFiltre()
->islem();
# Kayıtlar: Düzenle
$data = [];
foreach ($kayitlar->kayitlar as $sira => $liste) {
$data_butonlar = '';
$data_liste = [];
# Sıra
$data_liste[] = $sira + 1;
# Adı
$data_liste[] = $liste->uye_adi . ' ' . $liste->uye_soyadi;
# Adres
$data_liste[] = $liste->adres . '<br />' . $liste->adresIl->il_adi . ' / ' . $liste->adresIlce->ilce_adi;
# İşlemler
$data_butonlar .= '<a class="kf_dugme kf_dugme_mavi" href="' . route('uyeler.edit', $liste->uye_id) . '" kf_ipucu="Düzenle" title="Düzenle"><i class="fa fa-pencil"></i></a>';
$data_butonlar .= '<a class="kf_yon_sag kf_dugme kf_dugme_kirmizi" href="javascript:void(0)" onclick="sil(' . $liste->uye_id . ');" kf_ipucu="Sil" title="Sil"><i class="fa fa-trash"></i></a>';
$data_liste[] = $data_butonlar;
$data[] = $data_liste;
}
# JSON
$output = array(
"draw" => intval(Request::get('draw')),
"recordsTotal" => $kayitlar->toplam_sayi,
"recordsFiltered" => $kayitlar->toplam_sayi_filtre,
"data" => $data
);
# Yazdır
echo json_encode($output);
}
Kullanım doğrumu ? çalışıyor bu şekilde. ilgili servis sınıfımda filtre adına callable yani çağrılabilir fonksiyon yazdım ilk defa kullandım. sorunsuz ama sizinde naciz fikirlerinize ihtiyacım var.