F
F
freelancer0072016-05-04 18:52:26
Laravel
freelancer007, 2016-05-04 18:52:26

How to read data from session to view (Laravel)?

Good day everyone! I continue to get acquainted with Laravel, the question arose:
I try to manually authenticate the user using the attempt () method, everything works as it should, but if authorization fails, I redirect back and write an error message to the session
, it looks like this:

return redirect()->back()->with('error', 'Неверная пара логин пароль');

So how can I show this message to the user after the redirect? how to access session variable in view?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rustamka Vorontsov, 2016-05-04
@freelancer007

With validation

$user = \Auth::attempt(['email' => $request['email'], 'password' => $request['password']], true, true);

if(!$user) {
    $validator->errors()->add('email', 'Incorrect username or password.');
    return \Redirect::back()
                    ->withErrors($validator)
                    ->withInput();
}

Outputting an error to a template
@if ($errors->has())
     @foreach ($errors->all() as $error)
        {{ $error }}
     @endforeach
@endif

if just then
Output to template
if(Session::has('error'))
      {{ Session::get('error') }}
@endif

D
Dmitry Kuznetsov, 2016-05-04
@dima9595

https://laravel.ru/docs/v5/session
I did this: I created a view that is included in the main site template. In the view, I checked the session, and if it existed, I displayed it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question