Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jagood's avatar

Laravel problem "Missing required parameters for [Route"

Hello, I am trying to create a multilingual project, I have a problem with routing and with the "show" function, I'm getting an error:

after entering the address: projekt.test/en/cars/2/

ErrorException (E_ERROR) Trying to get property 'brand' of non-object (View: /var/www/projekt/resources/views/cars/show.blade.php)

after entering the address: projekt.test/pl/categories/3

ErrorException (E_ERROR) Missing required parameters for [Route: carsCategories] [URI: {locale}/categories/{id}]. (View: /var/www/projekt/resources/views/template.blade.php) (View: /var/www/projekt/resources/views/template.blade.php)

web.php

Route::get('/', function () {
    return redirect(app()->getLocale());
});
 
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'SetLocale'], function() {
 
    Route::get('/', function () {
        return view('welcome');
    });
 
    Route::get('cars/', 'CarController@index')->name('Car');
 
    Route::get('cars/{id}/', 'CarController@show')->name('cars');
 
    Route::get('cars/categories/{id}', 'CarController@CarsByCategory')->name('carCategories');
});

CarController.php

<?php
 
namespace App\Http\Controllers;
 
use App;
 
use Illuminate\Http\Request;
 
use App\Models\Car;
 
use App\Repositories\CarRepository;
 
class CarController extends Controller
{
    public function index(CarRepository $carRepository, $locale){
        
        $cars = $carRepository->getAllCars();
        App::setLocale($locale);
        session()->put('locale', $locale);
        return view('cars.list',["CarsList"=>$cars,
                                "title"=>"ok"]);
    }
 
    public function show(CarRepository $carRepository, $id, $locale){
        
        $carDetail = $carRepository->find($id);
        App::setLocale($locale);
        session()->put('locale', $locale);
        return view('cars.show', ["carDetail" => $carDetail]);
    }
 
    public function CarsByCategory(CarRepository $carRepository, $id){
 
        $cars = $carRepository->getCarsByCategory($id);
 
        return view('cars.list',["CarsList"=>$cars,
                                "title"=>"ok"]);
    }
}

CarRepository.php

<?php

namespace App\Repositories;

use App\Models\Car;

use DB;

class CarRepository extends BaseRepository {

    public function __construct(Car $model){
        $this->model = $model;
    }

    public function getAllCars(){

        return $this->model->where('status', 'online')->get();
    }

    public function getCarsByCategory($id){
        return $this->model->where('status', 'online')->whereHas('category',
        function ($query) use ($id) 
        {
            $query->where('categories.id',$id);
        })->orderBy('marka','asc')->get();
    }
}

show.blade.php

<div class="card-header">
        {{ $carDetail->marka }}
    </div>

I do not know how to fix it

0 likes
5 replies
charlozard's avatar

after entering the address: projekt.test/en/cars/2/

ErrorException (E_ERROR) Trying to get property 'brand' of non-object (View: /var/www/projekt/resources/views/cars/show.blade.php)

You seem to trying to get the brand of the car but the car is probably null. Does the car id exist in your database?

after entering the address: projekt.test/pl/categories/3

ErrorException (E_ERROR) Missing required parameters for [Route: carsCategories] [URI: {locale}/categories/{id}]. (View: /var/www/projekt/resources/views/template.blade.php) (View: >/var/www/projekt/resources/views/template.blade.php)

You're probably using the route() function in template.blade.php without the parameter. You can pass a parameter by adding a second argument with an array. route('carsCategories', ['id' => 1])

https://laravel.com/docs/5.8/helpers#method-route

jagood's avatar

sorry not "brand" only "marka".

ErrorException (E_ERROR) Trying to get property 'marka' of non-object (View: /var/www/projekt/resources/views/cars/show.blade.php)

it worked before, like adding $locale it stopped.

in the templete.blade.php I have the code to change the language and it causes an error:

<ul class="navbar-nav ml-auto">
                        <!-- Authentication Links -->
                        @foreach (config('app.available_locales') as $locale)
                            <li class="nav-item">
                                <a class="nav-link"
                                   href="{{ Route(\Illuminate\Support\Facades\Route::currentRouteName(), $locale) }}"
                                    @if (app()->getLocale() == $locale) style="font-weight: bold; text-decoration: underline" @endif>{{ strtoupper($locale) }}</a>
                            </li>
                        @endforeach
   </ul>
charlozard's avatar

@jagood

sorry not "brand" only "marka".

in your show() method in the CarController you are most likely not finding the car causing you to pass null to the view instead of your car object. You might want to check out what's in the $id variable and verify that you should be getting a result with that id. You could be passing the wrong value. Else I'd check your find logic to see if there's anything wrong there.

in the templete.blade.php I have the code to change the language and it causes an error:

You are calling the current route name and passing it the new locale, however the route seems to require more than one parameter. You could try something like the example below to fetch the current route parameters and merge the new locale into it. I would consider moving this logic to some kind of helper though because it gets kind of messy.

route(
    \Illuminate\Support\Facades\Route::currentRouteName(), 
    array_merge(\Illuminate\Support\Facades\Route::current()->parameters(), ['locale' => 'en'])
)
N4DA's avatar

I faced the same problem if by chance you solve it please tell me I'm stuck

Murodjon_Makhmudov's avatar

I have exactly the same problem. I'm sending parameter inside route() function getting result of parameter inside controller show() function. But Iti gives error or not found

Please or to participate in this conversation.