Laravel:如何设置有条件的自定义列?

时间:2015-03-19 04:48:31

标签: php mysql api laravel eloquent

可以使用纯粹的Eloquent用法,没有php循环,只要关系存在,就在select语句中添加一个自定义列和值给关系?我想我不知道如何更好地解释,但我会尝试......

我当前查询的json结果(eloquent)是以下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
            }
        ]
    }
]

在结果is_favorite的第一个索引中,字段值必须为" is_favorite":" false"因为不是exists() favorite关系,而在第二个索引中is_favorite值必须是" is_favorite":" true"因为存在关系。

我的代码获得当前的json结果我认为可以更好...我在Laravel 5.0中的noob

<?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是我的API命名空间。使用在响应标头上传递的application / json进行json返回:

<?php
// My Json Response Example
$Response = new \Illuminate\Http\Response();
$Response->header('Content-Type', 'application/json');
$Response->setContent($Interests->get());
return $Response;

1 个答案:

答案 0 :(得分:1)

在Laravel中,在模型中添加自定义字段使用appends属性,在这种情况下:

<?php
protected $appends = ['is_favorite'];

定义新属性的属性访问器,在本例中为:

<?php
public function getIsFavoriteAttribute() {
    return $this->favorite()
        ->where('user_id', '=', $this->user_id)
        ->exists();
}

在我的案例中,最喜欢的是一种关系:

<?php
public function favorite()
{
    return $this->hasMany('\Reverse\Favorite', 'favorite_id');
}

最后,当以这种方式满足以下条件时,我的favorites过滤器被激活:

<?php
if (isset($allFilters['favorites']) and filter_var($allFilters['favorites'], FILTER_VALIDATE_BOOLEAN)) {
    $Interests->has('favorite');
}

我认为存在编写此代码的更好方法......如果您知道,可以解释一下吗?