php Laravel缺少必需的参数[已关闭]

mklgxw1f  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(108)

已关闭,此问题需要details or clarity。它目前不接受回答。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

昨天关门了。
Improve this question
缺少[Route: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');

    }
}
unguejic

unguejic1#

你错过了很多东西:您正在使用资源路由,它将生成一个名为$ad(而不是$adList)的参数的路由,该路由称为隐式路由,这意味着在您的控制器中必须遵守show/store/update/edit/restore(doc)的约定。
参见:Controller resource 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)

更新:如果你想在控制器中保持相同的参数,你必须更新你的Route::resource(),例如:

Route::resource('/ads', AdsController::class)
->parameters([
    'ad' => 'adList'
]);

相关问题