Laravel with input respond - php

I am trying to achieve callback message on redirect if you done something. I found that it can be passed like this (in Controller):
return redirect()
->route('users')
->withInput()->with('status', 'Something Updated!');
How it can be achieved in other end after being redirected?
My first question on slack - ty, guys :)

On the blade file, you can get the value in with using session. Here is the example from the official docs
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif

Related

Laravel 7: Validation error message is not showing using key

I want to show validation errors right next to the field.
Therefore I am using this the #error directive.
#error('service_date')
<div class="error">{{ $message }}</div>
#enderror
But the error only shows up when iterating over all of them.
#if($errors->any())
{{ implode('', $errors->all('<div>:key - :message</div>')) }}
#endif
Using the above method, I can see that the key is correct. The output will be as follows:
service_date - The service date is not a valid date.
Other errors in the same form get displayed correctly. Why is behaving like this and how can I fix it?

Laravel, get data from controller for display purposes

I'd like to display data calculated in controller in a blade view based on data provided by a user in form . For now I'm doing this by:
//View xyz.blade.php
<div class="card">
<div class="card-header">Add link</div>
<div class="card-block">
{{ Form::open(array('url' => 'getSimilar', 'method' => 'GET')) }}
{{ Form::token() }}
<div class="form-group">
{{ Form::label('url', 'url:') }}
{{ Form::text('url', '') }}
</div>
{{ Form::submit('Get') }}
{{ Form::close() }}
</div>
#if( ! empty($similar))
<div class="card-block">
#foreach ($similar as $item)
{{$item->title}} <br/>
#endforeach
</div>
#endif
</div>
//Controller
public function getSimilar(){
..
return View('xyz', ['similar' => $found]);
}
The above code works as expected. I can provide a url and after clicking "Get" I can see a list of found items below the form. However, something tells me that this is not the right way to do this since the entire page will refresh. Am I right (and so the above code is bad)? If so, is there any build in feature to display fetched data without refreshing? I searched on the official Laravel-form page but I did not find anything.
As far as I could understand your question.
The code seems to be ok, but the logic you've created may not be possible without a page refresh, because the user interaction depends the server response which requires a new reload.
You can craft another interactive action without a page refresh using an AJAX so when the user clicks the button he gets the result from the server you then display the results in page. Because the AJAX Request/Response happens behind the scenes for the user it gives an impression the page didn't reload which may be what you want.

No session variables show up in view - Laravel 5.1

I am trying to display errors on my login page using the Session::flash() method in Laravel 5.1.
In my view I am using:
#if($errors->has())
<div class="alert alert-danger">
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
</div>
#endif
And in my controller:
return Redirect::route('login')->withErrors($validator)->withInput();
I am not getting anything from the Input::old() method when getting redirected with errors to this page.
The $errors variable is empty on the reload.
If I dd($validator); right before the return Redirect::route..., the $validator has data in it:
But when the page loads, nothing happens.
I can try Session::put but still, nothing happens when getting the view.
What could be wrong? Is there maybe something in composer or php artisan that may be able to reset this issue?
To anyone that has this problem, how I solved it was changing the .env file from SESSION_DRIVER=array to SESSION_DRIVER=file
composer and php artisan have got nothing to do with the Session.
You are not seeing the Session::flash message is because you are not retrieving it.
In order to retrieve a message from Session::flash, you need to use the following:
#if(Session::has('your_flash_message_key'))
<div class="alert alert-danger">
<div>{{ Session::get('your_flash_message_key') }}</div>
</div>
#endif
Coming to your view code on $errors, try replacing the #if with this
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
In Laravel's official documentation on displaying validation error message(s), it shows the above code only.

How can I add a success flash message for the password reset functionality of Laravel?

I'm using Laravel 5.1 and the built-in auth functionality. When testing it, however, I noticed there is no "Success" message when a user fills out the forgotten password form, the page simply refreshes, although the form does work.
How can I set a success variable for the forgotten password tool? I can't find the method that controls this anywhere.
Thanks
If you’re using Laravel’s built-in password reset functionality, then it does have a success message. You can see it here: https://github.com/laravel/framework/blob/5.1/src/Illuminate/Foundation/Auth/ResetsPasswords.php#L95
You merely need to listen for it in your view and display it if it’s there:
#if (session('status'))
<p class="alert alert-success">{{ session('status') }}</p>
#endif
This will display the default message, which is:
Your password has been reset!
If you want to change this message, just change the relevant line in resources/lang/en/passwords.php.
Simply paste this function in your ResetPasswordController
protected function sendResetResponse(Request $request, $response)
{
return redirect($this->redirectPath())
->with('success', 'Password changed successfully.');
}
And to get the message in your blade view, use this code
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif

Showing validation errors to view

I want to show the validation errors to the users in comma seperated
i.e.,
The username field is required, The password field is required
So far i can able to send the validation error messages to the view like this
$validation->messages()
But the only thing i can't able to do
#if(Session::has('Message'))
<p class="alert">{{ Session::get('Message') }}</p>
#endif
or
{{ $errors->first('username', '<div class="error">:message</div>') }}
The only thing i can do is to pass the messages as normal text.
So, How can i pass the validation messages to a view by plain text (rather than array or object)
Update :
I mean to say i can do any works only in controller and not in view
in controller:
return implode(',',$validation->errors()->all());

Categories