1
votes

I have route

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

and controller

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'));
}

my error

Argument 2 passed to App\Http\Controllers\HomeController::productDetail() must be an instance of App\Product, string given

and front

<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

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel { /** * The application's global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */ protected $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,
];

}

2
Can you share your code where you called this route ?Md.Sukel Ali
You are using Model Bindings. Could you show us your app/Http/Kernel.php file just to make sure you have the correct middlewares set up?Mozammil
Please share the link <a></a> that you use to call the product.index2 route. you shared the one that calls the route category.index2.Omer Abdelmajeed

2 Answers

0
votes

Your route and your controller method expect 2 parameter one is category object and the other is product object. But when you call route give only one parameter.

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

you need to pass your product object also like below,

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

You should try this:

Your function like this

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'));
}

Your view like this

<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>

Note: May be you call wrong route because your route name is product.index2 not category.index2