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

JoshWegener's avatar

Handling SEO/Meta Tags in Laravel

Handling SEO/Meta Tags in Laravel question....

What is the best way to handle Meta tags? The problem I'm running into is I have "static" pages like the home page, contact us, about us, and so on. but i also have dynamic, like discussion post. I want to be able to have a global set of meta tags, and a default set of meta tags... then be able to override the meta tags on dynamic pages. I'm thinking about doing a Polymorphic Relationship. something like a Meta data table, then a pages table that holds the static page (matched by slug = name of route). Then to handle the other pages, like a dynamic discussion post, would be posts then morph to the meta table.

Assuming the above is all correct way of doing this, where would I handle the logic to decide what meta tags to render? Middleware? or maybe something like a view composer on the header view partial and let that figure it out... if has name, use page model else if is post, use post table.. and so on.

0 likes
4 replies
Erik's avatar

Depends on how you load your pages but for static meta tags I'd go with a view composer.

Snapey's avatar

When you grab the page content from the database, have another column for meta, and one for description, one for title etc.

You can then @yield in your master template to include the page specific elements .

Have a look at this post https://laracasts.com/discuss/channels/laravel/pass-variable-to-partial-from-base-controller-variable-must-be-global-for-all-pages regarding setting up default view elements and overriding them on a page by page basis.

Please or to participate in this conversation.