LARAVEL: foreach中的所有元素的id都是1,这使得它们不可能被删除



我的处境很困难。我正在用scraper构建一个出生列表克隆,但很难编写代码,因为我没有经验。我已经设法创建了一个管理页面与多种形式插入网站,类别和链接形式,在那里你可以指定什么链接应该做的抓取和什么网站和类别它将被链接。然而,添加这些东西是没有问题的,但删除它们是不工作的。

我的网站,类别和链接都显示在我的管理页面上,每个元素都有一个抓取和删除按钮。当我删除该行的最后一项时,它删除了第一个元素。当我想要删除其他东西时,laravel抛出一个异常,它试图尝试读取null上的属性,这意味着它不再存在。当我转储并在delete函数之前死亡时,每个列表中的每个项都具有id '1'。这就是它删除一行中的第一项的原因。有人能帮帮我吗?

我认为这是因为请求删除项目的id是从url中检索的,而url中给出的id是用户id,为1。如果有人能给我一些建议确保我能以不同的方式给出用户id。让我知道!我的代码:

Admincontroller:

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsWebsite;
use AppModelsCategory;
use AppModelsLink;
use GoutteClient;

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' => $request->input('site'),
'url' => $request->input('url')
]);
return back();
}
public function destroyWeb(Request $request, $id)
{
$web = Website::find($id);
$web->delete();
return back();
}
public function storeCat(Request $request)
{
$cat = Category::create([
'title' => $request->input('title')
]);
return back();
}
public function destroyCat(Request $request, $id)
{
$cat = Category::find($id)->first();
$cat->delete();
return back();
}
public function storeLink(Request $request) 
{
$link = Link::create([
'url' => $request->input('scrapeUrl'),
'main_filter_selector' => $request->input('filter'),
'website_id' => $request->input('website_id'),
'category_id' => $request->input('category_id'),
]);
return back();
}
public function destroyLink(Request $request, $id)
{
$link = Link::find($id);
dd($link);
$link->delete();
return back();
}

public function scrape(Request $request)
{
$link = Link::find($request->link_id);
$scraper = new Scraper (new Client());
$scraper->handle($link);
if($scraper->status == 1) {
return response()->json(['status' => 1, 'msg' => 'Scraping done']);
} else {
return response()->json(['status' => 2, 'msg' => $scraper->status]);
};
}
}

我adminpage:


<body class="relative min-h-screen">
@include('partials.header')
<main class="pb-20">
<div class="text-center m-auto text-2xl p-4 w-fit border-b">
<h2>Welkom <strong>{{ auth()->user()->name }}</strong></h2>
</div>
<div class="gap-10 m-auto xl:w-3/5">
<div class="mt-8">
<h3 class="bg-gray-100 p-4"><strong>Winkels</strong></h3>
<div class="">
<div class="p-4 m-auto">
<form action="{{ route('newWeb', Auth()->user()) }}" method="POST">
@csrf
<div class="flex wrap flex-row items-center justify-left my-4">
<label for="shop" class="w-2/12">Webshop:</label>
<input type="text" required class="w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100" name="site" id="titel" placeholder="Dreambaby">
</div>
<div class="flex wrap flex-row items-center justify-left">
<label for="url" class="w-2/12">
Voeg een url toe:
</label>
<input type="url" required class="w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100" name="url" id="url" placeholder="http://dreambaby.com/">
<button class="w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white mx-2" name="shop" value="add" type="submit">Voeg link toe</button>
</div>
</form>
<div class="flex wrap flex-row items-center mt-12">
<div class="flex flex-row flex-wrap w-full">
<table class="table-auto">
@foreach ($websites as $web)
<form action="{{ route('delWeb', Auth()->user()) }}" method="POST">
@csrf
@method('DELETE')
<tr class="border-b">
<td class="p-4">{{ $web->title }}</td>
<td class="p-4">{{ $web->id }}</td>
<td class="p-4"><button class="w-fit h-full p-2 bg-red-400 h-fit rounded hover:bg-red-600 hover:text-white block m-auto" type="submit">Verwijderen</button>
</td>
</tr>
</form>
@endforeach
</table>
</div>
</div>
</div>
</div>   
</div>
</div>           
<div class="gap-10 m-auto xl:w-3/5">
<div class="mt-8">
<h3 class="bg-gray-100 p-4"><strong>Categorieën</strong></h3>
<div class="mt-4">
<div class="p-4 m-auto">
<form action="{{ route('newCat', Auth()->user()) }}" method="POST">
@csrf
<div class="flex wrap flex-row items-center justify-left">
<label for="url" class="w-2/12">
Voeg een categorie toe:
</label>
<input type="text" required class="w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100" name="title" id="cat" placeholder="Eten en Drinken">
<button class="w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white mx-2" name="category_submit" type="submit">toevoegen</button>
</div>
</form>
<div class="flex wrap flex-row items-center mt-12">
<div class="flex flex-row flex-wrap w-full">
<table class="table-auto">
@foreach ($categories as $category)
<form action="{{ route('delCat', Auth()->user()), $category->id }}" id="{{ $category->id }}" method="POST">
@csrf
@method('DELETE')
<tr class="border-b">
<td class="p-4">{{ $category->title }}</td>
<td class="p-4">{{ $category->id }}</td>
<td class="p-4"><button class="w-fit h-full p-2 bg-red-400 h-fit rounded hover:bg-red-600 hover:text-white block m-auto" type="submit">Verwijderen</button>
</td>
</tr>
</form>
@endforeach
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="gap-10 m-auto xl:w-3/5">
<div class="mt-8">
<h3 class="bg-gray-100 p-4"><strong>Scrapes</strong></h3>
<div class="">
<div class="p-4 m-auto">
<form action="{{ route('newLink', Auth()->user()) }}" method="POST">
@csrf
<div class="flex wrap flex-row items-center justify-left my-4">
<label for="scrape" class="w-2/12">Url:</label>
<input type="text" required class="w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100" name="scrapeUrl" id="scrapetitel" placeholder="https://www.thebabyscorner.be/nl-be/baby_eten_en_drinken/">
</div>
<div class="flex wrap flex-row items-center justify-left my-4">
<label for="text" class="w-2/12">
Filter:
</label>
<input type="text" required class="w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100" name="filter" id="url" placeholder=".1-products-item">
</div>
<div class="flex wrap flex-row items-center justify-left my-4">
<label for="webpicker" class="w-2/12">Winkel:</label>
<select name="website_id" id="" class="w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100">
<option value="#" disabled selected>Kies je winkel</option>
@foreach ($websites as $web)
<option value="{{ $web->id }}" >{{ $web->title }}</option>
@endforeach
</select>
</div>
<div class="flex wrap flex-row items-center justify-left my-4">
<label for="webpicker" class="w-2/12">Categorie:</label>
<select name="category_id" id="" class="w-7/12 h-fit p-2 focus:outline-sky-500 rounded bg-slate-100">
<option value="#" disabled selected>Kies je categorie</option>
@foreach ($categories as $cat)
<option value="{{ $cat->id }}" >{{ $cat->title }}</option>
@endforeach
</select>
<button class="w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white mx-2" type="submit" name="scrape_submit">toevoegen</button>
</div>
</form>
<div class="flex wrap flex-row items-center mt-12 w-full">
<div class="flex flex-row flex-wrap w-full">
<form action="{{ route('delLink', Auth()->user()) }}" method="POST">
@csrf
@method('DELETE')
<table class="table-auto w-full">
<tr class="bg-slate-300">
<td class="p-4"><strong>Url</strong></td>
<td class="p-4"><strong>Filter selector</strong></td>
<td class="p-4"><strong>Website</strong></td>
<td class="p-4"><strong>Categorie</strong></td>
<td></td>
<td></td>
</tr>
@foreach ($links as $link)
<tr data-id="{{ $link->id }}" class="">
<td class="p-4 border-r">{{ $link->url }}</td>
<td class="p-4 border-r">{{ $link->main_filter_selector }}</td>
<td class="p-4 border-r">{{ $link->website->title }}</td>
<td class="p-4 border-r">{{ $link->category->title }}</td>
<td class="p-4 border-r"><button class="w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white block m-auto" type="submit">Scrape</button></td>
<td class="p-4"><button class="w-fit h-full p-2 bg-red-400 h-fit rounded hover:bg-red-600 hover:text-white block m-auto" type="submit">Verwijderen</button>
</td>
</tr>
@endforeach
</table>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
@include('partials.footer')
</body>

我的路线:

<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersHomeController;
use AppHttpControllersDashController;
use AppHttpControllersListviewController;
use AppHttpControllersNewlistController;
use AppHttpControllersBabyController;
use AppHttpControllersScrapeController;
use AppHttpControllersAdminController;
use AppHttpControllersItemsController;
/*
|--------------------------------------------------------------------------
| 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 "web" middleware group. Now create something great!
|
*/
require __DIR__.'/auth.php';
Route::get('/', [HomeController::class, 'home'])->name('home');
Route::get('/home', [HomeController::class, 'home']);
Route::get('/listview', [ListviewController::class, 'listview'])
->name('lists');


Route::middleware('auth')->group (function () 
{   
Route::get('/create', [NewlistController::class, 'newlist'])
->name('create');
Route::get('/dashboard/{role}/{id?}', [DashController::class, 'dashboard'])
->name('dashboard');
Route::post('/dashboard/{role}/{id}', [NewlistController::class, 'store']);
Route::get('/dashboard/{user_id}/{baby}', [BabyController::class, 'show'])
->name('baby');
Route::get('/dashboard/{user_id?}/{baby}/catalogus', [ScrapeController::class, 'show']);

Route::get('/admin/{id}', [AdminController::class, 'showAdmin'])
->name('admin');
Route::post('/admin/{id}/websites/', [AdminController::class, 'storeWeb'])
->name('newWeb');
Route::delete('/admin/{id}/websites/', [AdminController::class, 'destroyWeb'])
->name('delWeb');

Route::post('/admin/{id}/categories/', [AdminController::class, 'storeCat'])
->name('newCat');
Route::delete('/admin/{id}/categories/', [AdminController::class, 'destroyCat'])
->name('delCat');
Route::post('/admin/{id}/links/', [AdminController::class, 'storeLink'])
->name('newLink');
Route::delete('/admin/{id}/links/', [AdminController::class, 'destroyLink'])
->name('delLink');
});

替换此代码

<form action="{{ route('delLink', Auth()->user()) }}" method="POST">
@csrf
@method('DELETE')
<table class="table-auto w-full">
<tr class="bg-slate-300">
<td class="p-4"><strong>Url</strong></td>
<td class="p-4"><strong>Filter selector</strong></td>
<td class="p-4"><strong>Website</strong></td>
<td class="p-4"><strong>Categorie</strong></td>
<td></td>
<td></td>

</tr>
@foreach ($links as $link)
<tr data-id="{{ $link->id }}" class="">
<td class="p-4 border-r">{{ $link->url }}</td>
<td class="p-4 border-r">{{ $link->main_filter_selector }}</td>
<td class="p-4 border-r">{{ $link->website->title }}</td>
<td class="p-4 border-r">{{ $link->category->title }}</td>
<td class="p-4 border-r"><button class="w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white block m-auto" type="submit">Scrape</button></td>
<td class="p-4"><button class="w-fit h-full p-2 bg-red-400 h-fit rounded hover:bg-red-600 hover:text-white block m-auto" type="submit">Verwijderen</button>
</td>
</tr>
@endforeach
</table>
</form>

<table class="table-auto w-full">
<tr class="bg-slate-300">
<td class="p-4"><strong>Url</strong></td>
<td class="p-4"><strong>Filter selector</strong></td>
<td class="p-4"><strong>Website</strong></td>
<td class="p-4"><strong>Categorie</strong></td>
<td></td>
<td></td>

</tr>
@foreach ($links as $link)
<tr data-id="{{ $link->id }}" class="">
<td class="p-4 border-r">{{ $link->url }}</td>
<td class="p-4 border-r">{{ $link->main_filter_selector }}</td>
<td class="p-4 border-r">{{ $link->website->title }}</td>
<td class="p-4 border-r">{{ $link->category->title }}</td>
<td class="p-4 border-r"><button class="w-fit bg-green-400 h-fit p-2 rounded hover:bg-green-600 hover:text-white block m-auto" type="submit">Scrape</button></td>
<td class="p-4">
<form action="{{ route('delLink', $link->id) }}" method="POST">
@csrf
@method('DELETE')
<button class="w-fit h-full p-2 bg-red-400 h-fit rounded hover:bg-red-600 hover:text-white block m-auto" type="submit">Verwijderen</button>
</form>
</td>
</tr>
@endforeach
</table>

你的destroyLink()方法应该包含这个

public function destroyLink(Request $request)
{  
$id = $request->id;
$link = Link::find($id);
$link->delete();
return back();
}

相关内容

最新更新