Amrinder Singh
Amrinder Singh

Reputation: 5502

How to pass variable with response view in Laravel 5.1?

I am trying to pass variables with response view in Laravel, but it is not allowing me to do so. Here is what I have tried so far:

return response(view('home'))->with('name','rocky');

the above method doesn't work,

the second one I tried is as follow:

return response(view('home'), ['name','rocky']);

but I don't know how to use the above one?

NOTE: I don't want to use view::make method, I want to pass variable with response method.

Upvotes: 0

Views: 6182

Answers (5)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You can try this:

$name = 'rocky';
return view('resizeImage',compact('name'));

Basically reponse() will help you to customize the response's HTTP status code and headers so no need to use it. See Documentation

Hope this work for you!

Upvotes: 0

user7399674
user7399674

Reputation:

You can try like that,

$name = 'rocky';
return view("folder_name.blade_name", compact("name'));

In your case, blade_name will be home and check if there is any folder above it, inside resources folder.

I hope this will work.

Upvotes: 0

Mihai Matei
Mihai Matei

Reputation: 24276

Take a look at the Laravel documentation:

return response()->view('hello', ['name' => 'rocky']);

Upvotes: 3

patricus
patricus

Reputation: 62338

The view is not passed in as a parameter to response(), it is called as a method on response(). So, you need to change your code to:

return response()->view('home', ['name' => 'rocky']);

If you don't actually need the full response object (you don't need to modify headers or anything), you can just return the view object using the view() helper method:

return view('home', ['name' => 'rocky']);

You can read more in the documentation here.

Upvotes: 2

aceraven777
aceraven777

Reputation: 4556

Try to use the view method itself. E.g.:

$name = 'rocky';
return view('home', compact('name'));

My suggestion is you read the Laravel documentation thoroughly, and master the basics of PHP. (http://laravel.com/docs/5.1)

Upvotes: 0

Related Questions