Reputation: 2948
Is possible with pure Eloquent uses, without php loops, add in a select statement a custom column and value to a relation just if the relation exists? I think that I don't know how to explain better, but I will try...
The json result from my current query(eloquent) is the following json:
[
{
"id": 5,
"user_id": 3,
"category_id": 2,
"city_id": 1,
"title": "Outro teste",
"body": "999999 sdf23asd f23asd32f1 as321f32as1d f1sdf",
"image_path": "",
"status": "ACTIVE",
"expires_at": "2015-03-20 04:44:53",
"popularity": 0,
"is_favorite": "true",
"category": {
"id": 2,
"name": "Categoria 2",
"status": "ACTIVE",
"color": "#0F0",
"parent_id": null,
"type": "INTEREST",
"slug": "categoria-2",
"icon_path": null
},
"favorite": []
},
{
"id": 4,
"user_id": 3,
"category_id": 3,
"city_id": 1,
"title": "Interesse de Teste 2",
"body": "2321fads132f123d sf 12sdf sd132",
"image_path": "",
"status": "ACTIVE",
"expires_at": "2015-03-21 02:34:53",
"popularity": 0,
"is_favorite": "true",
"category": {
"id": 3,
"name": "Subcategoria 1",
"status": "ACTIVE",
"color": "#00F",
"parent_id": 1,
"type": "INTEREST",
"slug": "subcategoria-1",
"icon_path": null
},
"favorite": [
{
"id": 1,
"user_id": 3,
"favorite_id": 4,
"type": "INTEREST",
"created_at": null,
"updated_at": null
}
]
}
]
In the first index from results is_favorite
field value must be "is_favorite": "false" because not exists()
the favorite
relation, and in the second index is_favorite value must be "is_favorite": "true" because exists the relation.
My code to get this current json result I think that can be better... I'm noob in Laravel 5.0:
<?php
$Interests = \Reverse\Interest::with('category')
->where('status', '<>', 'DELETED')
->take($limit)
->offset($offset);
if (isset($allFilters['user'])) {
$Interests->where('user_id', $allFilters['user']);
}
if (isset($allFilters['popularity'])) {
$Interests->orderBy('popularity', 'desc')
->orderBy('expires_at', 'asc');
}
if (isset($allFilters['favorites'])) {
if($Interests->with('favorite')->exists()) {
$Interests->select(DB::raw('*, "true" AS is_favorite'));
}
}
$responseContent = $Interests->get();
Reverse is my API namespace. The json return is made with application/json passed on Response Header:
<?php
// My Json Response Example
$Response = new \Illuminate\Http\Response();
$Response->header('Content-Type', 'application/json');
$Response->setContent($Interests->get());
return $Response;
Upvotes: 2
Views: 3211
Reputation: 2948
In Laravel to add a custom field use appends
attribute on your model, in this case:
<?php
protected $appends = ['is_favorite'];
And define an attribute accessor to your new attribute, in this case:
<?php
public function getIsFavoriteAttribute() {
return $this->favorite()
->where('user_id', '=', $this->user_id)
->exists();
}
The favorite is a relation in my case:
<?php
public function favorite()
{
return $this->hasMany('\Reverse\Favorite', 'favorite_id');
}
And finally my favorites
filter is activated when satisfy the following condition this way:
<?php
if (isset($allFilters['favorites']) and filter_var($allFilters['favorites'], FILTER_VALIDATE_BOOLEAN)) {
$Interests->has('favorite');
}
I think that exists a better way to write this code... Can you explain if you know?
Upvotes: 1