2016-10-06 6 views
1

URLに応じて特定のページにコンテンツを表示したい。 URLをmypage.com/menues/{city}と呼んで、特定のcitiyのレストランをすべて表示したいと思います。Laravel 5追加クエリの範囲

Route::get('menues/{city?}', '[email protected]'); 

そして、これのcontroler:

私はこのルートを持っている

public function scopeNowPublished($query) { 
    $zero = Carbon::today()->addHours(23)->addMinutes(59)->addSeconds(59); 
    $query->whereBetween('published_at',[Carbon::today(),$zero])->orderBy('published_at','desc'); 
} 

QueryExceptionは上記のコードでは:私は私の記事のモデルでは、この範囲を定義し

public function menue($city = null) { 

    if ($city) { 
    $restaurants = User::with(['articles' => function ($q){ 
     $q->nowpublished()->where('city', '=', 'Heilbronn'); 
    }])->get(); 
    } else { 
    $restaurants = User::with(['articles' => function ($q){ 
     $q->nowpublished(); 
    }])->get(); 
    } 

    return view('pages.menues')->withRestaurants($restaurants)        
          ->withCity($city); 


} 

スローされる。

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'city' in 'where clause' (SQL: select * from `articles` where `articles`.`user_id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) and `published_at` between 2016-10-06 00:00:00 and 2016-10-06 23:59:59 and `city` = Heilbronn order by `published_at` desc) 

私はチェーンは残念ながら、私はこの問題を解決する方法が分からないwhere句...

で)(nowpublishedことができるかどうかわかりません。

答えて

1

記事にフィールドcityがありますか?

$restaurants = User::with(['articles' => function ($q){ 
    $q->nowpublished(); 
}]) 
->where('city', 'Heilbronn') 
->get(); 
+0

ありがとうございました! – Mamulasa

0

エラーからのクエリを見てみましょう:私は推測している

Column not found: 1054 Unknown column 'city' in 'where clause' 

(SQL: 
    select * 
    from `articles` 
    where `articles`.`user_id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) 
    and `published_at` between 2016-10-06 00:00:00 and 2016-10-06 23:59:59 
    and `city` = Heilbronn 
    order by `published_at` desc 
) 

を表し、私はUserはそうソリューションはwhere方法は関係クエリを呼び出す取得することである持っていると思いますPHPコードのこの部分は:

'articles' => function ($q){ 
    $q->nowpublished()->where('city', '=', 'Heilbronn'); 
} 

それはテーブルの上にarticlescityを探しています。

articlesテーブルにはcityという列がありますか?

あなたはあなたの質問で述べている:

I want to show all restaurants for a specific citiy

をしかし、あなたも任意のRestaurantタイプのエンティティを問い合わせるない、私が見るすべてはUserArticleです。問題のクエリの一部であるRestaturantが不足している可能性があります。あなたのDBスキーマがわからないので、言うのは難しいです!私はその都市のURLを呼び出すと、私は

if ($city) { 
    $restaurants = User::with(['articles' => function ($q){ 
     $q->nowpublished(); 
    }])->where('city', '=', $city)->get(); 
    } else { 
    $restaurants = User::with(['articles' => function ($q){ 
     $q->nowpublished(); 
    }])->get(); 
    } 

に自分のコードを更新したあなたが尋ねた他の質問...

public function menue($city = null) 
{ 
    // prep query so we don't write it twice 
    $query = User::with(['articles' => function ($q){ 
     $q->nowpublished(); 
    }]); 

    // Check city exists with users 
    $city = User::where('city', $city)->get(); 

    // if it does then apply where 
    if ($city) { 
    $query->where('city', '=', $city)->get(); 
    } 

    // get results from query 
    $restaurants = $query->get(); 

    // return view with query results 
    return view('pages.menues') 
    ->withRestaurants($restaurants) 
    ->withCity($city); 

} 
+0

ヒントありがとうございます。記事には市が含まれていません。このcolmunはUserに属します。 – Mamulasa

+0

@Mamulasaよろしくお願いします! – haakym

+0

上記の新しい質問のアドバイスはありますか? – Mamulasa

0

に答えるために

EDIT

私のDBの列にexcistすると、結果は表示されず、ページは空です。都市がDBエントリと一致しない場合は、どのようにしてすべてのエントリを表示できますか?

+0

あなたは本当にあなたの質問を更新するか、新しい質問をするべきです - これは答えです – haakym

関連する問題