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










share|improve this question


















  • 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















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










share|improve this question


















  • 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













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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








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












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





share|improve this answer























    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',
    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
    });


    }
    });














     

    draft saved


    draft discarded


















    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








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





    share|improve this answer



























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





      share|improve this answer

























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





        share|improve this answer














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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 at 17:55

























        answered Nov 12 at 23:28









        bryceandy

        364




        364






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?