如何使用自定义方法返回响应json?

时间:2017-09-15 01:01:06

标签: php json laravel orm

我在酒店模特

中有自定义功能
public function images()
{
    return $this->hasMany('App\HotelImage', 'hotel_id', 'id');
}
public function translate(){
    $hotel =  $this->translates->where('lang',app()->getLocale())->first();
    return $hotel ? $hotel : $this;
}

在我的api控制器中我返回响应() - > json($ hotels);

1. $hotels =  Hotel::with('images')->where(function($query) use($q){
            foreach($q as $price){
                $query->orWhereBetween( 'price', explode(',',$price) );
            }
        })->get();

2. $hotels =  Hotel::with('translate')->where(function($query) use($q){
            foreach($q as $price){
                $query->orWhereBetween( 'price', explode(',',$price) );
            }
        })->get();

1 - 正在运作 输出 id:2, images:[{id: 3, name: "1505464164_5.jpg", size: 270617, hotel_id: 2}] name:"test2" phone:"556464" place:"Baku, Azerbaijan"

2 - 无效输出 Call to undefined method Illuminate\Database\Query\Builder::addEagerConstraints()

0 个答案:

没有答案