Pass data to view in Laravel
up vote
0
down vote
favorite
I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies
error
this is my Controller:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
and this is my view:
<ul class="list-group">
@foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
@endforeach
</ul>
I use Laravel version 5.5
php laravel laravel-5.5
|
show 9 more comments
up vote
0
down vote
favorite
I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies
error
this is my Controller:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
and this is my view:
<ul class="list-group">
@foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
@endforeach
</ul>
I use Laravel version 5.5
php laravel laravel-5.5
1
The code seems to be correct. Where are you getting this error, in the view or in the controller?
– Laerte
Nov 12 at 20:14
3
So, you are not calling the view from this method. You are calling it from another method or another controller.
– Laerte
Nov 12 at 20:39
2
The code is/was fine, except I'm not sure about thatall()
, as far as I know that is only applicable to hydrated collections so there has to be something you're not sharing with us, somewhere you're not calling the correct code. @Laerte is right. Something is off.
– Stephen Lake
Nov 12 at 20:45
1
I mean that you are not calling thisindex()
function. Maybe the route is wrong or you are using a wrong url action, ir something like this. Try to review the path from the button/link to the function to find where it is going.
– Laerte
Nov 12 at 21:55
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 at 5:31
|
show 9 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies
error
this is my Controller:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
and this is my view:
<ul class="list-group">
@foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
@endforeach
</ul>
I use Laravel version 5.5
php laravel laravel-5.5
I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies
error
this is my Controller:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
and this is my view:
<ul class="list-group">
@foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
@endforeach
</ul>
I use Laravel version 5.5
php laravel laravel-5.5
php laravel laravel-5.5
asked Nov 12 at 20:03
Mehran
6118
6118
1
The code seems to be correct. Where are you getting this error, in the view or in the controller?
– Laerte
Nov 12 at 20:14
3
So, you are not calling the view from this method. You are calling it from another method or another controller.
– Laerte
Nov 12 at 20:39
2
The code is/was fine, except I'm not sure about thatall()
, as far as I know that is only applicable to hydrated collections so there has to be something you're not sharing with us, somewhere you're not calling the correct code. @Laerte is right. Something is off.
– Stephen Lake
Nov 12 at 20:45
1
I mean that you are not calling thisindex()
function. Maybe the route is wrong or you are using a wrong url action, ir something like this. Try to review the path from the button/link to the function to find where it is going.
– Laerte
Nov 12 at 21:55
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 at 5:31
|
show 9 more comments
1
The code seems to be correct. Where are you getting this error, in the view or in the controller?
– Laerte
Nov 12 at 20:14
3
So, you are not calling the view from this method. You are calling it from another method or another controller.
– Laerte
Nov 12 at 20:39
2
The code is/was fine, except I'm not sure about thatall()
, as far as I know that is only applicable to hydrated collections so there has to be something you're not sharing with us, somewhere you're not calling the correct code. @Laerte is right. Something is off.
– Stephen Lake
Nov 12 at 20:45
1
I mean that you are not calling thisindex()
function. Maybe the route is wrong or you are using a wrong url action, ir something like this. Try to review the path from the button/link to the function to find where it is going.
– Laerte
Nov 12 at 21:55
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 at 5:31
1
1
The code seems to be correct. Where are you getting this error, in the view or in the controller?
– Laerte
Nov 12 at 20:14
The code seems to be correct. Where are you getting this error, in the view or in the controller?
– Laerte
Nov 12 at 20:14
3
3
So, you are not calling the view from this method. You are calling it from another method or another controller.
– Laerte
Nov 12 at 20:39
So, you are not calling the view from this method. You are calling it from another method or another controller.
– Laerte
Nov 12 at 20:39
2
2
The code is/was fine, except I'm not sure about that
all()
, as far as I know that is only applicable to hydrated collections so there has to be something you're not sharing with us, somewhere you're not calling the correct code. @Laerte is right. Something is off.– Stephen Lake
Nov 12 at 20:45
The code is/was fine, except I'm not sure about that
all()
, as far as I know that is only applicable to hydrated collections so there has to be something you're not sharing with us, somewhere you're not calling the correct code. @Laerte is right. Something is off.– Stephen Lake
Nov 12 at 20:45
1
1
I mean that you are not calling this
index()
function. Maybe the route is wrong or you are using a wrong url action, ir something like this. Try to review the path from the button/link to the function to find where it is going.– Laerte
Nov 12 at 21:55
I mean that you are not calling this
index()
function. Maybe the route is wrong or you are using a wrong url action, ir something like this. Try to review the path from the button/link to the function to find where it is going.– Laerte
Nov 12 at 21:55
1
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 at 5:31
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 at 5:31
|
show 9 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Make sure in your route file on routes/web.php
you have a route to the index function like so:
Route::get('urlToYourView', 'YourController@index');
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Make sure in your route file on routes/web.php
you have a route to the index function like so:
Route::get('urlToYourView', 'YourController@index');
add a comment |
up vote
1
down vote
accepted
Make sure in your route file on routes/web.php
you have a route to the index function like so:
Route::get('urlToYourView', 'YourController@index');
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Make sure in your route file on routes/web.php
you have a route to the index function like so:
Route::get('urlToYourView', 'YourController@index');
Make sure in your route file on routes/web.php
you have a route to the index function like so:
Route::get('urlToYourView', 'YourController@index');
edited Nov 14 at 17:55
answered Nov 12 at 23:28
bryceandy
364
364
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269304%2fpass-data-to-view-in-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The code seems to be correct. Where are you getting this error, in the view or in the controller?
– Laerte
Nov 12 at 20:14
3
So, you are not calling the view from this method. You are calling it from another method or another controller.
– Laerte
Nov 12 at 20:39
2
The code is/was fine, except I'm not sure about that
all()
, as far as I know that is only applicable to hydrated collections so there has to be something you're not sharing with us, somewhere you're not calling the correct code. @Laerte is right. Something is off.– Stephen Lake
Nov 12 at 20:45
1
I mean that you are not calling this
index()
function. Maybe the route is wrong or you are using a wrong url action, ir something like this. Try to review the path from the button/link to the function to find where it is going.– Laerte
Nov 12 at 21:55
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 at 5:31