2017-11-14 11 views
1

こんにちは皆てみてください -

指数関数にLaravelをキャッチ私は質問をしました、そしてそれはおよそ試しています - 私は2つの貴様モデルから3つの変数を、きた機能をしました、キャッチ。問題は、モデルがDBにレコードを持っていないときに、ホームページからリダイレクトしようとしましたが、仕事をしませんでした!重要なのは、ビュー内にajax関数のリクエストがありました。ここに私のコード:このようなこの1つは試してみて、私もとIFとELSE使用

try 
     { 
     $events = Event::all(); 
     $competitions = Competition::orderBy('id', 'DESC')->get(); 
     $ultimos = Event::orderBy('id', 'DESC')->paginate(5); 

     if ($request->ajax()) { 
      return Response::json(\View::make('events.partials.last', array('ultimos' => $ultimos))->render()); 
     } 

     return View('events.index', compact('events', 'ultimos', 'competitions')); 
     } catch (\Exception $e) { 
      return redirect('/')->with('errors', 'Ha ocurrido un errror, lo sentimos'); 
     } 

キャッチである

、:

public function show_events(Request $request) 
    { 
     $events = Event::all(); 
     if($events) { 
     $competitions = Competition::orderBy('id', 'DESC')->get(); 
     $ultimos = Event::orderBy('id', 'DESC')->paginate(5); 

     if ($request->ajax()) { 
      return Response::json(\View::make('events.partials.last', array('ultimos' => $ultimos))->render()); 
     } 

     return View('events.index', compact('events', 'ultimos', 'competitions')); 

     } else { 
     return redirect('/')->with('errors', 'Ha ocurrido un errror, lo sentimos'); 
     } 
    } 

しかし、誰かが私を助けることができれば、本当に感謝しています!

+1

私はEvent :: all()がCollectionを返すと思います。それは空かもしれませんが、まだコレクションなのでヌルではありません...あなたのelseは動かないでしょう。 $ events-> count()> 0 – jcorry

+0

if($ events)の代わりにif(count($ events)> 0)を使うことができます。あなたの他の競争が働くでしょう。 – LetsCMS

+0

ありがとう、それは仕事です! –

答えて

1

あなたはこのようなコレクションのcount方法を使用することができますこの

try { 
    $events = Event::all(); 
    if($events->count() > 0) { 
     throw new Exception("Ha ocurrido un errror, lo sentimos"); 
    } 
    $competitions = Competition::orderBy('id', 'DESC')->get(); 
    $ultimos = Event::orderBy('id', 'DESC')->paginate(5); 

    if ($request->ajax()) { 
     return Response::json(\View::make('events.partials.last', array('ultimos' => $ultimos))->render()); 
    } 

    return View('events.index', compact('events', 'ultimos', 'competitions')); 
} catch (\Exception $e) { 
    return redirect('/')->with('errors', 'Ha ocurrido un errror, lo sentimos'); 
} 
1

if($ events)の代わりにif(count($ events)> 0)を使用できます。あなたの他の競争が働くでしょう。例外は、あなたがcount方法1つのより多くの時間などを使ってものを投げる場合を除いて、ここがキャッチしないようにするので、それは動作しませんtry catchについて

public function show_events(Request $request) 
    { 
     $events = Event::all(); 
     if($events->count() > 0) { 
     $competitions = Competition::orderBy('id', 'DESC')->get(); 
     $ultimos = Event::orderBy('id', 'DESC')->paginate(5); 

     if ($request->ajax()) { 
      return Response::json(\View::make('events.partials.last', array('ultimos' => $ultimos))->render()); 
     } 

     return View('events.index', compact('events', 'ultimos', 'competitions')); 

     } else { 
     return redirect('/')->with('errors', 'Ha ocurrido un errror, lo sentimos'); 
     } 
    } 

関連する問題