user2006734
user2006734

Reputation: 325

How can I redirect when it gets the exception in Laravel?

In laravel ,how should I code the exception?

 class DetailController extends Controller 
 { 

     public function index() {

         $user_id = Request::get('id'); 
         $user = User::find($user_id); 

         if($teacher) { 
             return view("detail",compact(["user"]));
         } 

         return view("top");      
         // "Undefined variable: AAA (View:/Users/user2006734/lesson/lesson/resources/views/top.blade.php)"    
     }
 }

When user_id is null,I want to make it redirect top page...

Using Route::redirect('/here', '/there', 301);

The Error does not occur but Redirected page is always blank.

Upvotes: 0

Views: 1306

Answers (2)

user8034901
user8034901

Reputation:

Route::redirect('/here', '/there', 301); is for redirectiong routes via web.php.

In your controller use

return Redirect::route('home');

to redirect to the route named home

Upvotes: 1

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

check Request has parameter , if not then redirect or render different view

 public function index() {
      if(Request::has('id')){

         $user_id = Request::get('id');
         $user = User::find($user_id);
         return view("detail", compact(["user"]));

      }else{
         return view("top");
      }
   }

Upvotes: 1

Related Questions