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

Shiva's avatar

Getting url data to show in the url bar

I'm using Laravel 4 and I'm trying to get the url bar to display the text url that is saved in the database instead of using the id.

This is my routes.php

Route::get('/{id}', function($id = 1){

    if(is_numeric($id))
    {

        $page = Menu::find($id);
        $action = 'content';
        return App::make('HomeController')->$action($id);
    } else {
        $column = 'url';
        $url = Seo::where($column, '=', $id)->get();
        $action = 'show';
        return App::make('HomeController')->$action($url[0]->id);
    }   
});

I'm also using a pivot table to link the menu to the seo.

Seo model

<?php

class Seo extends \Eloquent {
    protected $fillable = array('url', 'meta_title', 'meta_description', 'keywords');
    protected $guarded = array('id');

    protected $table = 'seo';

    public static $rules = array(
        'title' => '',
        'content' => '',
        'image' => ''
    );

    public function menu(){
        return $this->belongsToMany('Menu', 'menu_seo', 'seo_id', 'menu_id');
    }
}

Menu model

<?php

class Menu extends \Eloquent {
    protected $fillable = array('title', 'menu_id', 'image');
    protected $guarded = array('id');

    protected $table = 'menus';

    public static $rules = array(
            
        );

    public function seo(){
        return $this->belongsToMany('Seo', 'menu_seo', 'menu_id', 'seo_id');
    }
}
0 likes
10 replies
Shiva's avatar

The about us would be about-us.html

Shiva's avatar

I have added some stuff to my routes.php function

Route::get('/{id}', function($id = 1){

    if(is_numeric($id))
    {

        echo "hi";
        
        $page = Menu::find($id);
        
        $action = 'content';
        return App::make('HomeController')->$action($id);

    } else {

        echo "bye";
        $column = 'url';

        $seo_url = Seo::where($column, '=', $id)->get();

        foreach($seo_url as $seo){ //Added
            $url_id = $seo->id; //Added
            $url = $seo->url; //Added
        }

        $action = 'content';
        $test = $url; //Added

        return App::make('HomeController')->$action($test);
    }   


});

And I get this error

No query results for model [Menu]. 

I'm not sure how to get passed this error. I've tried adding

$menu = Menu::where('id', $id)->firstOrFail();
@foreach($menu->seo as $seo)
{{ $seo->url }}

@endforeach

and I still got the same error.

SachinAgarwal's avatar

@shiva Not sure of you are asking for this, URL bar will show the current url. So if you want like, be on some url but show some other url in url bar that cannot be done. you have to make a route for that. and redirect there.

SachinAgarwal's avatar

@Shiva This is not possible. You cannot change the URL displayed in the browser. Not only would it be a horrible security practice, it would be a violation of trust to the people visiting your site.
On other hand, you do not need to show the .html part, and create a route for about us page

Route::get('/about-us',function(){
});
Shiva's avatar

@SachinAgarwal - But that wouldn't be user friendly. For example if I had to go and give this to a user and that person ended up deleting about us and then they went and re added the about us the id would be different and then an error would happen.

SachinAgarwal's avatar

@Shiva What you can do is,
Generate this link:

Route::get('/about-us','Controller@show');

then in your controller

public function show()
{
    // TODO get the name of the about us page from the database and view it.
}

Please or to participate in this conversation.