传递给 App\Http\Controllers\HomeController::productDetail() 的参数 2 必须是 App\Product 的实例,给定字符串

Posted

技术标签:

【中文标题】传递给 App\\Http\\Controllers\\HomeController::productDetail() 的参数 2 必须是 App\\Product 的实例,给定字符串【英文标题】:Argument 2 passed to App\Http\Controllers\HomeController::productDetail() must be an instance of App\Product, string given传递给 App\Http\Controllers\HomeController::productDetail() 的参数 2 必须是 App\Product 的实例,给定字符串 【发布时间】:2019-06-04 23:28:53 【问题描述】:

我有路线

Route::get('catalog/category/product', 'HomeController@productDetail')->name('product.index2');

和控制器

public function productDetail(categories $categories, product $product)

    $products = product::where('active', 1)->get();
    if($product->categories != $categories)
        abort(404);
    
    return view('products', compact('product', 'products'));

我的错误

参数 2 传递给 App\Http\Controllers\HomeController::productDetail() 必须是 App\Product 实例,给定字符串

和前面

<ul class="accordion-menu">
    @foreach ($categories as $item)
        <li>
            <div class="dropdownlink">$item->name <img src=" asset('build/img/d1.svg') " ></div>
            <ul class="submenuItems">
                @foreach($item->children as $subcategory)
                    <li><a href="route('category.index2', $subcategory)"> $subcategory->name </a></li>
                @endforeach

            </ul>
        </li>
    @endforeach                    
</ul>

kernel.php

<?php

命名空间 App\Http;

使用 Illuminate\Foundation\Http\Kernel 作为 HttpKernel;

类内核扩展了 HttpKernel /** * 应用程序的全局 HTTP 中间件堆栈。 * * 这些中间件在对您的应用程序的每个请求期间运行。 * * @var 数组 */ 受保护的 $middleware = [ \App\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \App\Http\Middleware\TrustProxies::class, ];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];

/**
 * The priority-sorted list of middleware.
 *
 * This forces non-global middleware to always be in the given order.
 *
 * @var array
 */
protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\Authenticate::class,
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Illuminate\Auth\Middleware\Authorize::class,
];

【问题讨论】:

你能分享你调用这条路线的代码吗? 您正在使用模型绑定。您能否向我们展示您的 app/Http/Kernel.php 文件以确保您设置了正确的中间件? 请分享您用来调用product.index2 路由的链接。你分享了调用路由category.index2的那个。 【参考方案1】:

您的路线和控制器方法需要 2 个参数,一个是类别对象,另一个是产品对象。但是当你调用 route 时只给一个参数。

Route::get('catalog/category/product', 'HomeController@productDetail')->name('product.index2');

您还需要传递您的产品对象,如下所示,

<li><a href="route('category.index2', ['category'=> $subcategory, 'product'=>$product ])"> $subcategory->name </a></li>

【讨论】:

传递给 App\Http\Controllers\HomeController::productDetail() 的参数 2 必须是 App\Product 的实例,给定字符串【参考方案2】:

你应该试试这个:

你的函数是这样的

public function productDetail(categories $categories, product $product)

    $products = Product::where('active', 1)->get();
    if($product->categories != $categories)
        abort(404);
    
    return view('products', compact('product', 'products'));

你的看法是这样的

<ul class="accordion-menu">
    @foreach ($categories as $item)
        <li>
            <div class="dropdownlink">$item->name <img src=" asset('build/img/d1.svg') " ></div>
            <ul class="submenuItems">
                @foreach($item->children as $subcategory)
                    <li><a href="route('category.index2', [$subcategory,$product)"> $subcategory->name </a></li>// please add second parameter for product
                @endforeach

            </ul>
        </li>
    @endforeach                    
</ul>

注意:可能是您调用了错误的路线,因为您的路线名称是product.index2 而不是category.index2

【讨论】:

以上是关于传递给 App\Http\Controllers\HomeController::productDetail() 的参数 2 必须是 App\Product 的实例,给定字符串的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 目标类 [App\Http\Controllers\App\Http\Controllers\ApiController] 不存在

InvalidArgumentException: Action App\Http\Controllers\App\Http\Controllers\OrderController@store 未定义

Route::controllers - [App\Http\Controllers 上不存在方法 [index]

找不到类“App\Http\Controllers\App\Model”

类 App\Http\Controllers\ManagerLoginController 不存在

未找到 Laravel 5 类“App\Http\Controllers\File”