dynamically returning views by a foreach loop












0














I have a foreach loop that runs by my database:



<div id="nav" >
@foreach(AppCategories::whereNull('parent_id')->get() as $category)
<a id="link1" href="{{ route('showCategory' , $category->id) }}" ><div class="link">{{ $category->name }}</div></a>
@endforeach
</div>


I want foreach link that adds by that it knows his path to the blades dynamically.



What should I do here?



My web.php:



Route::get('/category/{category}', 'PagesController@showCategory')->name('showCategory');


My controller:



public function showCategory(Categories $category) {

}









share|improve this question





























    0














    I have a foreach loop that runs by my database:



    <div id="nav" >
    @foreach(AppCategories::whereNull('parent_id')->get() as $category)
    <a id="link1" href="{{ route('showCategory' , $category->id) }}" ><div class="link">{{ $category->name }}</div></a>
    @endforeach
    </div>


    I want foreach link that adds by that it knows his path to the blades dynamically.



    What should I do here?



    My web.php:



    Route::get('/category/{category}', 'PagesController@showCategory')->name('showCategory');


    My controller:



    public function showCategory(Categories $category) {

    }









    share|improve this question



























      0












      0








      0







      I have a foreach loop that runs by my database:



      <div id="nav" >
      @foreach(AppCategories::whereNull('parent_id')->get() as $category)
      <a id="link1" href="{{ route('showCategory' , $category->id) }}" ><div class="link">{{ $category->name }}</div></a>
      @endforeach
      </div>


      I want foreach link that adds by that it knows his path to the blades dynamically.



      What should I do here?



      My web.php:



      Route::get('/category/{category}', 'PagesController@showCategory')->name('showCategory');


      My controller:



      public function showCategory(Categories $category) {

      }









      share|improve this question















      I have a foreach loop that runs by my database:



      <div id="nav" >
      @foreach(AppCategories::whereNull('parent_id')->get() as $category)
      <a id="link1" href="{{ route('showCategory' , $category->id) }}" ><div class="link">{{ $category->name }}</div></a>
      @endforeach
      </div>


      I want foreach link that adds by that it knows his path to the blades dynamically.



      What should I do here?



      My web.php:



      Route::get('/category/{category}', 'PagesController@showCategory')->name('showCategory');


      My controller:



      public function showCategory(Categories $category) {

      }






      database laravel dynamic foreach controller






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 20:20









      Mihai Chelaru

      2,13291022




      2,13291022










      asked Nov 12 at 15:44









      Bashar Al-Aswad

      262




      262
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I'm guessing that you want to create a link for each category which goes to a page that displays more information about the selected category.



          Your blade file in which you create the links for all the categories seems fine to me, but I'd recommend changing your route file to this:



          Route::get('/category/{id}', 'PagesController@showCategory')->name('showCategory');



          Then for your showCategory function, you would need something like this:



          public function showCategory($id) {
          $category = Categories::find($id);

          // i used categories.show here, change it to whatever view you use
          return view('categories.show')->with('category', $category);
          }


          Then in your categories.show view, you can access the properties of the category like so:



          $category->id; // or whatever you want to display





          As per OP's request: the first 5 categories in the database that lead to their pages:





          In your controller:



          public function myFunction()
          {
          $categories = Categories::all()->take(5)->get();

          return view('your.view')->with('categories', $categories);
          }


          In your blade view (assuming that the view for the category is at: /category/id):



          @foreach($categories as $category)
          <a href="/category/{{$category->id}}">{{$category->name}}</a>
          @endforeach





          share|improve this answer























          • Thank you for your answer.And if I have 5 different links and each one has a different 'href' how would you recommend to do it in the categories.show?
            – Bashar Al-Aswad
            Nov 12 at 21:11










          • 5 different links in one category? Or 5 different categories leading to their pages?
            – rpm192
            Nov 12 at 21:18










          • 5 different categories leiding to their pages is what I mean.
            – Bashar Al-Aswad
            Nov 13 at 8:07










          • Do you want 5 random categories from you database? All of them? Or the 5 first or 5 last.
            – rpm192
            Nov 13 at 8:18










          • First 5 categories.
            – Bashar Al-Aswad
            Nov 13 at 8:46











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


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265545%2fdynamically-returning-views-by-a-foreach-loop%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









          1














          I'm guessing that you want to create a link for each category which goes to a page that displays more information about the selected category.



          Your blade file in which you create the links for all the categories seems fine to me, but I'd recommend changing your route file to this:



          Route::get('/category/{id}', 'PagesController@showCategory')->name('showCategory');



          Then for your showCategory function, you would need something like this:



          public function showCategory($id) {
          $category = Categories::find($id);

          // i used categories.show here, change it to whatever view you use
          return view('categories.show')->with('category', $category);
          }


          Then in your categories.show view, you can access the properties of the category like so:



          $category->id; // or whatever you want to display





          As per OP's request: the first 5 categories in the database that lead to their pages:





          In your controller:



          public function myFunction()
          {
          $categories = Categories::all()->take(5)->get();

          return view('your.view')->with('categories', $categories);
          }


          In your blade view (assuming that the view for the category is at: /category/id):



          @foreach($categories as $category)
          <a href="/category/{{$category->id}}">{{$category->name}}</a>
          @endforeach





          share|improve this answer























          • Thank you for your answer.And if I have 5 different links and each one has a different 'href' how would you recommend to do it in the categories.show?
            – Bashar Al-Aswad
            Nov 12 at 21:11










          • 5 different links in one category? Or 5 different categories leading to their pages?
            – rpm192
            Nov 12 at 21:18










          • 5 different categories leiding to their pages is what I mean.
            – Bashar Al-Aswad
            Nov 13 at 8:07










          • Do you want 5 random categories from you database? All of them? Or the 5 first or 5 last.
            – rpm192
            Nov 13 at 8:18










          • First 5 categories.
            – Bashar Al-Aswad
            Nov 13 at 8:46
















          1














          I'm guessing that you want to create a link for each category which goes to a page that displays more information about the selected category.



          Your blade file in which you create the links for all the categories seems fine to me, but I'd recommend changing your route file to this:



          Route::get('/category/{id}', 'PagesController@showCategory')->name('showCategory');



          Then for your showCategory function, you would need something like this:



          public function showCategory($id) {
          $category = Categories::find($id);

          // i used categories.show here, change it to whatever view you use
          return view('categories.show')->with('category', $category);
          }


          Then in your categories.show view, you can access the properties of the category like so:



          $category->id; // or whatever you want to display





          As per OP's request: the first 5 categories in the database that lead to their pages:





          In your controller:



          public function myFunction()
          {
          $categories = Categories::all()->take(5)->get();

          return view('your.view')->with('categories', $categories);
          }


          In your blade view (assuming that the view for the category is at: /category/id):



          @foreach($categories as $category)
          <a href="/category/{{$category->id}}">{{$category->name}}</a>
          @endforeach





          share|improve this answer























          • Thank you for your answer.And if I have 5 different links and each one has a different 'href' how would you recommend to do it in the categories.show?
            – Bashar Al-Aswad
            Nov 12 at 21:11










          • 5 different links in one category? Or 5 different categories leading to their pages?
            – rpm192
            Nov 12 at 21:18










          • 5 different categories leiding to their pages is what I mean.
            – Bashar Al-Aswad
            Nov 13 at 8:07










          • Do you want 5 random categories from you database? All of them? Or the 5 first or 5 last.
            – rpm192
            Nov 13 at 8:18










          • First 5 categories.
            – Bashar Al-Aswad
            Nov 13 at 8:46














          1












          1








          1






          I'm guessing that you want to create a link for each category which goes to a page that displays more information about the selected category.



          Your blade file in which you create the links for all the categories seems fine to me, but I'd recommend changing your route file to this:



          Route::get('/category/{id}', 'PagesController@showCategory')->name('showCategory');



          Then for your showCategory function, you would need something like this:



          public function showCategory($id) {
          $category = Categories::find($id);

          // i used categories.show here, change it to whatever view you use
          return view('categories.show')->with('category', $category);
          }


          Then in your categories.show view, you can access the properties of the category like so:



          $category->id; // or whatever you want to display





          As per OP's request: the first 5 categories in the database that lead to their pages:





          In your controller:



          public function myFunction()
          {
          $categories = Categories::all()->take(5)->get();

          return view('your.view')->with('categories', $categories);
          }


          In your blade view (assuming that the view for the category is at: /category/id):



          @foreach($categories as $category)
          <a href="/category/{{$category->id}}">{{$category->name}}</a>
          @endforeach





          share|improve this answer














          I'm guessing that you want to create a link for each category which goes to a page that displays more information about the selected category.



          Your blade file in which you create the links for all the categories seems fine to me, but I'd recommend changing your route file to this:



          Route::get('/category/{id}', 'PagesController@showCategory')->name('showCategory');



          Then for your showCategory function, you would need something like this:



          public function showCategory($id) {
          $category = Categories::find($id);

          // i used categories.show here, change it to whatever view you use
          return view('categories.show')->with('category', $category);
          }


          Then in your categories.show view, you can access the properties of the category like so:



          $category->id; // or whatever you want to display





          As per OP's request: the first 5 categories in the database that lead to their pages:





          In your controller:



          public function myFunction()
          {
          $categories = Categories::all()->take(5)->get();

          return view('your.view')->with('categories', $categories);
          }


          In your blade view (assuming that the view for the category is at: /category/id):



          @foreach($categories as $category)
          <a href="/category/{{$category->id}}">{{$category->name}}</a>
          @endforeach






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 at 8:51

























          answered Nov 12 at 20:32









          rpm192

          1,1571417




          1,1571417












          • Thank you for your answer.And if I have 5 different links and each one has a different 'href' how would you recommend to do it in the categories.show?
            – Bashar Al-Aswad
            Nov 12 at 21:11










          • 5 different links in one category? Or 5 different categories leading to their pages?
            – rpm192
            Nov 12 at 21:18










          • 5 different categories leiding to their pages is what I mean.
            – Bashar Al-Aswad
            Nov 13 at 8:07










          • Do you want 5 random categories from you database? All of them? Or the 5 first or 5 last.
            – rpm192
            Nov 13 at 8:18










          • First 5 categories.
            – Bashar Al-Aswad
            Nov 13 at 8:46


















          • Thank you for your answer.And if I have 5 different links and each one has a different 'href' how would you recommend to do it in the categories.show?
            – Bashar Al-Aswad
            Nov 12 at 21:11










          • 5 different links in one category? Or 5 different categories leading to their pages?
            – rpm192
            Nov 12 at 21:18










          • 5 different categories leiding to their pages is what I mean.
            – Bashar Al-Aswad
            Nov 13 at 8:07










          • Do you want 5 random categories from you database? All of them? Or the 5 first or 5 last.
            – rpm192
            Nov 13 at 8:18










          • First 5 categories.
            – Bashar Al-Aswad
            Nov 13 at 8:46
















          Thank you for your answer.And if I have 5 different links and each one has a different 'href' how would you recommend to do it in the categories.show?
          – Bashar Al-Aswad
          Nov 12 at 21:11




          Thank you for your answer.And if I have 5 different links and each one has a different 'href' how would you recommend to do it in the categories.show?
          – Bashar Al-Aswad
          Nov 12 at 21:11












          5 different links in one category? Or 5 different categories leading to their pages?
          – rpm192
          Nov 12 at 21:18




          5 different links in one category? Or 5 different categories leading to their pages?
          – rpm192
          Nov 12 at 21:18












          5 different categories leiding to their pages is what I mean.
          – Bashar Al-Aswad
          Nov 13 at 8:07




          5 different categories leiding to their pages is what I mean.
          – Bashar Al-Aswad
          Nov 13 at 8:07












          Do you want 5 random categories from you database? All of them? Or the 5 first or 5 last.
          – rpm192
          Nov 13 at 8:18




          Do you want 5 random categories from you database? All of them? Or the 5 first or 5 last.
          – rpm192
          Nov 13 at 8:18












          First 5 categories.
          – Bashar Al-Aswad
          Nov 13 at 8:46




          First 5 categories.
          – Bashar Al-Aswad
          Nov 13 at 8:46


















          draft saved

          draft discarded




















































          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


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




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265545%2fdynamically-returning-views-by-a-foreach-loop%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python