我有路线
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') }}" alt="Банковские терминалы"></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;
class Kernel扩展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,
];
}
答案 0 :(得分:0)
您的路线和您的控制器方法期望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>
答案 1 :(得分:0)
您应该尝试以下操作:
您这样的功能
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') }}" alt="Банковские терминалы"></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