Laravel

How to Set Meta(Title,Description,Keywords) in Layout file in Laravel livewire

Ravindra Kumar

In Laravel, To set meta tags (title, description, and keywords) dynamically in a Laravel Livewire layout file, follow these steps:

Step 1. Define Meta Properties in the Livewire Component

To add title, description, and keywords properties to your Livewire component and set default values or dynamically update them in the component’s logic.

Example:


<?php

namespace App\Http\Livewire;

use Livewire\Component;

class ExampleComponent extends Component
{
public $title = "This is Dynamic Title";
public $description = "This is description for the page.";
public $keywords = "These are keywords default, keywords";

public function render()
{
return view('livewire.example-component')
->layout('layouts.app', [
'title' => $this->title,
'description' => $this->description,
'keywords' => $this->keywords,
]);
}
}

2. Pass Meta Data to the Layout File

Use the layout() method to pass the meta data when rendering the component.

3. Update the Layout File

Modify your layout file (e.g., resources/views/layouts/app.blade.php) to include meta tags. Use @yield for dynamic values with sensible defaults.

Example:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="{{ $description ?? 'Default description for the site' }}">
<meta name="keywords" content="{{ $keywords ?? 'default, keywords' }}">
<title>{{ $title ?? 'Default Title' }}</title>
</head>
<body>
{{ $slot }}
</body>
</html>

4. Set Meta Data Dynamically in the Component

You can update the meta data dynamically in the Livewire component logic.

Example:


public function mount($data)
{
$this->title = $data['title'] ?? "Dynamic Title";
$this->description = $data['description'] ?? "Dynamic Description";
$this->keywords = $data['keywords'] ?? "dynamic, keywords";
}

5. Optional: Use Blade Components for Meta Tags

To streamline meta tag management, create a Blade component for meta tags.

Create a MetaTags Component:


// resources/views/components/meta-tags.blade.php
<meta name="description" content="{{ $description }}">
<meta name="keywords" content="{{ $keywords }}">
<title>{{ $title }}</title>

Include in Layout:


<x-meta-tags :title="$title ?? 'Default Title'" :description="$description ?? 'Default Description'" :keywords="$keywords ?? 'default, keywords'" />

Result

When you render the Livewire component, the meta tags in your layout file will automatically update based on the values provided.

Laravel-How can I do a filter from multiple dropdown select on the view using one single query

Custom Login and Registration in Laravel

How to Remove public folder from the URL in Laravel

Ravindra Kumar

Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.

Suggested Reading

How to Clear Cache in Laravel Without Using Artisan Commands

When working with Laravel, caching plays a key role in improving your application’s performance by storing compiled configurations, routes, views, and other data. However, during development or after making changes, you may need to clear the cache to see your latest updates. Usually, developers use Artisan commands like php artisan cache:clear or php artisan optimize:clear. […]

Upload (restore/import) a MySQL database backup via the command prompt (CLI)

To upload (restore/import) a MySQL database backup via the command prompt (CLI), you can use the mysql command-line utility. Here’s how: You must follow these requirement You must have a MySQL backup file (e.g., backup.sql) MySQL server should be installed and running You should have credentials for the MySQL user If you are working with […]

How to add new column in laravel migration without losing data

To add a new column in a Laravel migration without losing existing data, you can create a new migration file that alters the existing table. Laravel makes this easy using the php artisan make:migration command with the --table option. Step-by-Step Guide: You can generate a new migration php artisan make:migration add_column_name_to_your_table_name_table --table=your_table_name Replace column_name with […]

imagick php extension must be installed to use this driver in Laravel

It means that your Laravel app (or a package like Intervention Image, Spatie Media Library, or Laravel Snappy) is trying to use the Imagick image processing library, but it’s not installed or enabled on your server. If you’re using Ubuntu/Debian (Linux server): sudo apt-get update sudo apt-get install -y php-imagick sudo service apache2 restart # […]

Attempt to read property “id” on null in Laravel

The error message “Attempt to read property ‘id’ on null” in Laravel typically occurs when you’re trying to access the id property of an object that is null. This commonly happens when you’re expecting an object to be returned (such as a database record), but instead, null is returned. You can handle this null value […]

How to check first record in Laravel blade file

Here’s how to check the first iteration in a Blade @foreach loop: You can use Laravel’s built-in $loop variable: @foreach ($all_categories_ as $vals) <li class="nav-item" role="presentation"> <button class="nav-link p-3 border-0 text-start lh-sm w-100 rounded-0 fs-6 fw-medium {{ $loop->first ? 'active' : '' }}" id="pills-navTab{{ $vals->id }}-tab" data-bs-toggle="pill" data-bs-target="#pills-navTab{{ $vals->id }}" type="button" role="tab" aria-controls="pills-navTab{{ $vals->id }}" […]