title: LARAVEL: All elements from foreach have the id 1, which makes deleting them impossible
body: <p>I'm in a difficult situation.
  I'm building a birthlist clone with a scraper but it's difficult to code it because I'm not that experienced. I've managed to create an admin page with multiple forms to insert websites, categories and a link form where you can specify on what link the scraping should be done and to what site and category it will be linked. However, adding such things is no problem, but deleting them is not working..</p>
  <p>My sites, categories en links are displayed on my admin page with for each element a scrape and delete button. When I delete the last item in the row, it deletes the first element. When I want to delete something else, laravel throws an exception that it tries to attempt to read property on null, which means that it no longer exists. When I dump and die before the delete function, every item in every list has the id '1'. That is why it deletes the first item in a row. Can anyone help me please?</p>
  <p>I assume it's because the id that is requested to delete the item, is retrieved from the url and the id that is given in the url is the user id which is 1. So if anyone can give me tips to make sure I can give the id of the user but in a different way. Let me know!
  My code:</p>
  <p>Admincontroller:</p>
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Models\Website;
  use App\Models\Category;
  use App\Models\Link;
  use Goutte\Client;
  
  
  class AdminController extends Controller
  {
      public function showAdmin($id)
      {
  
          $categories = Category::all();
          $websites = Website::all();
          $links = Link::all();
          return view('admin', compact('categories', 'websites', 'links'));
      }
  
      public function storeWeb(Request $request)
      {
              $web = Website::create([
                  'title' =&gt; $request-&gt;input('site'),
                  'url' =&gt; $request-&gt;input('url')
              ]);
          return back();
      }
  
      public function destroyWeb(Request $request, $id)
      {
          $web = Website::find($id);
          $web-&gt;delete();
          return back();
      }
  
      public function storeCat(Request $request)
      {
          $cat = Category::create([
              'title' =&gt; $request-&gt;input('title')
          ]);
          return back();
      }
  
      public function destroyCat(Request $request, $id)
      {
          $cat = Category::find($id)-&gt;first();
          $cat-&gt;delete();
          return back();
      }
  
      public function storeLink(Request $request) 
      {
          $link = Link::create([
              'url' =&gt; $request-&gt;input('scrapeUrl'),
              'main_filter_selector' =&gt; $request-&gt;input('filter'),
              'website_id' =&gt; $request-&gt;input('website_id'),
              'category_id' =&gt; $request-&gt;input('category_id'),
          ]);
          return back();
      }
  
      public function destroyLink(Request $request, $id)
      {
          $link = Link::find($id);
          dd($link);
          $link-&gt;delete();
          return back();
      }
  
  
      public function scrape(Request $request)
      {
          $link = Link::find($request-&gt;link_id);
  
          $scraper = new Scraper (new Client());
  
          $scraper-&gt;handle($link);
  
          if($scraper-&gt;status == 1) {
              return response()-&gt;json(['status' =&gt; 1, 'msg' =&gt; 'Scraping done']);
          } else {
              return response()-&gt;json(['status' =&gt; 2, 'msg' =&gt; $scraper-&gt;status]);
          };
  
      }
  }
  </code></pre>
  <p>My adminpage:</p>
  <pre><code>
  &lt;body class=&quot;relative min-h-screen&quot;&gt;
      @include('partials.header')
     &lt;main class=&quot;pb-20&quot;&gt;
         &lt;div class=&quot;text-center m-auto text-2xl p-4 w-fit border-b&quot;&gt;
             &lt;h2&gt;Welkom &lt;strong&gt;{{ auth()-&gt;user()-&gt;name }}&lt;/strong&gt;&lt;/h2&gt;
         &lt;/div&gt;
         &lt;div class=&quot;gap-10 m-auto xl:w-3/5&quot;&gt;
              &lt;div class=&quot;mt-8&quot;&gt;
                  &lt;h3 class=&quot;bg-gray-100 p-4&quot;&gt;&lt;strong&gt;Winkels&lt;/strong&gt;&lt;/h3&gt;
                  &lt;div class=&quot;&quot;&gt;
                      &lt;div class=&quot;p-4 m-auto&quot;&gt;
                          &lt;form action=&quot;{{ route('newWeb', Auth()-&gt;user()) }}&quot; method=&quot;POST&quot;&gt;
                              @csrf
                              &lt;div class=&quot;flex wrap flex-row items-center justify-left my-4&quot;&gt;
                                  &lt;label for=&quot;shop&quot; class=&quot;w-2/12&quot;&gt;Webshop:&lt;/label&gt;
                                  &lt;input type=&quot;text&quot; required class=&quot;w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100&quot; name=&quot;site&quot; id=&quot;titel&quot; placeholder=&quot;Dreambaby&quot;&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;flex wrap flex-row items-center justify-left&quot;&gt;
                                  &lt;label for=&quot;url&quot; class=&quot;w-2/12&quot;&gt;
                                     Voeg een url toe:
                                  &lt;/label&gt;
                                  &lt;input type=&quot;url&quot; required class=&quot;w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100&quot; name=&quot;url&quot; id=&quot;url&quot; placeholder=&quot;http://dreambaby.com/&quot;&gt;
                                  &lt;button class=&quot;w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white mx-2&quot; name=&quot;shop&quot; value=&quot;add&quot; type=&quot;submit&quot;&gt;Voeg link toe&lt;/button&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
                              &lt;div class=&quot;flex wrap flex-row items-center mt-12&quot;&gt;
                                  &lt;div class=&quot;flex flex-row flex-wrap w-full&quot;&gt;
                                      &lt;table class=&quot;table-auto&quot;&gt;
                                          @foreach ($websites as $web)
                                          &lt;form action=&quot;{{ route('delWeb', Auth()-&gt;user()) }}&quot; method=&quot;POST&quot;&gt;
                                              @csrf
                                              @method('DELETE')
                                              &lt;tr class=&quot;border-b&quot;&gt;
                                                  &lt;td class=&quot;p-4&quot;&gt;{{ $web-&gt;title }}&lt;/td&gt;
                                                  &lt;td class=&quot;p-4&quot;&gt;{{ $web-&gt;id }}&lt;/td&gt;
                                                  &lt;td class=&quot;p-4&quot;&gt;&lt;button class=&quot;w-fit h-full p-2 bg-red-400 h-fit rounded hover:bg-red-600 hover:text-white block m-auto&quot; type=&quot;submit&quot;&gt;Verwijderen&lt;/button&gt;
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                          &lt;/form&gt;
                                          @endforeach
                                      &lt;/table&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;   
              &lt;/div&gt;
          &lt;/div&gt;           
          &lt;div class=&quot;gap-10 m-auto xl:w-3/5&quot;&gt;
              &lt;div class=&quot;mt-8&quot;&gt;
                  &lt;h3 class=&quot;bg-gray-100 p-4&quot;&gt;&lt;strong&gt;Categorieën&lt;/strong&gt;&lt;/h3&gt;
                  &lt;div class=&quot;mt-4&quot;&gt;
                      &lt;div class=&quot;p-4 m-auto&quot;&gt;
                          &lt;form action=&quot;{{ route('newCat', Auth()-&gt;user()) }}&quot; method=&quot;POST&quot;&gt;
                              @csrf
                              &lt;div class=&quot;flex wrap flex-row items-center justify-left&quot;&gt;
                                  &lt;label for=&quot;url&quot; class=&quot;w-2/12&quot;&gt;
                                     Voeg een categorie toe:
                                  &lt;/label&gt;
                                  &lt;input type=&quot;text&quot; required class=&quot;w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100&quot; name=&quot;title&quot; id=&quot;cat&quot; placeholder=&quot;Eten en Drinken&quot;&gt;
                                  &lt;button class=&quot;w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white mx-2&quot; name=&quot;category_submit&quot; type=&quot;submit&quot;&gt;toevoegen&lt;/button&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
                              &lt;div class=&quot;flex wrap flex-row items-center mt-12&quot;&gt;
                                  &lt;div class=&quot;flex flex-row flex-wrap w-full&quot;&gt;
                                      &lt;table class=&quot;table-auto&quot;&gt;
                                          @foreach ($categories as $category)
                                          &lt;form action=&quot;{{ route('delCat', Auth()-&gt;user()), $category-&gt;id }}&quot; id=&quot;{{ $category-&gt;id }}&quot; method=&quot;POST&quot;&gt;
                                              @csrf
                                              @method('DELETE')
                                              &lt;tr class=&quot;border-b&quot;&gt;
                                                  &lt;td class=&quot;p-4&quot;&gt;{{ $category-&gt;title }}&lt;/td&gt;
                                                  &lt;td class=&quot;p-4&quot;&gt;{{ $category-&gt;id }}&lt;/td&gt;
                                                  &lt;td class=&quot;p-4&quot;&gt;&lt;button class=&quot;w-fit h-full p-2 bg-red-400 h-fit rounded hover:bg-red-600 hover:text-white block m-auto&quot; type=&quot;submit&quot;&gt;Verwijderen&lt;/button&gt;
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                          &lt;/form&gt;
                                          @endforeach
                                      &lt;/table&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class=&quot;gap-10 m-auto xl:w-3/5&quot;&gt;
              &lt;div class=&quot;mt-8&quot;&gt;
                  &lt;h3 class=&quot;bg-gray-100 p-4&quot;&gt;&lt;strong&gt;Scrapes&lt;/strong&gt;&lt;/h3&gt;
                  &lt;div class=&quot;&quot;&gt;
                      &lt;div class=&quot;p-4 m-auto&quot;&gt;
                          &lt;form action=&quot;{{ route('newLink', Auth()-&gt;user()) }}&quot; method=&quot;POST&quot;&gt;
                              @csrf
                              &lt;div class=&quot;flex wrap flex-row items-center justify-left my-4&quot;&gt;
                                  &lt;label for=&quot;scrape&quot; class=&quot;w-2/12&quot;&gt;Url:&lt;/label&gt;
                                  &lt;input type=&quot;text&quot; required class=&quot;w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100&quot; name=&quot;scrapeUrl&quot; id=&quot;scrapetitel&quot; placeholder=&quot;https://www.thebabyscorner.be/nl-be/baby_eten_en_drinken/&quot;&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;flex wrap flex-row items-center justify-left my-4&quot;&gt;
                                  &lt;label for=&quot;text&quot; class=&quot;w-2/12&quot;&gt;
                                     Filter:
                                  &lt;/label&gt;
                                  &lt;input type=&quot;text&quot; required class=&quot;w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100&quot; name=&quot;filter&quot; id=&quot;url&quot; placeholder=&quot;.1-products-item&quot;&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;flex wrap flex-row items-center justify-left my-4&quot;&gt;
                                  &lt;label for=&quot;webpicker&quot; class=&quot;w-2/12&quot;&gt;Winkel:&lt;/label&gt;
                                  &lt;select name=&quot;website_id&quot; id=&quot;&quot; class=&quot;w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100&quot;&gt;
                                      &lt;option value=&quot;#&quot; disabled selected&gt;Kies je winkel&lt;/option&gt;
                                      @foreach ($websites as $web)
                                          &lt;option value=&quot;{{ $web-&gt;id }}&quot; &gt;{{ $web-&gt;title }}&lt;/option&gt;
                                      @endforeach
                                  &lt;/select&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;flex wrap flex-row items-center justify-left my-4&quot;&gt;
                                  &lt;label for=&quot;webpicker&quot; class=&quot;w-2/12&quot;&gt;Categorie:&lt;/label&gt;
                                  &lt;select name=&quot;category_id&quot; id=&quot;&quot; class=&quot;w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100&quot;&gt;
                                      &lt;option value=&quot;#&quot; disabled selected&gt;Kies je categorie&lt;/option&gt;
                                      @foreach ($categories as $cat)
                                          &lt;option value=&quot;{{ $cat-&gt;id }}&quot; &gt;{{ $cat-&gt;title }}&lt;/option&gt;
                                      @endforeach
                                  &lt;/select&gt;
                                  &lt;button class=&quot;w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white mx-2&quot; type=&quot;submit&quot; name=&quot;scrape_submit&quot;&gt;toevoegen&lt;/button&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
                          &lt;div class=&quot;flex wrap flex-row items-center mt-12 w-full&quot;&gt;
                              &lt;div class=&quot;flex flex-row flex-wrap w-full&quot;&gt;
                                  &lt;form action=&quot;{{ route('delLink', Auth()-&gt;user()) }}&quot; method=&quot;POST&quot;&gt;
                                      @csrf
                                      @method('DELETE')
                                      &lt;table class=&quot;table-auto w-full&quot;&gt;
                                          &lt;tr class=&quot;bg-slate-300&quot;&gt;
                                              &lt;td class=&quot;p-4&quot;&gt;&lt;strong&gt;Url&lt;/strong&gt;&lt;/td&gt;
                                              &lt;td class=&quot;p-4&quot;&gt;&lt;strong&gt;Filter selector&lt;/strong&gt;&lt;/td&gt;
                                              &lt;td class=&quot;p-4&quot;&gt;&lt;strong&gt;Website&lt;/strong&gt;&lt;/td&gt;
                                              &lt;td class=&quot;p-4&quot;&gt;&lt;strong&gt;Categorie&lt;/strong&gt;&lt;/td&gt;
                                              &lt;td&gt;&lt;/td&gt;
                                              &lt;td&gt;&lt;/td&gt;
  
                                          &lt;/tr&gt;
                                          @foreach ($links as $link)
                                              &lt;tr data-id=&quot;{{ $link-&gt;id }}&quot; class=&quot;&quot;&gt;
                                                  &lt;td class=&quot;p-4 border-r&quot;&gt;{{ $link-&gt;url }}&lt;/td&gt;
                                                  &lt;td class=&quot;p-4 border-r&quot;&gt;{{ $link-&gt;main_filter_selector }}&lt;/td&gt;
                                                  &lt;td class=&quot;p-4 border-r&quot;&gt;{{ $link-&gt;website-&gt;title }}&lt;/td&gt;
                                                  &lt;td class=&quot;p-4 border-r&quot;&gt;{{ $link-&gt;category-&gt;title }}&lt;/td&gt;
                                                  &lt;td class=&quot;p-4 border-r&quot;&gt;&lt;button class=&quot;w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white block m-auto&quot; type=&quot;submit&quot;&gt;Scrape&lt;/button&gt;&lt;/td&gt;
                                                  &lt;td class=&quot;p-4&quot;&gt;&lt;button class=&quot;w-fit h-full p-2 bg-red-400 h-fit rounded hover:bg-red-600 hover:text-white block m-auto&quot; type=&quot;submit&quot;&gt;Verwijderen&lt;/button&gt;
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                          @endforeach
                                      &lt;/table&gt;
                                  &lt;/form&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
     &lt;/main&gt;
     @include('partials.footer')
  &lt;/body&gt;
  </code></pre>
  <p>My routes:</p>
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Route;
  
  use App\Http\Controllers\HomeController;
  use App\Http\Controllers\DashController;
  use App\Http\Controllers\ListviewController;
  use App\Http\Controllers\NewlistController;
  use App\Http\Controllers\BabyController;
  use App\Http\Controllers\ScrapeController;
  use App\Http\Controllers\AdminController;
  use App\Http\Controllers\ItemsController;
  
  /*
  |--------------------------------------------------------------------------
  | Web Routes
  |--------------------------------------------------------------------------
  |
  | Here is where you can register web routes for your application. These
  | routes are loaded by the RouteServiceProvider within a group which
  | contains the &quot;web&quot; middleware group. Now create something great!
  |
  */
  
  require __DIR__.'/auth.php';
  
  Route::get('/', [HomeController::class, 'home'])-&gt;name('home');
  Route::get('/home', [HomeController::class, 'home']);
  
  Route::get('/listview', [ListviewController::class, 'listview'])
      -&gt;name('lists');
  
  
  
  
  
  Route::middleware('auth')-&gt;group (function () 
  
  {   
  
      Route::get('/create', [NewlistController::class, 'newlist'])
          -&gt;name('create');
  
      Route::get('/dashboard/{role}/{id?}', [DashController::class, 'dashboard'])
          -&gt;name('dashboard');
  
      Route::post('/dashboard/{role}/{id}', [NewlistController::class, 'store']);
  
      Route::get('/dashboard/{user_id}/{baby}', [BabyController::class, 'show'])
          -&gt;name('baby');
  
      Route::get('/dashboard/{user_id?}/{baby}/catalogus', [ScrapeController::class, 'show']);
  
  
      Route::get('/admin/{id}', [AdminController::class, 'showAdmin'])
          -&gt;name('admin');
  
      Route::post('/admin/{id}/websites/', [AdminController::class, 'storeWeb'])
          -&gt;name('newWeb');
  
      Route::delete('/admin/{id}/websites/', [AdminController::class, 'destroyWeb'])
          -&gt;name('delWeb');
   
      Route::post('/admin/{id}/categories/', [AdminController::class, 'storeCat'])
          -&gt;name('newCat');
  
      Route::delete('/admin/{id}/categories/', [AdminController::class, 'destroyCat'])
          -&gt;name('delCat');
  
      Route::post('/admin/{id}/links/', [AdminController::class, 'storeLink'])
          -&gt;name('newLink');
  
      Route::delete('/admin/{id}/links/', [AdminController::class, 'destroyLink'])
          -&gt;name('delLink');
  
  });
      
  </code></pre>