Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel redirect inside of Trait

Tags:

php

laravel

trait foo{

    public function bar()
    {
         redirect('/'); //not working
    }
}

use Traits;

class DonController extend Controller{
     use Traits\foo;

     $this->bar();
     redirect('/'); //working
}

I'm new in Laravel, I need to create a redirect method inside of trait

However I have problem to do it inside of trait. Anyone know how to solve this?

like image 646
Benjamin W Avatar asked Oct 23 '25 19:10

Benjamin W


1 Answers

Only a controller method can return a redirect object to go to another page. You can however invoke the ->send() method of a redirect object to send the redirect directly from anywhere in your application.

trait foo {

    public function bar()
    {
         redirect('/')->send();
    }
}
like image 77
Jerodev Avatar answered Oct 25 '25 10:10

Jerodev