Passing array from AuthController to login mode in Laravel 5

I have a problem passing an array from Laravel AuthController to auth.login mode. What I'm trying to do is get the data from the news model and send it to the view. I know how to use eloquence to fetch data, going from controller to view is my problem as I can't see how / where Laravel renders the view.

+3


source to share


4 answers


Add an array as the second parameter to the view method when it is returned to the controller.

return view('greetings', ['name' => 'Victoria']); // in controller

      

Then, in your opinion, you should have access to the variable $name

, which should be equal toVictoria



var_dump($name); // in view

      

Read more in the documentation

+1


source


I solved it by passing the variable through the controller using the redirect method.



0


source


I'm not really sure what the purpose is here, but you said:

I don't see how / where Laravel renders the view.

So, to answer this:

Laravel's AuthController uses the AuthenticatesAndRegistersUsers attribute, which itself pulls several other traits, one being AuthenticatesUsers and the other being RegistersUsers.

In AuthenticatesUsers you will find a way like this:

/**
 * Show the application login form.
 *
 * @return \Illuminate\Http\Response
 */
public function showLoginForm()
{
    $view = property_exists($this, 'loginView')
                ? $this->loginView : 'auth.authenticate';

    if (view()->exists($view)) {
        return view($view);
    }

    return view('auth.login');
} 

      

A similar method is similar to the RegistersUsers method.

Here AuthController returns its views.

If you need to customize this behavior or return a view with some data, you can override these methods in your controller if this is really the best solution for your particular situation.

0


source


In the meantime, I have found a better way to do this.

You can override the showRegistrationForm () method in AuthController.php and pass the data you want to use in the view. Example:.

    public function showRegistrationForm()
    {
        $results = Model::all();
        return view('auth.register', ['results' => $results]);
    }
      

Run codeHide result


0


source







All Articles