可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am confuse as how to pass variable from controller to view. I know there is a lot about this already on stockoverflow but unable to find one that works, please point me in the right direction. Thank you.
CONTROLLER
class StoreController extends Controller { public function index() { $store = Store::all(); // got this from database model return view('store', $store); } {
VIEW
// try a couple of differnt things nothing works print_r($store); print_r($store[0]->id); print_r($id);
回答1:
public function index() { $store = Store::all(); // got this from database model return view('store')->with('store', $store); }
You've three options in general.
return view('store')->with('store', $store);
return view('store')->withStore($store);
return view('store')->with(compact('store'));
1.Creates a variable named store
which will be available in your view
and stores the value in the variable $store
in it.
2.Shorthand for the above one.
3.Also a short hand and it creates the same variable name as of the value passing one.
Now in your view
you can access this variable using
{{ $store->name }}
it will be same for all 3 methods.
回答2:
Try this
public function index() { return view('store', ['store' => Store::all()]); }
Then you can access $store in your review.
回答3:
Both of the answers provided so far will solve your problem but it's also worth knowing that your original approach of providing $store
as the second argument to view()
was very nearly correct. What you were missing is that the argument needs to be an array of the variables you want to pass to the view.
I would probably have gone with return view('store', compact('store'));
but you could also have used return view('store', ['store' => $store]);
.
回答4:
Try this :
class StoreController extends Controller { public function index() { $store = Store::all(); // got this from database model return view('store')->withStore($store); } } View : {{$store->id}}
回答5:
It works for me. You can use 'compact' like this:
CONTROLLER:
class StoreController extends Controller { public function index() { $store = Store::all(); // 'don't forget' use App\Driver; return view('store', compact('store')); } }
Don't forget the library: use App\Store;
VIEW:
In the view you have one array with all the data just calling {{ $store }}
If you want an specific data use something like:
@foreach($store as $data) {{ $data->name }} @endforeach
Don't forget to use the "Forms & HTML" of Laravel, here is the link of the documentation: https://laravel.com/docs/4.2/html
回答6:
In larval v5.6.3, I am passing Array from Controller to View like this,
$data = array( 'title' => 'Welcome to Laravel', 'services' => ['Web Design', 'Programming', 'SEO'] ); return view('pages.index', compact('data'));
And in HTML you can read Array as,
@if(count($data['services']) > 0) <ul> @foreach($data['services'] as $service) <li>{{$service}}</li> @endforeach </ul> @endif
回答7:
Even when the other methods work great, the suggested way, is using the compact() helper as a parameter.
public function index() { $stores = Store::all(); // got this from database model return view('store', compact('stores')); }
In laravel 5.2 the "with()" method is also used to flash data to your view, the compact helper can handle multiple variables.
Source: Laravel docs and personal experience.
回答8:
I had same challenge using
return view('store', compact('store'));
But I was rather using <?php foreach()... ?>
inside the blade file to test. I changed to {{ }}
blade syntax and everything worked fine now.