Laravel查询生成器 - 自定义响应

时间:2016-01-21 09:33:12

标签: php laravel laravel-5.1 laravel-query-builder

我有一个像这样的Laravel查询 -

public  function rearrangePhoto(Request $request)
{
    $ordering = $request->input('ordering');
    return UserSlidePhoto::where('user_id', Auth::id())
                  ->select(
                            'thumbnail_name',
                            'original_image_name',
                            'id'
                          )
                  ->orderBy('created_at', $ordering)
                  ->get();
}

和这样的模型 -

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserSlidePhoto extends Model
{
    protected $table="users_slides_photos";

    protected $fillable=['user_id','social_image_id','provider_key','provider_id','thumbnail_name'];
    public function users()
    {
        return $this->belongsTo('App\User')->withTimestamps();
    }
}

我得到这样的反应 -

[{
  "thumbnail_name": "2-hR1vt2ZOOesbpGv00MRbGDYyGAMEqU.jpg",
  "original_image_name": "qweqqwe.jpg",
  "id": 49
},
{
  "thumbnail_name": "2-xqiUrT2lJkEBQ0aA3MmZARmWlAq0BT.jpg",
  "original_image_name": "tyutjh.jpg",
  "id": 50
},
{
  "thumbnail_name": "2-s0zzdC275XqvrT2llBQ4h9sugadCHk.jpg",
  "original_image_name": "asdas.jpg",
  "id": 101
}]

我想得到这样的回应 -

[{
  "thumbnail_name": "base_url/2-hR1vt2ZOOesbpGv00MRbGDYyGAMEqU.jpg",
  "original_image_name": "qweqqwe.jpg",
  "id": 49
},
{
  "thumbnail_name": "base_url/2-xqiUrT2lJkEBQ0aA3MmZARmWlAq0BT.jpg",
  "original_image_name": "tyutjh.jpg",
  "id": 50
},
{
  "thumbnail_name": "base_url/2-s0zzdC275XqvrT2llBQ4h9sugadCHk.jpg",
  "original_image_name": "asdas.jpg",
  "id": 101
}]

所以我所做的就是这个 -

public  function rearrangePhoto(Request $request)
{
    $ordering = $request->input('ordering');
    $base_url = "base_url/";
    return UserSlidePhoto::where('user_id', Auth::id())
                  ->select(
                            $base_url . DB::raw('thumbnail_name'),
                            'original_image_name',
                            'id'
                          )
                  ->orderBy('created_at', $ordering)
                  ->get();
}

我收到了这个错误 -

enter image description here

有人可以帮忙吗?

提前感谢您的帮助....

3 个答案:

答案 0 :(得分:1)

尝试将您的选择更改为:

select(
    DB::raw('concat("'.$base_url.'/", thumbnail_name)'),
    'original_image_name',
    'id'
)

但我认为最好使用getAttribute。即使你使用JS,它也可以解决你的问题,因为在Laravel调用mutator的场景后面,你会得到你需要的东西。 所以你不需要做任何开销,只需写一个mutator。希望我的回答也可以帮到你。

答案 1 :(得分:0)

为此你需要使用mutator,在UserSlidePhoto模型中使用更多https://laravel.com/docs/5.1/eloquent-mutators#accessors-and-mutators.Use这个方法

public function getThumbnailNameAttribute($value)
{
    return $base_url .$value;
}

答案 2 :(得分:0)

您正在寻找的是Laravel访问器。

class UserSlidePhoto extends Model
{
    $base_url = "base_url/";
    protected $table="users_slides_photos";

    protected $fillable=['user_id','social_image_id','provider_key','provider_id','thumbnail_name'];
    public function users()
    {
        return $this->belongsTo('App\User')->withTimestamps();
    }

    public getThumbnailNameAttribute($value){
        return $base_url. $value;
    }
}