Laravel deleting a thread of a forum












0















I already searched and tried a lot of solutions of similar case but nothing works for my case. I'm still pretty new to Laravel and don't really understand about eloquent. I'm trying to delete thread of a forum, please help.



This is the route for deleting threads:



Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [
'uses' => 'ForumsController@deleteThread',
'as' => 'thread.delete']);


This is the function (i don't know how to get the thread id):



    public function deleteThread($id)
{
$forum = Forum::find($id);
$thread = $forum->threads;

dd($thread);
$thread->delete();

return redirect()->back();
}


This is the delete button:



<a href="{{ route('thread.delete', ['forum_id' => $forum->id, 'thread_id' => $thread->id]) }}" class="btn btn-danger">Delete</a>


This is the Forum model:



<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Forum extends Model
{
public function threads () {
return $this->hasMany(Thread::class);
}
}


This is the Thread Model:



<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Thread extends Model
{
public function forum () {
return $this->belongsTo(Forum::class);
}
}









share|improve this question





























    0















    I already searched and tried a lot of solutions of similar case but nothing works for my case. I'm still pretty new to Laravel and don't really understand about eloquent. I'm trying to delete thread of a forum, please help.



    This is the route for deleting threads:



    Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [
    'uses' => 'ForumsController@deleteThread',
    'as' => 'thread.delete']);


    This is the function (i don't know how to get the thread id):



        public function deleteThread($id)
    {
    $forum = Forum::find($id);
    $thread = $forum->threads;

    dd($thread);
    $thread->delete();

    return redirect()->back();
    }


    This is the delete button:



    <a href="{{ route('thread.delete', ['forum_id' => $forum->id, 'thread_id' => $thread->id]) }}" class="btn btn-danger">Delete</a>


    This is the Forum model:



    <?php

    namespace App;

    use IlluminateDatabaseEloquentModel;

    class Forum extends Model
    {
    public function threads () {
    return $this->hasMany(Thread::class);
    }
    }


    This is the Thread Model:



    <?php

    namespace App;

    use IlluminateDatabaseEloquentModel;

    class Thread extends Model
    {
    public function forum () {
    return $this->belongsTo(Forum::class);
    }
    }









    share|improve this question



























      0












      0








      0








      I already searched and tried a lot of solutions of similar case but nothing works for my case. I'm still pretty new to Laravel and don't really understand about eloquent. I'm trying to delete thread of a forum, please help.



      This is the route for deleting threads:



      Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [
      'uses' => 'ForumsController@deleteThread',
      'as' => 'thread.delete']);


      This is the function (i don't know how to get the thread id):



          public function deleteThread($id)
      {
      $forum = Forum::find($id);
      $thread = $forum->threads;

      dd($thread);
      $thread->delete();

      return redirect()->back();
      }


      This is the delete button:



      <a href="{{ route('thread.delete', ['forum_id' => $forum->id, 'thread_id' => $thread->id]) }}" class="btn btn-danger">Delete</a>


      This is the Forum model:



      <?php

      namespace App;

      use IlluminateDatabaseEloquentModel;

      class Forum extends Model
      {
      public function threads () {
      return $this->hasMany(Thread::class);
      }
      }


      This is the Thread Model:



      <?php

      namespace App;

      use IlluminateDatabaseEloquentModel;

      class Thread extends Model
      {
      public function forum () {
      return $this->belongsTo(Forum::class);
      }
      }









      share|improve this question
















      I already searched and tried a lot of solutions of similar case but nothing works for my case. I'm still pretty new to Laravel and don't really understand about eloquent. I'm trying to delete thread of a forum, please help.



      This is the route for deleting threads:



      Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [
      'uses' => 'ForumsController@deleteThread',
      'as' => 'thread.delete']);


      This is the function (i don't know how to get the thread id):



          public function deleteThread($id)
      {
      $forum = Forum::find($id);
      $thread = $forum->threads;

      dd($thread);
      $thread->delete();

      return redirect()->back();
      }


      This is the delete button:



      <a href="{{ route('thread.delete', ['forum_id' => $forum->id, 'thread_id' => $thread->id]) }}" class="btn btn-danger">Delete</a>


      This is the Forum model:



      <?php

      namespace App;

      use IlluminateDatabaseEloquentModel;

      class Forum extends Model
      {
      public function threads () {
      return $this->hasMany(Thread::class);
      }
      }


      This is the Thread Model:



      <?php

      namespace App;

      use IlluminateDatabaseEloquentModel;

      class Thread extends Model
      {
      public function forum () {
      return $this->belongsTo(Forum::class);
      }
      }






      php laravel eloquent laravel-5.5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 12:31









      Namoshek

      3,2122920




      3,2122920










      asked Nov 23 '18 at 12:24









      Kurniawan PratamaKurniawan Pratama

      31




      31
























          2 Answers
          2






          active

          oldest

          votes


















          2














          As your route looks like this:



          Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [ ... ])


          you'll have to use forum_id and thread_id as parameters of your controller function:



          public function deleteThread($forum_id, $thread_id)
          {
          $forum = Forum::find($forum_id);
          $thread = Thread::find($thread_id);

          $thread->delete();

          return redirect()->back();
          }


          You could even let Laravel inject the Forum and Thread into the controller for you - by type-hinting them on the function:



          public function deleteThread(Forum $forum, Thread $thread)
          {
          $thread->delete();

          return redirect()->back();
          }


          Of course you'd have to adjust the forum_id parameter of the route to forum and the thread_id to thread respectively. This also requires to change the parameters you pass to the URL in other views for example (aka the delete button).



          UPDATE
          Just by the way you shouldn't be using a get request for deleting. You should use a DELETE HTTP request.






          share|improve this answer


























          • @Borisu good spot, missed that. Thanks.

            – Namoshek
            Nov 23 '18 at 12:39











          • Thanks a lot! I've been looking for this answer so many days

            – Kurniawan Pratama
            Nov 23 '18 at 12:59



















          1














          You should try this:



          public function deleteThread($forum_id,$thread_id)
          {
          Thread::destroy($thread_id);

          return redirect()->back();
          }





          share|improve this answer


























          • I think you meant Thread::destroy($thread_id).

            – Namoshek
            Nov 23 '18 at 12:33











          • @Namoshek: Yes destroy thread

            – user10186369
            Nov 23 '18 at 12:34












          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%2f53446685%2flaravel-deleting-a-thread-of-a-forum%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          As your route looks like this:



          Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [ ... ])


          you'll have to use forum_id and thread_id as parameters of your controller function:



          public function deleteThread($forum_id, $thread_id)
          {
          $forum = Forum::find($forum_id);
          $thread = Thread::find($thread_id);

          $thread->delete();

          return redirect()->back();
          }


          You could even let Laravel inject the Forum and Thread into the controller for you - by type-hinting them on the function:



          public function deleteThread(Forum $forum, Thread $thread)
          {
          $thread->delete();

          return redirect()->back();
          }


          Of course you'd have to adjust the forum_id parameter of the route to forum and the thread_id to thread respectively. This also requires to change the parameters you pass to the URL in other views for example (aka the delete button).



          UPDATE
          Just by the way you shouldn't be using a get request for deleting. You should use a DELETE HTTP request.






          share|improve this answer


























          • @Borisu good spot, missed that. Thanks.

            – Namoshek
            Nov 23 '18 at 12:39











          • Thanks a lot! I've been looking for this answer so many days

            – Kurniawan Pratama
            Nov 23 '18 at 12:59
















          2














          As your route looks like this:



          Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [ ... ])


          you'll have to use forum_id and thread_id as parameters of your controller function:



          public function deleteThread($forum_id, $thread_id)
          {
          $forum = Forum::find($forum_id);
          $thread = Thread::find($thread_id);

          $thread->delete();

          return redirect()->back();
          }


          You could even let Laravel inject the Forum and Thread into the controller for you - by type-hinting them on the function:



          public function deleteThread(Forum $forum, Thread $thread)
          {
          $thread->delete();

          return redirect()->back();
          }


          Of course you'd have to adjust the forum_id parameter of the route to forum and the thread_id to thread respectively. This also requires to change the parameters you pass to the URL in other views for example (aka the delete button).



          UPDATE
          Just by the way you shouldn't be using a get request for deleting. You should use a DELETE HTTP request.






          share|improve this answer


























          • @Borisu good spot, missed that. Thanks.

            – Namoshek
            Nov 23 '18 at 12:39











          • Thanks a lot! I've been looking for this answer so many days

            – Kurniawan Pratama
            Nov 23 '18 at 12:59














          2












          2








          2







          As your route looks like this:



          Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [ ... ])


          you'll have to use forum_id and thread_id as parameters of your controller function:



          public function deleteThread($forum_id, $thread_id)
          {
          $forum = Forum::find($forum_id);
          $thread = Thread::find($thread_id);

          $thread->delete();

          return redirect()->back();
          }


          You could even let Laravel inject the Forum and Thread into the controller for you - by type-hinting them on the function:



          public function deleteThread(Forum $forum, Thread $thread)
          {
          $thread->delete();

          return redirect()->back();
          }


          Of course you'd have to adjust the forum_id parameter of the route to forum and the thread_id to thread respectively. This also requires to change the parameters you pass to the URL in other views for example (aka the delete button).



          UPDATE
          Just by the way you shouldn't be using a get request for deleting. You should use a DELETE HTTP request.






          share|improve this answer















          As your route looks like this:



          Route::get('/forum/{forum_id}/thread/{thread_id}/delete', [ ... ])


          you'll have to use forum_id and thread_id as parameters of your controller function:



          public function deleteThread($forum_id, $thread_id)
          {
          $forum = Forum::find($forum_id);
          $thread = Thread::find($thread_id);

          $thread->delete();

          return redirect()->back();
          }


          You could even let Laravel inject the Forum and Thread into the controller for you - by type-hinting them on the function:



          public function deleteThread(Forum $forum, Thread $thread)
          {
          $thread->delete();

          return redirect()->back();
          }


          Of course you'd have to adjust the forum_id parameter of the route to forum and the thread_id to thread respectively. This also requires to change the parameters you pass to the URL in other views for example (aka the delete button).



          UPDATE
          Just by the way you shouldn't be using a get request for deleting. You should use a DELETE HTTP request.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 12:38









          Borisu

          549312




          549312










          answered Nov 23 '18 at 12:30









          NamoshekNamoshek

          3,2122920




          3,2122920













          • @Borisu good spot, missed that. Thanks.

            – Namoshek
            Nov 23 '18 at 12:39











          • Thanks a lot! I've been looking for this answer so many days

            – Kurniawan Pratama
            Nov 23 '18 at 12:59



















          • @Borisu good spot, missed that. Thanks.

            – Namoshek
            Nov 23 '18 at 12:39











          • Thanks a lot! I've been looking for this answer so many days

            – Kurniawan Pratama
            Nov 23 '18 at 12:59

















          @Borisu good spot, missed that. Thanks.

          – Namoshek
          Nov 23 '18 at 12:39





          @Borisu good spot, missed that. Thanks.

          – Namoshek
          Nov 23 '18 at 12:39













          Thanks a lot! I've been looking for this answer so many days

          – Kurniawan Pratama
          Nov 23 '18 at 12:59





          Thanks a lot! I've been looking for this answer so many days

          – Kurniawan Pratama
          Nov 23 '18 at 12:59













          1














          You should try this:



          public function deleteThread($forum_id,$thread_id)
          {
          Thread::destroy($thread_id);

          return redirect()->back();
          }





          share|improve this answer


























          • I think you meant Thread::destroy($thread_id).

            – Namoshek
            Nov 23 '18 at 12:33











          • @Namoshek: Yes destroy thread

            – user10186369
            Nov 23 '18 at 12:34
















          1














          You should try this:



          public function deleteThread($forum_id,$thread_id)
          {
          Thread::destroy($thread_id);

          return redirect()->back();
          }





          share|improve this answer


























          • I think you meant Thread::destroy($thread_id).

            – Namoshek
            Nov 23 '18 at 12:33











          • @Namoshek: Yes destroy thread

            – user10186369
            Nov 23 '18 at 12:34














          1












          1








          1







          You should try this:



          public function deleteThread($forum_id,$thread_id)
          {
          Thread::destroy($thread_id);

          return redirect()->back();
          }





          share|improve this answer















          You should try this:



          public function deleteThread($forum_id,$thread_id)
          {
          Thread::destroy($thread_id);

          return redirect()->back();
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 12:34

























          answered Nov 23 '18 at 12:31







          user10186369




















          • I think you meant Thread::destroy($thread_id).

            – Namoshek
            Nov 23 '18 at 12:33











          • @Namoshek: Yes destroy thread

            – user10186369
            Nov 23 '18 at 12:34



















          • I think you meant Thread::destroy($thread_id).

            – Namoshek
            Nov 23 '18 at 12:33











          • @Namoshek: Yes destroy thread

            – user10186369
            Nov 23 '18 at 12:34

















          I think you meant Thread::destroy($thread_id).

          – Namoshek
          Nov 23 '18 at 12:33





          I think you meant Thread::destroy($thread_id).

          – Namoshek
          Nov 23 '18 at 12:33













          @Namoshek: Yes destroy thread

          – user10186369
          Nov 23 '18 at 12:34





          @Namoshek: Yes destroy thread

          – user10186369
          Nov 23 '18 at 12:34


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446685%2flaravel-deleting-a-thread-of-a-forum%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()