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

shahr's avatar
Level 10

Missing required parameter for [Route: company.index] [URI: company/{companySlug}] [Missing parameter: companySlug].

articles table

public function up()
{
    Schema::create('articles', function (Blueprint $table) {
        $table->id();
        $table->foreignId('user_id')->constrained()->cascadeOnDelete();
        $table->string('title');
        $table->string('slug');
        $table->string('image')->nullable();
        $table->text('body');
        $table->timestamps();
    });
}

companies table

public function up()
{
    Schema::create('companies', function (Blueprint $table) {
        $table->id();
        $table->foreignId('user_id')->constrained()->cascadeOnDelete();
        $table->foreignId('unity_id')->constrained()->cascadeOnDelete();
        $table->foreignId('article_id')->constrained()->cascadeOnDelete();
        $table->string('title');
        $table->string('slug');
        $table->string('image')->nullable();
        $table->text('body');
        $table->timestamps();
    });
}

Company.php

use HasFactory, Sluggable;

protected $guarded = [];

public function user()
{
    return $this->belongsTo(User::class);
}

public function unity()
{
    return $this->belongsTo(Unity::class);
}

public function sluggable(): array
{
    return [
        'slug' => [
            'source' => 'title'
        ]
    ];
}

public function path()
{
    return '/company/'.$this->slug;
}

Article.php

use HasFactory, Sluggable;

protected $guarded = [];

public function user()
{
    return $this->belongsTo(User::class);
}

public function sluggable(): array
{
    return [
        'slug' => [
            'source' => 'title'
        ]
    ];
}

public function path()
{
    return '/article/'.$this->slug;
}

ArticleController.php

public function single(Article $article)
{
    $cities = City::query()->where('state_id', 26)->get();
    return view('Home.contents.article', compact('article', 'cities'));
}

CompanyController.php

public function single(Company $company)
{
    //$company = Company::query()->where('');
    return view('Home.contents.company', compact('company'));
}

article.blade.php

<div class="container bg-white pt-5 pb-5">
    <div class="row">
        <h2>{{ $article->title }}</h2>
        <div class="d-flex justify-content-center p-5">
            <img src="{{ asset('storage/'.$article->image) }}" alt="{{ $article->title }}" class="img-fluid">
        </div>
        <div class="ps-5 pe-5 mb-3">
            {{ $article->body }}
        </div>
        <div class="ps-5 pe-5">
            <form action="{{ route('company.index') }}" method="get">
                <div class="row mb-3">
                    <div class="col-lg-3">
                        <label for="city_id" class="form-label">city_id</label>
                        <select class="form-select" id="city_id" name="city_id">
                            @foreach($cities as $city)
                                <option value="{{ $city->id }}">{{ $city->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-lg-3">
                        <label for="company_id" class="form-label">company_id</label>
                        <select class="form-select" id="company_id" name="company_id">

                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="col-lg-3">
                        <button type="submit" class="btn btn-warning btn-lg">Continue</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

company.blade.php

<div class="container bg-white pt-5 pb-5">
    <div class="row">
        <h2>{{ $company->title }}</h2>
        <div class="d-flex justify-content-center p-5">
            <img src="{{ asset('storage/'.$company->image) }}" alt="{{ $company->title }}" class="img-fluid">
        </div>
        <div class="ps-5 pe-5 mb-3">
            {{ $company->body }}
        </div>
    </div>
</div>

web.php

Route::get('/article/{articleSlug}', [App\Http\Controllers\ArticleController::class, 'single']);
Route::get('/company/{companySlug}', [App\Http\Controllers\CompanyController::class, 'single'])->name('company.index');

RouteServiceProvider.php

Route::bind('companySlug', function ($value) {
    return Company::whereSlug($value)->firstOrFail();
});
Route::bind('articleSlug', function ($value) {
    return Article::whereSlug($value)->firstOrFail();
});
0 likes
9 replies
Ben Taylor's avatar

Try this

<form action="{{ route('company.index', $company) }}" method="get">
1 like
shahr's avatar
Level 10

@Ben Taylor

It has an error

Undefined variable $company

Ben Taylor's avatar

Yes. Your route is requiring a company slug. If you are not passing the company model through to your view, you might want to change the route to this (especially since it is an index route)

Route::get('/companies', [App\Http\Controllers\CompanyController::class, 'single'])->name('company.index');

You might also want to stick to the normal naming conventions for your controller methods.

1 like
shahr's avatar
Level 10

@Ben Taylor

It has an error again

Undefined variable $company

what's the solution?

Ben Taylor's avatar

Show the full error from the laravel.log file please

1 like
Ben Taylor's avatar

If you changed your route to no longer require the company slug then revert your form tag to how you had it.

<form action="{{ route('company.index') }}" method="get">
1 like
Ben Taylor's avatar
Level 35

Ok, so now that your aren't using the company slug in the url, you can't use route model binding to get the Company model in your controller. You will have to get the model from the database yourself

$company = Company::where('id', request()->company_id)->first();
1 like

Please or to participate in this conversation.