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

Laravel: how to set a custom column with conditions?

是否可以在没有 php 循环的情况下使用纯 Eloquent,在 select 语句中添加自定义列和关系值(如果关系存在)?我想我不知道如何更好地解释,但我会尝试...

我当前查询 (eloquent) 的 json 结果如下 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 中的菜鸟:

<?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 命名空间。 json return 是通过 application/json 传递给 Response Header:

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

在 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');
}

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