Reputation: 9024
Here is my Model Scope
public function scopeUser($query, $user_id)
{
return $query->where('user_id', $user_id)
->select(['_id', 'company_name', 'location', 'template_id', 'current_theme_id', 'site_url', 'site_path', 'addons']);
}
What i want is to us theme_id
as alias in place of current_theme_id
Tried already searching for solution but all of theme were based on DB queries.
Thanks
Upvotes: 1
Views: 1118
Reputation: 9
you can use this code.
public function scopeUser($query, $user_id)
{
return $query->where('user_id', $user_id)
->selectRaw('_id, company_name, location, template_id, current_theme_id as theme_id, site_url, site_path, addons');
}
Upvotes: 0
Reputation: 4020
In your comment:
I do not get any error but in the result there is no
current_theme_id
ortheme_id
present. the records are being returned fine
I doubt the column current_theme_id
is listed in your Model's $fillable
property. If it is already listed, try the below suggestion.
This might just be a work around, using selectRaw
method
public function scopeUser($query, $user_id)
{
return $query->where('user_id', $user_id)
->selectRaw('*, current_theme_id AS theme_id')
->select([
'_id', 'company_name', 'location',
'template_id', 'site_url', 'site_path',
'addons', 'theme_id'
]);
}
Upvotes: 0
Reputation: 5791
In your model, add:
protected $appends = ['theme_id']
public function getThemeIdAttribute()
{
return $this->current_theme_id;
}
Documentation: https://laravel.com/docs/5.1/eloquent-mutators#accessors-and-mutators
Upvotes: 0
Reputation: 716
Replace "current_theme_id" with "current_theme_id as theme_id" as given below.
public function scopeUser($query, $user_id)
{
return $query->where('user_id', $user_id)
->select(['_id', 'company_name', 'location', 'template_id', 'current_theme_id as theme_id', 'site_url', 'site_path', 'addons']);
}
Upvotes: 1