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

webfuelcode's avatar

Route error on live server but localhost runs fine

I have checked for the same path on both (live shared hosting and on localhost).

Localhost works well but the live server shows route errors on the specific path only. Rest of the path runs fine.

Missing required parameters for [Route: code]
.....
.../resources/views/pages/single.blade.php)

I have not moved the folders. I created a .htaccess file in the root.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>

How it is possible the whole path works fine but a single path shows the error? I even looked for the slug if there is a missing (null). but the slug is available...

0 likes
21 replies
Sinnbeck's avatar

Can you show some code? Hard to guess based on what you have shown :)

Sinnbeck's avatar

@webfuelcode can you try adding this just above the route call?

@if (!$code->slug) 
    @dd($code)
@endif
tykus's avatar

@webfuelcode the slug is empty for at least one of the Model instances in the loop.; I.e. a data issue!

Sinnbeck's avatar

@webfuelcode So it was triggered by that if statement, even though it has a slug ? Do you have some sort of accessor on the model then? Or did you forget the if statement I added?

tykus's avatar

@Sinnbeck the better approach would be to examine the instance’s slug, which will be empty/falsey

@if (!$code->slug) 
    @dd([$code->slug, $code->getRawOriginal('slug')])
@endif
Sinnbeck's avatar

@tykus Ah yeah thats a good point 👍 Btw. No need for array syntax in dd() :)

tykus's avatar

@Sinnbeck just my preference putting everything in an array rather than getting individual dump output for each variable.

webfuelcode's avatar

@Sinnbeck I used (!empty($code->slug)) in the place (!$code->slug)

@if (!empty($code->slug)) 
    @dd($code)
@endif

My model file is

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Code extends Model
{
    protected $fillable = ['village', 'officename', 'pincode', 'subdistname', 'districtname', 'statename', 'slug'];

    public function getRouteKeyName()
    {
        return 'slug';
    }
}
Sinnbeck's avatar

@webfuelcode That means you swapped its meaning. If you want to use empty, remove !

@if (empty($code->slug)) 
tykus's avatar

@webfuelcode you're beyond helping... this is a data issue like I mentioned almost an hour ago - make sure the there is a valid, non-empty slug for every item in Collection, or don't try to display a link; adding a simple check like this around the link is all you need:

@foreach ($samedist as $code)
    <div class="list-group-item list-group-item-action">
        <div class="d-flex justify-content-between">
            <div>{{$code->village}}, {{$code->subdistname}}</div>
            <div>
                @if ($code->slug) <!-- add this -->
                    <a href="{{route('code', $code->slug)}}">
                        {{$code->pincode}}
                    </a>
                @endif <!-- and this -->
            </div>
        </div>
    </div
@endforeach
tykus's avatar

Seems more likely a data issue than logic; can you tell us the connection between slug and code? How/where are you using the route?

tykus's avatar

@webfuelcode how many different $code variables are you using in the view??? You have three difference loops in the small snippet from the error page; and another outside any loop! Are you sure you're working with the right instance???

{{$code->pincode}}. <!-- one ( inside loop) -->
<h3 class="mt-3 px-2 text-warning">Shares the district {{$code->districtname}}</h3>  <!-- two (outside loop) -->
 @foreach ($samedist as $code) <!-- three (inside loop) -->
@foreach ($samepincodes as $code) <!-- four (inside loop) -->
webfuelcode's avatar

@tykus Controller here

public function show(Code $code)
    {
        $pin = $code->pincode;
        $subdist = $code->subdistname;
        $dist = $code->districtname;
        $state = $code->statename;
        $samepincodes = Code::where('pincode', $pin)->get();
        $samesubdist = Code::where('subdistname', $subdist)->get();
        $samedist = Code::where('districtname', $dist)->get();
        return view('pages.single', compact('code', 'samepincodes', 'samesubdist', 'samedist'));
    }
tykus's avatar

@webfuelcode irrelevant for now; it doesn't prove that all of the records actually have a non-empty slug.

webfuelcode's avatar

@tykus Yes, a slug is present for that particular data. I have checked by phpmyadmin table for that post. I have even displayed the slug on the view page.

A similar error happened for a different route but new files made it good but this error happened. I am just can not find where to look for...

tykus's avatar

@webfuelcode the problem is with record(s) in the $samedist Collection. Again, are you sure you're working with the correct $code instance?

Note that you are overwriting the original $code variable passed to the view whenever you name the current loop item $code as well! If you were more discerning with your variable names, it would help, e.g.

@foreach ($samedist as $sameDistCode)
  <div>{{$sameDistCode->village}}, {{$sameDistCode->subdistname}}</div>
  @if ($code->slug)
    <a href="{{route('code', $code->slug)}}">
      {{$code->pincode}}
    </a>
  @endif 
@endforeach

Last time, are you sure that all of the Code instances in the $samedist Collection have a slug? Does it work with the if check around the link like I showed above?

Please or to participate in this conversation.