Возможно ли с помощью laravel получить свойство модели при использовании метода $queryBuilder->get()
?laravel map model properties
Моя база данных имеет странные именования (старое приложение), и я написал один get*Attribute()
метод для каждого поля, так что я могу получить доступ к нему, делая $model->prettyName
вместо $model->weird_Convention_Name
, но когда я использую $queryBuilder->get()
, я получаю базу данных поле, а не свойства модели.
Есть ли способ сделать это?
EDIT
Вот пример кода:
Модель
<?php
namespace Model\Day;
use Illuminate\Database\Eloquent\Builder;
/**
* Class Incomplete
* @package Model
* @subpackage Day
* @property string $alarm
* @property string $status
* @property string $mark1
* @property int $dayRate
* @property bool $isHalfDay
* @property bool $noPause
* @property bool $isTracked
* @property bool $noTime
* @property string $dayType
* @property string $shortDay
* @property \DateTime $date
* @property \DateTime $startDay
* @property \DateTime $startBreak
* @property \DateTime $endBreak
* @property \DateTime $startBreak2
* @property \DateTime $endBreak2
* @property \DateTime $endDay
* @property string $statusDescription
* @property string $subtype
* @property string $subcode
* @property string $description
* @property int $locationId
* @property string $locationDes
*
* @method \Eloquent currentUser()
*/
class Incomplete extends \Eloquent
{
/**
* The database table used by the model.
* @var string
*/
protected $table = 'vlogger_pending';
/**
* The primary key for the model.
* @var string
*/
protected $primaryKey = null;
/**
* The sequence name used for incrementing
* @var string
*/
protected $sequence = null;
/**
* Indicates if the IDs are auto-incrementing.
* @var bool
*/
public $incrementing = false;
/**
* Indicates if the model should be timestamped.
* @var bool
*/
public $timestamps = false;
/***********************************************************************
* SCOPES
***********************************************************************/
/**
* Add a where statement for current logged in user
* @param Builder $query
* @return Builder
*/
public function scopeCurrentUser(Builder $query)
{
return $query->where('usr_id', '=', \Auth::user()->id);
}
/***********************************************************************
* ACCESSORS
***********************************************************************/
public function getAlarmAttribute()
{
return $this->attributes['alarm'];
}
public function getStatusAttribute()
{
return $this->attributes['status'];
}
public function getMark1Attribute()
{
return $this->attributes['mark1'];
}
public function getDayRateAttribute()
{
return (int)$this->attributes['day_rate'];
}
public function getIsHalfDayAttribute()
{
return !!$this->attributes['half_day'];
}
public function getNoPauseAttribute()
{
return !!$this->attributes['no_pause'];
}
public function getIsTrackedAttribute()
{
return !!$this->attributes['tracked'];
}
public function getNoTimeAttribute()
{
return !!$this->attributes['no_hours'];
}
public function getDayTypeAttribute()
{
return $this->attributes['day_type'];
}
public function getShortDayAttribute()
{
return $this->attributes['dd'];
}
public function getDateAttribute()
{
$date = $this->attributes['day'];
if (null !== $date) {
$date = \DateTime::createFromFormat('U', strtotime($date));
}
return $date;
}
public function getStartDayAttribute()
{
$date = $this->attributes['dstart'];
if (null !== $date) {
$date = \DateTime::createFromFormat('U', strtotime($date));
}
return $date;
}
public function getStartBreakAttribute()
{
$date = $this->attributes['pstart'];
if (null !== $date) {
$date = \DateTime::createFromFormat('U', strtotime($date));
}
return $date;
}
public function getEndBreakAttribute()
{
$date = $this->attributes['pend'];
if (null !== $date) {
$date = \DateTime::createFromFormat('U', strtotime($date));
}
return $date;
}
public function getStartBreak2Attribute()
{
$date = $this->attributes['s_pstart2'];
if (null !== $date) {
$date = \DateTime::createFromFormat('U', strtotime($date));
}
return $date;
}
public function getEndBreak2Attribute()
{
$date = $this->attributes['s_pend2'];
if (null !== $date) {
$date = \DateTime::createFromFormat('U', strtotime($date));
}
return $date;
}
public function getEndDayAttribute()
{
$date = $this->attributes['dend'];
if (null !== $date) {
$date = \DateTime::createFromFormat('U', strtotime($date));
}
return $date;
}
public function getStatusDescriptionAttribute()
{
return $this->attributes['status_des'];
}
public function getSubtypeAttribute()
{
return $this->attributes['sub_type'];
}
public function getSubcodeAttribute()
{
return $this->attributes['sub_code'];
}
public function getDescriptionAttribute()
{
return $this->attributes['des'];
}
public function getLocationIdAttribute()
{
return (int)$this->attributes['location'];
}
public function getLocationDesAttribute()
{
return $this->attributes['location_des'];
}
/***********************************************************************
* MUTATORS
***********************************************************************/
public function setAlarmAttribute($value)
{
$this->attributes['alarm'] = $value;
return $this;
}
public function setStatusAttribute($value)
{
$this->attributes['status'] = $value;
return $this;
}
public function setMark1Attribute($value)
{
$this->attributes['mark1'] = $value;
return $this;
}
public function setDayRateAttribute($value)
{
$this->attributes['day_rate'] = $value;
return $this;
}
public function setIsHalfDayAttribute($value)
{
$this->attributes['half_day'] = $value;
return $this;
}
public function setNoPauseAttribute($value)
{
$this->attributes['no_pause'] = $value;
return $this;
}
public function setIsTrackedAttribute($value)
{
$this->attributes['tracked'] = $value;
return $this;
}
public function setNoTimeAttribute($value)
{
$this->attributes['no_hours'] = $value;
return $this;
}
public function setDayTypeAttribute($value)
{
$this->attributes['day_type'] = $value;
return $this;
}
public function setShortDayAttribute($value)
{
$this->attributes['dd'] = $value;
return $this;
}
public function setDateAttribute($value)
{
$this->attributes['day'] = $value;
return $this;
}
public function setStartDayAttribute($value)
{
$this->attributes['dstart'] = $value;
return $this;
}
public function setStartBreakAttribute($value)
{
$this->attributes['pstart'] = $value;
return $this;
}
public function setEndBreakAttribute($value)
{
$this->attributes['pend'] = $value;
return $this;
}
public function setStartBreak2Attribute($value)
{
$this->attributes['s_pstart2'] = $value;
return $this;
}
public function setEndBreak2Attribute($value)
{
$this->attributes['s_pend2'] = $value;
return $this;
}
public function setEndDayAttribute($value)
{
$this->attributes['dend'] = $value;
return $this;
}
public function setStatusDescriptionAttribute($value)
{
$this->attributes['status_des'] = $value;
return $this;
}
public function setSubtypeAttribute($value)
{
$this->attributes['sub_type'] = $value;
return $this;
}
public function setSubcodeAttribute($value)
{
$this->attributes['sub_code'] = $value;
return $this;
}
public function setDescriptionAttribute($value)
{
$this->attributes['des'] = $value;
return $this;
}
public function setLocationIdAttribute($value)
{
$this->attributes['location'] = $value;
return $this;
}
public function setLocationDesAttribute($value)
{
$this->attributes['location_des'] = $value;
return $this;
}
}
контроллер
/**
* Get pending report for current logged user
* @return \Illuminate\Http\JsonResponse
*/
public function pendingReport()
{
return Response::json(Day\Incomplete::currentUser()->get());
}
Почему вы хотите использовать конструктор запросов, когда модели доступны? – Dencker
Когда вы используете 'get()' все данные, получаемые как есть. Но, когда вы пытаетесь получить его '$ model-> prettyName', laravel использует мутатор, и вы получаете значение' PrettY_Name' – xAoc
Я не использую querybuilder, но Eloquent, queryBuilder используется с областями, я обновляю свой вопрос – kitensei