Reputation: 3925
I have a index.blade.php page, I have a this code that shows alerts success or errors:
<!-- ALERTS -->
@if (isset($msg))
@if ($msg_type == 'error')
<div id="alert" class="alert alert-danger" role="alert">{{ $msg }}</div>
@else
<div id="alert" class="alert alert-success" role="alert">{{ $msg }}</div>
@endif
<script>setTimeout("document.getElementById('mensajecarrito').style.display='none'",6000);</script>
@endif
<!-- //ALERTS -->
This index have a form, and this form url POST to the post_designs url, and this is his controller method:
public function postDesign() {
if (Session::has('FUNCTION'))
$function = Session::get('FUNCTION');
if (Input::has('FUNCTION'))
$function = Input::get('FUNCTION');
if (isset($function))
{
switch($function)
{
case 'createDesign':
try
{
//do something good
$msg_type = 'success';
$msg = 'Design created successfully';
}
catch(FotiApiException $e)
{
$msg_type = 'error';
$msg = 'Unexpected error';
}
break;
}
}
return back()->with(array('msg' => $msg, 'msg_type' => $msg_type));
}
The problem is, that the $msg and $msg_type variables are empty when return back to my index.blade.php...
I don´t understand, my project is Laravel 5.2.3 and I have the correct route Middleware:
Route::group(['middleware' =>[ 'web']], function () {
# Index
Route::get('/',['as'=> 'index','uses' => 'WebController@getIndex']);
// # POSTS
Route::post('/post-design', ['as' => 'post_design', 'uses' => 'WebController@postDesign']);
});
Upvotes: 5
Views: 30979
Reputation: 31
Function
return back()->with('msg', $msg)
It doesn't return data to the usual variables and not to
request()->get("msg") or $msg
It returns data to
session()->get("msg")
and you can use it to get the variables you need.
Upvotes: 1
Reputation: 61
you can use withInput()
return back()->withInput(array('msg' => $msg, 'msg_type' => $msg_type));
the data saved in session if you want to use the data in blade. try use old('msg') or old('msg_type')
@if(old('msg'))
<p>msg:{{ old('msg') }}<p>
@endif
php
$msg = $request->old('msg');
Upvotes: 6
Reputation: 2684
If you want to send some extra GET parameters then simply follow below.
$previousUrl = app('url')->previous();
return redirect()->to($previousUrl.'?'. http_build_query(['varName'=>'varValue']));
It will redirect with yoururl?varName=varValue, hope it helps!!
Upvotes: 2
Reputation: 21
Instead of using
return back()->with('msg', $msg)->with('msg_type', $msg_type);
use
return back()->withInput(['msg' => $msg, 'msg_type' => $msg_type]);
Upvotes: 1
Reputation: 38
I'm new with laravel 5.2, about routes there is no need to but routes in
['middleware' =>[ 'web']]
scene all routes in laravel 5.2 goes in it , so try to remove it
and to return view with 2 values try to make it like that :
return back()->with('msg', $msg)->with('msg_type', $msg_type);
I hope that fixes the problem :)
Upvotes: 0