Laravel Passing variable not working - php

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().

Related

Laravel redirecting back using withErrors but can't display error

So I have a user interface where users can upload videos. I have a validation rule to prevent too big videos. But if the video is even bigger than the post_max_size is giving an error screen before reaching the formrequest class, and I don't want that. I just want to inform the user, that ,,Hello, this video is too big" . So I went to my Handler.php class and made changes to the render function like this:
public function render($request, Throwable $exception)
{
if ($exception instanceof PostTooLargeException)
{
return back()->withErrors(['message' => 'Too big file']);
}
return parent::render($request, $exception);
}
It returns me back to the previous url but fails to inform about error, and that's my problem.
I tried
#error('message')
and
#foreach($errors as $error)
{{$error}}
#endforeach
even
session->get('message');
but non of these helped, it seems my error message is not there. I would be happy to have any advice on what I'm doing wrong.
withErrors already contain error and all you have to do is put a message only
return back()->withError('Too big file');
and display in blade like
session('error')
Edited
my working data
return redirect()->back()->withError('Data is used in another page, delete is not possibe!');
and in blade file
#if (Session('error'))
<p class="text-danger">{{ session('error') }}</p>
#endif
I figured it out. I couldn't access the session from Handler.php.
Here is a solution for the problem: Unable to access sessions in Laravel app\Exceptions\Handler.php
But I did it another way. I've added a nullable parameter to my route, and when redirecting -if it's not already there-, I've added the error, passed it to my blade, and it works now, but note that this wont work for every occurence for an error, only on the interface / interfaces where you add this functionality.

How do I send a form but stay on the same page with phalcon?

In the form I have to define a handler like admin/login. Then it changes the page when I send it. I also tried using public function indexAction() handler and only using the controller admin without a handler, but then nothing happens.
Any help would be greatly appreciated.
Edit:
I wanted to have a error message but it was not so big a site that i wanted to make a form class.
The way i eventually fixed it was the following:
First i use $this->view->pick('admin/index'); in the handler to select my old view.
Next i send a variable like a error message with $this->view->setVars(['username' => $user->name,]); in the handler.
Last i used it with volt in the previous view views/admin/index.phtml like this {{ username }}
If you are using a form object you can then use the following code in your volt template
{{ form('myform', 'method':'post', 'role': 'form') }}
This will post your data to the same controller/action that invoked the form. From then you will be able to handle the posted data using valid() in the form as well as request->isPost()

How to access cookie from view in PHP Laravel 5

I can access cookie in controller then pass it to view
//HomeController.php
public function index(Request $request)
{
$name = Cookie::get('name');
return view('index', ['name'=> $name]);
}
But I want to write a small control (widget) that can fetch data from cookie without concern of parent controller. For example, header, footer widgets could fetch its own data without main page controller knowing which data is needed.
I can query the data from database by using View Composer. But, how can I access data from view in the request cookie ?
Using static function with defining namespace and etc is a not safe.
Cuz maybe in next versions of framework this namespace can change.
It's better to use helper functions.
{{ request()->cookie('laravel_session') }}
or
{{ cookie('laravel_session') }}
Tested on working app with Laravel 5.2
You can use {{ Cookie::get('laravel_session') }} to print out the cookie inside your view.
You can use
{{\Illuminate\Support\Facades\Cookie::get('laravel_session')}}
in a blade template
You can use:
$response = new \Illuminate\Http\Response(view('your_view'));
$response->withCookie(cookie('cookieName' , 'cookieValue' , expire));
return $response;
or
\Cookie::queue('cookieName', 'cookieValue', expire);
return view('your_view');
I've used both of them.

How to access session in blade before request

I have an admin template which I builded with blade templates,including one another.The user request is extending the main blade and returning only the content.
However in the template I have stuff like - user messages(count),theme options etc.
These things must be saved in the user session.Cookies are not an option.
The question is how to do it in the best possible option.
I'm thinking of gettind the request in middleware and accessing the session there.After that I must pass the data to the blade templates (not the final extending one).
Whats your opinion ?! Thanks!
If I understand correctly, you have a main layout blade template that is later extended by the user view returned by the controller.
No additional code like you described is needed. Both user and layout templates are processed after controller action is executed and both have access to user session via session() helper and user object via Auth::user().
So the following sample code should work for you :
// SomeController
public function someAction() {
return return response()->view('user');
}
// main.blade.php
#if (Auth::check())
Show this text only to authenticated users
#endif
Value of session parameter is {{ session('parameter_name') }}
// user.blade.php
#extends('main')

Data is not being passed with redirect

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)) ;

Categories