You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel搜索结果页面调用编辑与删除按钮技术咨询

Hey there! Let's walk through how to add those Edit and Delete buttons to your search results step by step—no jargon, just straightforward steps you can follow right away.

Step 1: Fix Your Search Controller Method

First, your current search method only queries data but doesn't pass the results to your view. Let's update it to return the search results to a Blade template:

public function search(Request $request) {
    // Initialize empty collection to avoid errors in the view if no search term is entered
    $searchs = collect();

    if ($request->filled('search')) { // Use `filled()` to check for non-empty input
        $searchs = DB::table('domain')
            ->where('Domain','like','%'. $request->search .'%')
            ->orWhere('Registrar','like','%'. $request->search .'%')
            ->orWhere('Login_ID','like','%'. $request->search .'%')
            ->get(); // Don't forget `get()` to fetch the actual results!
    }

    // Replace 'domain.search' with your actual view name (e.g., 'search-results')
    return view('domain.search', compact('searchs'));
}

Step 2: Set Up Routes

Make sure your routes for search, edit, update, and delete are defined in routes/web.php:

// Search route
Route::get('/domain/search', [DomainController::class, 'search'])->name('domain.search');

// Edit & Update routes
Route::get('/domain/{domain}/edit', [DomainController::class, 'edit'])->name('domain.edit');
Route::put('/domain/{domain}', [DomainController::class, 'update'])->name('domain.update');

// Delete route
Route::delete('/domain/{domain}', [DomainController::class, 'destroy'])->name('domain.destroy');

Alternatively, you can use Laravel's resource route to auto-generate all standard CRUD routes in one line:

Route::resource('domain', DomainController::class);

Step 3: Render Results & Buttons in Blade

Now open your search view (e.g., resources/views/domain/search.blade.php) and add this code to display results with Edit/Delete buttons:

<!-- Search Form (add this if you don't have it already) -->
<form action="{{ route('domain.search') }}" method="GET">
    <input 
        type="text" 
        name="search" 
        value="{{ request('search') }}" 
        placeholder="Search Domain/Registrar/Login ID..."
    >
    <button type="submit">Search</button>
</form>

<!-- Search Results Table -->
@if($searchs->isNotEmpty())
    <table>
        <thead>
            <tr>
                <th>Domain</th>
                <th>Registrar</th>
                <th>Login ID</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach($searchs as $domain)
                <tr>
                    <td>{{ $domain->Domain }}</td>
                    <td>{{ $domain->Registrar }}</td>
                    <td>{{ $domain->Login_ID }}</td>
                    <td>
                        <!-- Edit Button -->
                        <a href="{{ route('domain.edit', $domain->id) }}" style="padding: 4px 8px; background: #4CAF50; color: white; text-decoration: none;">Edit</a>

                        <!-- Delete Button (uses form for DELETE request) -->
                        <form 
                            action="{{ route('domain.destroy', $domain->id) }}" 
                            method="POST" 
                            style="display: inline; margin-left: 8px;"
                        >
                            @csrf
                            @method('DELETE') <!-- Laravel's method spoofing for DELETE -->
                            <button 
                                type="submit" 
                                style="padding: 4px 8px; background: #f44336; color: white; border: none; cursor: pointer;"
                                onclick="return confirm('Are you sure you want to delete this record?')"
                            >
                                Delete
                            </button>
                        </form>
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
@else
    <p>No matching records found.</p>
@endif

Step 4: Add Edit/Update/Delete Controller Methods

If you haven't already, add these methods to your DomainController to handle the button actions:

// Show edit form
public function edit($id) {
    $domain = DB::table('domain')->find($id);
    return view('domain.edit', compact('domain'));
}

// Update the record
public function update(Request $request, $id) {
    // Validate input (adjust rules to match your table fields)
    $request->validate([
        'Domain' => 'required|string',
        'Registrar' => 'required|string',
        'Login_ID' => 'required|string',
    ]);

    DB::table('domain')
        ->where('id', $id)
        ->update($request->only(['Domain', 'Registrar', 'Login_ID']));

    return redirect()->route('domain.search')->with('success', 'Record updated successfully!');
}

// Delete the record
public function destroy($id) {
    DB::table('domain')->where('id', $id)->delete();
    return redirect()->route('domain.search')->with('success', 'Record deleted successfully!');
}

If you create a Domain model with php artisan make:model Domain, you can replace DB::table('domain') with Domain:: for cleaner, more maintainable code. For example:

$searchs = Domain::where('Domain','like','%'. $request->search .'%')
    ->orWhere('Registrar','like','%'. $request->search .'%')
    ->orWhere('Login_ID','like','%'. $request->search .'%')
    ->get();

内容的提问来源于stack exchange,提问作者wwkk123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:35:39