2017-02-25 5 views
1

私のコードは次のようである:Relationship Existenceのクエリでフィールドとオーダーを選択するにはどうすればいいですか? Laravel 5.3

public function getFavoriteStore($param) 
{ 
    $num = 20; 
    $q = $param['q']; 
    $location = $param['location']; 

    $result = $this->store_repository->whereHas('favorites', function ($query) use($q, $location) { 
     $query->select('stores.id', 'stores.name', 'stores.photo','stores.address') 
       ->where('stores.status', '=', 1) 
       ->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store'); 
     if($location) 
      $query->where('stores.address', 'like', "%$location%"); 

     if($q) { 
      $query->where('stores.name', 'like', "%$q%") 
        ->where('stores.address', 'like', "%$q%", 'or'); 
     } 
     $query->orderBy('favorites.updated_at', 'desc'); 
    })->paginate($num); 

    return $result; 
} 

それは

はまた、上記のクエリは、私が唯一のいくつかのフィールドを選択動作しないことにより、順序、

を動作しますが。私はすべてのフィールドに

を問い合わせ、結果表示をデバッグするとき、それは私を助けることができる誰もがあります

間違っが残っているようですか?

私はこのチュートリアルに従う:https://laravel.com/docs/5.3/eloquent-relationships#querying-relationship-existence

UPDATE

私のお気に入りのモデルは、このようなものです:

<?php 
namespace App\Models; 
use Illuminate\Database\Eloquent\Model; 
class Store extends Model 
{ 
    protected $fillable = ['name', 'address', 'phones', 'address', 'email']; 
    public function favorites() 
    { 
     return $this->morphMany(Favorite::class, 'favoritable'); 
    } 
} 

お気に入り:

<?php 
namespace App\Models; 
use Illuminate\Database\Eloquent\Model; 
class Favorite extends Model 
{ 
    protected $fillable = ['user_id', 'favoritable_id', 'favoritable_type']; 
    public function favoritable() 
    { 
     return $this->morphTo(); 
    } 
} 

マイストアモデルは、このようなものですテーブルにはフィールドiがありますD、USER_ID、favoritable_id、favoritable_type

店舗テーブルのフィールドID、名前、住所、電話、ADDRES、電子メール、写真

店やお気に入りの間の関係は、ID(storesテーブル)とfavoritable_id(お気に入りテーブル)です持っています

+0

Laravelデバッグバーを使用して、実際に生成されたSQLを確認し、そこから移動します。 –

+0

店舗とお気に入りの関係は? –

答えて

1

whereHas()だけなので、関係の有無を確認するために使用され、私はそれをテストしていない

$query = $this->store_repository 
    ->join('favorites', function ($join) { 
     $join->on('stores.id', '=', 'favorites.favoritable_id') 
      ->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store'); 
    }) 
    ->where('stores.status', '=', 1) 
    ->select('stores.id', 'stores.name', 'stores.photo','stores.address'); 

if($location) 
    $query->where('stores.address', 'like', "%$location%"); 

if($q) { 
    $query->where('stores.name', 'like', "%$q%") 
     ->where('stores.address', 'like', "%$q%", 'or'); 
} 

$result = $query->orderBy('favorites.updated_at', 'desc')->paginate($num); 

関連テーブルとの結果を注文するjoin()を使用する必要がありますが、私はかなりしていますそれが動作することを確認します。

+0

できます。ありがとう –

+0

しかし、私はあなたのコードを少し変更しました。このように: 'if($ location) $ query = $ query-> where( 'stores.address'、 'like'、 '%$ location%'); if($ q){ $ query = $ query-> where( 'stores.name'、 'like'、 '%$ q%') - >ここで( 'stores.address'、 'like' "%$ q%"、 "or '); } $ query = $ query-> orderBy( '​​favorites.updated_at'、 'desc') - > paginate($ num); '。 'Illuminate \ Database \ Eloquent \ Builderのオブジェクトを文字列に変換できませんでした。 ' –

+0

前の質問で指摘したように、それぞれの' where'に対して '$ query'変数を格納する必要はありません句。更新された回答をもう一度確認できますか? –

関連する問題