Pass data to view in Laravel
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
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 '18 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 '18 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 '18 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 '18 at 21:55
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 '18 at 5:31
|
show 9 more comments
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 '18 at 20:03
MehranMehran
1831312
1831312
1
The code seems to be correct. Where are you getting this error, in the view or in the controller?
– Laerte
Nov 12 '18 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 '18 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 '18 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 '18 at 21:55
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 '18 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 '18 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 '18 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 '18 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 '18 at 21:55
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 21:55
1
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 '18 at 5:31
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 '18 at 5:31
|
show 9 more comments
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 17:55
answered Nov 12 '18 at 23:28
bryceandybryceandy
615
615
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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 '18 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 '18 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 '18 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 '18 at 21:55
1
@Laerte ,@Stephen Lake Thanks that was my mistake, I add the wrong route in web.php
– Mehran
Nov 13 '18 at 5:31