Data is not being passed with redirect - php

Bellow is a code of redirect to route. I am trying to get success variable in view using $success but it is giving error "Undefined variable: success". I also tried passing data in array format to with method. How can I correct it?
if ($data->save())
return redirect('/cases/ppost/notification')->with('success', 'Your case has been posted.')->with('caseNo', $data->id);

According to the documentation the redirect()->with() method does not assign any data to your view, but flashes it to the session:
http://laravel.com/docs/5.0/responses#redirects
Returning A Redirect With Flash Data
Redirecting to a new URL and flashing data to the session are
typically done at the same time. So, for convenience, you may create a
RedirectResponse instance and flash data to the session in a single
method chain:
return redirect('user/login')->with('message', 'Login Failed');
This means you will have to access it as such in your view. That would probabaly look something like this:
#if (Session::has('message'))
<div class="alert alert-info">{{ Session::get('message') }}</div>
#endif

wouldn't that be ??
Redirect::route('/cases/ppost/notification')->with( array('success'=>'Your case has been posted.','caseNo'=>$data->id)) ;

Related

Laravel adding an alert to a view

I am trying to add an alert/warning to a view but displaying nothing. I can get my alerts to work with a redirect but wondering how to do it with a view that passes data. Is there a way to pass an alert to a view or is there a "Correct" way of doing this?
$form = new Form();
$form->storeRequest($request);
$form->saveJson();
$form->loadForm($request->cuid, $request->cubaseName);
return view('layouts.pages.form', ['form'=>$form])->with('success', 'Form has been saved.');
//return redirect()->back()->with('success', 'Saved!'); <--this would work if i wasn't passing data
All Framework use a special method to send message from controller known as Flash message
In your controller
$request->session()->flash('success', 'Form has been saved');
And to access it on view
#if($message = Session::get("success"))
<h3 class="text-center text-success">{{$message}}</h3>
#endif
Laravel -> Http Session -> Flash Data
If you are looking for a neat way of doing something like bootstrap alerts, you should consider the laracasts/flash package by Jeffrey Way.
Documentation can be found here

Laravel pass array to view using with

I have a problem with with method in view function. I would like to pass data variable and status. I do that:
return view('user.blogsettings')
->with(array('data' => $this->_data, 'status' => 'Save'));
Data item from array works ok, but status is not
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
There is no message.
The with() method on View doesn't send to session, it passes it as variable to the view so you can access it as $status instead of session('status')
If you want to explicitly add it to session then use session(['status' => 'Save']) and this would be flashed to the session for the ongoing request.
You may check Laravel's doc Passing Data to view for further information
I hope this is useful.

Laravel 5.1 flash message handling

In Laravel 5.0 and 4.* there was this option to check if the Session had flash_messages:
Controller:
public function store(Request $request) {
Session::flash('flash_message', 'Success');
return redirect('somepage');
}
And then you could call this in your layout:
#if (Session::has('flash_message'))
<p>{{ Session::get('flash_message') }}</p>
#endif
This worked perfectly fine, however in Laravel 5.1 it changed. Now it's stored in the request variable. So, I changed it in the controller to:
public function store(Request $request) {
$request->session()->flash('flash_message', 'Success');
return redirect('somepage');
}
And I call this in the layout:
#if($request->session()->has('flash_message'))
<p>{{ $request->session()->get('flash_message') }}</p>
#endif
I'm doing exactly what is stated in the Laravel 5.1 docs:
Determining If An Item Exists In The Session
The has method may be used to check if an item exists in the session.
This method will return true if the item exists:
if ($request->session()->has('users')) { // }
Now this works perfectly fine for store method now, because I only added a flash_message there, but if I want to access another method like index, create, edit, etc. it shows this error:
ErrorException
Undefined variable: request
I understand that the request variable is not declared in the other methods and I can workaround this if-statement using isset($request).
Is there a better solution to check if my flash_message is set in the $request variable like in the earlier versions of Laravel?
It sounds like you're using controllers generated by Laravel.
If you look at the method definitions for store and update you'll find the parameter Request $request. By including this Laravel automatically creates a Request instance for you.
In order to access this in other methods you have three options:
Add the same parameter to any of the other method definitions and Laravel will generate it for you in the same way.
Create a constructor function and have it assign a Request instance to an attribute.
public function __construct(Request $request)
{
$this->request = $request;
}
Declare use Session; before the class definition to make the Session class accessible in your namespace.
You can still use the old method :) . latest laravel just added new ways to achieve that goal.here goes the sample code to display error messages which are passed as flash message into a view page .
Sample code
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<h4>Success</h4>
#if(is_array($message))
#foreach ($message as $m)
{{ $m }}
#endforeach
#else
{{ $message }}
#endif
</div>
#endif

Laravel Passing variable not working

Hello People here is my code i have used in controller...
public function bulk()
{
return View::make('bulk')->with('message','hii there');
}
my route file contains...
Route::get('bulk',array('uses'=>'HomeController#bulk'))->before('auth');
In my view Iam testing it by ...
#if(Session::has('message'))
Present
#else
not Present
#endif
The page is making a view with the message 'not Present' why is it??
I even tried
return Redirect::to('bulk')->with('message','hii there');
I get an erro mesage on Console
mypro/public/bulk net::ERR_TOO_MANY_REDIRECTS
What could be the problem?? is there any issues with name?? I tried this method earlier which worked fine for me.... :(
Iam using Blade Template..
You are confusing Redirect and View. You use Session::get to access variables passed to redirects. For views (as in your case), with will pass an simple PHP variable into your view. So your check should be:
#if(isset($message))
{{{ $message }}}
#else
No message!
#endif
Read more here
As per the docs, you need to access the "with" value in the view using a PHP variable, it's not passed via the session. In your case, that would be $message. If you want to use the session, you should use Session::flash() or Session::put().

Passing URL to Views in Laravel

Here is my controller code:
$view = View::make('homepage');
$loglink = URL::route('account.login');
$view->loglink = $loglink;
return $view;
Here is how I recv it in view:
<a href="{{ $loglink }}" class="red">
I get an error saying:
Undefined variable: loglink (View:
C:\BottleBookings\server\app\views\partials\header.blade.php)
Any problem with the way I am sending or recving it?
Thats not how view creation works. Just return your view and pass the variables you want to use with the with() function in the same command. Create your view like this:
$loglink = URL::route('account.login');
return View::make('homepage')->with('loglink', $loglink);
Or even shorter:
return View::make('homepage')->with('loglink', URL::route('account.login'));
Also, you don't really have to pass the link URLs, it's perfectly fine to generate them where they are needed in the view:
<a href="{{ URL::route('account.login') }}" class="red">
why you complicate. just put the datas in array and pass it to view.
Controller
$data['loglink'] = URL::route('account.login');
return View::make('homepage',$data);
p.s. your loglink variable will be available to only homepage view. NOT on header page.
if you want to pass the loglink to header, then you have to pass it as View::composer or View::share or where you are including the file. any one of the three will do.

Categories