Laravel 缺少必需参数

问题描述 投票:0回答:1

缺少 [路由:admin.ads.update] [URI:admin/ads/{ad}] [缺少参数:ad] 的必需参数。

我无法通过 id 参数 {{route('admin.ads.update', $adslist->id)}} 检索数据 我不知道我想念什么,请帮助我,我只是初中生。

Laravel 10 respond

当用户单击编辑链接时,我的代码将执行,Laravel 将使用 id 参数检索数据

index.php

@extends('layouts.admin.master')
@extends('layouts.admin.sidebar')
{{-- @extends('layouts.admin.maincontent') --}}
{{-- @extends('layouts.user.service') --}}
@extends('layouts.admin.footer')
@section('title', 'Kaffe Mini | Admin')

@section('content')

<x-admin.dashboard.nav />
 <!-- Main Content -->
<div id="content">
    <!-- Begin Page Content -->
    <div class="container-fluid">
        <x-admin.dashboard.flash />
        <!-- Page Heading -->
        <div class="d-sm-flex align-items-center justify-content-between mb-4">
            <h1 class="h3 mb-0 text-gray-800">Ads</h1>
            {{-- <a href="#" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i
                    class="fas fa-download fa-sm text-white-50"></i> Generate Report</a> --}}
        </div>
        
            <a href="{{route('admin.ads.create')}}" class="btn btn-success float-right font-weght bold">
                <i class="fa fa-solid fa-plus p-2"></i>Add</a>

                
                <div id="content">
                    <!-- Begin Page Content -->
                    <div class="container-fluid">
                        <!-- Page Heading -->
                        <br>
                            <div class="banner_slide">
                            @foreach($list as $lists)
                            <div class="multi-carousel-item">
                            <div class="image-parent">

                                    <a href="{{route ('admin.ads.edit', $lists->id)}}" class="btn btn-success btn-icon-split float-right m-1">
                                        <span class="icon text-white-50">
                                            <i class="fa fa-pen" style="color: #ffffff;"></i>
                                        </span>
                                    </a>
                                    <form method="POST" action="{{ route ('admin.ads.destroy', $lists->id) }}"
                                        onsubmit="return confirm('Are you sure?')" class="d-inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger btn-icon-split float-right m-1">
                                            <span class="icon text-white-30">
                                                <i class="fas fa-trash"></i>
                                            </span>
                                        </button>
                                    </form>
                            <img
                                src="{{ $lists->ads_image ? asset('storage/' . $lists->ads_image) : asset('img/error/no-image.jpg') }}"
                                class="image-parent w-100" width="150" height="150"
                            />
                               
                            </div>
                            </div>
                            <br>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>
        </div>

@endsection

Web.php



Route::middleware(['auth', 'role:admin'])->name('admin.')->prefix('/admin')->group(function() {
    Route::get('/', [AdminController::class, 'index'])->name('index');
    Route::get('/adduser/invite', [AddUserController::class, 'invite_view'])->name('invite_view');

    Route::post('/roles/{role}/permissions', [RoleController::class, 'assignPermissions'])-  >name('roles.permissions');
    Route::post('/adduser/invite', [AddUserController::class, 'invite'])->name('adduser.invite');
    
    Route::resource('/roles', RoleController::class);
    Route::resource('/permissions', PermissionController::class);
    Route::resource('/adduser', AddUserController::class);
    Route::resource('/catagories', CatagoriesAdminController::class);
    Route::resource('/products', ProductController::class);
    Route::resource('/slide', SlideController::class);
    Route::resource('/orders', OrderController::class);
    Route::resource('/ads', AdsController::class);              //Advertising page
    Route::resource('/settings', SettingsController::class);
});


AdsController.php

<?php

namespace App\Http\Controllers\Shop;


use App\Models\Catagories;
use Illuminate\Http\Request;
use App\Models\Shop\OrderProduct;
use App\Http\Controllers\Controller;
use App\Models\Shop\Advertising;

class AdsController extends Controller
{public function index(){

        return view('admin.ads.index', [
        'count' => OrderProduct::where('status', '=', 'paid')->count(),
        'list' => Advertising::all(),
        'catagories' => Catagories::all()
        ]);
    }

    public function create()
    {

        return view('admin.ads.create', [
        'count' => OrderProduct::where('status', '=', 'paid')->count(),
        'catagories' => Catagories::all()  
        ]);

    }

    Public function store(Request $request) {

        $validated = $request->validate([
            'list_by' => ['min:3'],
            'ads_image' => ['min:3'],
            'promoted_des',
            'catagory' => ['min:3'],
            'subject' => ['min:3'],
            'title' => ['min:3'],
            'title_color' => ['min:3'],
            'subject_color' => ['min:3']
        ]);
        if($request->hasFile('ads_image')){
            //get filename with extension
            $validated['ads_image'] = $request->file('ads_image')->store('ads_image', 'public');
        }
        Advertising::create($validated);
        // dd($validated);
        
        return to_route('admin.ads.index')->with('message', 'New ads Added');

    }

    public function edit(Advertising $adslist)
    {
        $count = OrderProduct::where('status', '=', 'paid')->count();
        $catagories = Catagories::all();
        return view('admin.ads.edit', compact('adslist', 'count', 'catagories'));
    }
    public function update(Request $request, Advertising $adslist) {

        $validated = $request->validate([

            'list_by' => ['min:3'],
            'ads_image' => ['min:3'],
            'promoted_des',
            'catagory' => ['min:3'],
            'subject' => ['min:3'],
            'title' => ['min:3'],
            'title_color' => ['min:3'],
            'subject_color' => ['min:3']

        ]);

        if($request->hasFile('ads_image')){
            //get filename with extension
            $validated['ads_image'] = $request->file('ads_image')->store('ads_image', 'public');
        }
        
        $adslist->update($validated);
        
        return to_route('admin.ads.index')->with('message', 'New Ads Updated');
    }

    public function destroy(Advertising $adslist){
    $adslist->delete();
    return to_route('admin.ads.index')->with('message', 'Advertisement Deleted');

    }
}
php laravel parameter-passing
1个回答
0
投票

你错过了很多东西: 您正在使用的资源路由将生成具有名为

$ad
而不是
$adList
的参数的路由 这个路由称为隐式路由,这意味着在你的控制器中你必须遵守
show/store/update/edit/restore
(doc)

的约定

另请参阅:控制器资源文档

Public function show(Advertising $ad)
Public function store(Request $request, Advertising $ad)
public function update(Request $request, Advertising $ad) 
public function edit(Advertising $ad)
public function destroy(Advertising $ad)
© www.soinside.com 2019 - 2024. All rights reserved.