Merhabalar,
Elequent model ile bazı verileri listelemek için en iyi çözümü araştırıyorum. Çalışan kodu yazdım fakat en iyisi olduğuna emin değilim. Aşağıdaki kodu geliştirmeme yardımcı olabilir misiniz
Şu kodları kullanmak istemiyorum.
"$item = District::countryProvinceDistrict()->select('districts.*', 'countries.name as country_name', 'provinces.name as province_name')->get();"
"return $this->join('countries', 'countries.id', '=', 'districts.country_id')
->whereNull('countries.deleted_at')
->join('provinces', 'provinces.id', '=', 'districts.province_id')
->whereNull('provinces.deleted_at');"
Ayrıca soft delete kullanıyorum.
Country model - use SoftDeletes;
Province model - use SoftDeletes;
District model - use SoftDeletes;
District model :
protected $fillable = ['country_id', 'province_id', 'name', 'status'];
public $timestamps = false;
/**
* To allow soft deletes
*/
use SoftDeletes;
protected $dates = ['deleted_at'];
public function countries()
{
return $this->belongsTo(Country::class, 'country_id', 'id');
}
public function provinces()
{
return $this->belongsTo(Province::class, 'province_id', 'id');
}
public function scopeCountryProvinceDistrict()
{
return $this->join('countries', 'countries.id', '=', 'districts.country_id')
->whereNull('countries.deleted_at')
->join('provinces', 'provinces.id', '=', 'districts.province_id')
->whereNull('provinces.deleted_at');
}
public function getStatusTextAttribute()
{
return $this->attributes['status'] ? Lang::get('general.status.active') : Lang::get('general.status.passive');
}
District Controller:
/**
* @return mixed
*/
public function getAjaxData()
{
$item = District::countryProvinceDistrict()->select('districts.*', 'countries.name as country_name', 'provinces.name as province_name')->get();
return Datatables::of($item)
->addColumn('countries', function($item) {
return $item->country_name;
})
->addColumn('provinces', function($item) {
return $item->province_name;
})
->addColumn('actions', function($item) {
$route['edit'] = "admin.definitions.district.edit";
$route['delete'] = "admin.definitions.district.destroy";
return view('datatables.action', compact('item', 'route'))->render();
})
->editColumn('status', function($item) {
return $item->status_text;
})
->make(true);
}
Teşekkürler.