<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Laravel Tuts</title>
    <description>The latest articles on Forem by Laravel Tuts (@laraveltuts).</description>
    <link>https://forem.com/laraveltuts</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F896977%2F0a63aa33-2f34-47eb-bcfc-62e9bd2922be.jpg</url>
      <title>Forem: Laravel Tuts</title>
      <link>https://forem.com/laraveltuts</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/laraveltuts"/>
    <language>en</language>
    <item>
      <title>How to Automatically Generate an XML Sitemap in Laravel</title>
      <dc:creator>Laravel Tuts</dc:creator>
      <pubDate>Sun, 11 Dec 2022 09:38:07 +0000</pubDate>
      <link>https://forem.com/laraveltuts/how-to-automatically-generate-an-xml-sitemap-in-laravel-om4</link>
      <guid>https://forem.com/laraveltuts/how-to-automatically-generate-an-xml-sitemap-in-laravel-om4</guid>
      <description>&lt;p&gt;&lt;strong&gt;December 11, 2022・Originally published at &lt;a href="https://laraveltuts.com/how-to-automatically-generate-an-xml-sitemap-in-laravel/" rel="noopener noreferrer"&gt;laraveltuts.com&lt;/a&gt;・8 min read&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How often should I update my sitemap? What should I include in it? Should I create a new sitemap every time. I add or remove a page from my site?&lt;/p&gt;

&lt;p&gt;The sitemap is a file that contains links to all pages on your site. This helps search engines crawl your site better and index your pages faster.&lt;/p&gt;

&lt;p&gt;Sitemaps are essential for SEO (search engine optimization). They allow search engines to crawl your entire site and index its contents.&lt;/p&gt;

&lt;p&gt;We are going to create a XML Sitemap for our posts using a &lt;strong&gt;spatie/laravel-sitemap&lt;/strong&gt; package.&lt;/p&gt;

&lt;p&gt;Follow the below Step-by-Step Guide on how to automatically generate an XML Sitemap in Laravel Application.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Automatically Generate an XML Sitemap in Laravel
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: Install Laravel Application&lt;/li&gt;
&lt;li&gt;Step 2: Create Database and Configure&lt;/li&gt;
&lt;li&gt;Step 3: Installing spatie/laravel-sitemap Package&lt;/li&gt;
&lt;li&gt;Step 4: Creating Post Model, Migration and Controller&lt;/li&gt;
&lt;li&gt;Step 5: Create Dummy Records for Post&lt;/li&gt;
&lt;li&gt;Step 6: Create Sitemap Command&lt;/li&gt;
&lt;li&gt;Step 7: Scheduled Command in Console Kernel&lt;/li&gt;
&lt;li&gt;Step 8: Testing&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1: Install Laravel Application
&lt;/h3&gt;

&lt;p&gt;Firstly, we will install a laravel application. To install a laravel application run the following code in terminal as show in figure.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer create-project --prefer-dist laravel/laravel laravel-sitemap

cd laravel-sitemap


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07qxrjnwoqicx7is3f13.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07qxrjnwoqicx7is3f13.png" alt="Install Laravel Application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Create Database and Configure
&lt;/h3&gt;

&lt;p&gt;Let create database in phpmyadmin for our project for adding posts and then we will create those posts sitemap later.&lt;/p&gt;

&lt;p&gt;We are creating a database with name &lt;strong&gt;“laravel-sitemap”&lt;/strong&gt; as you can see in below image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb1zg9fz2w5mtfnt72wa1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb1zg9fz2w5mtfnt72wa1.png" alt="Create Database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After creating database we are going to configure it to our laravel application. To configure the database in laravel. First open .env from our project root directory and enter the database details as show below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-sitemap
DB_USERNAME=root
DB_PASSWORD=


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvg2bkwr3128wlfzsicjt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvg2bkwr3128wlfzsicjt.png" alt="Database Configure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now run the migration to create a default tables like users table etc.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan migrate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fub6vvgnm8hnmev0s35sp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fub6vvgnm8hnmev0s35sp.png" alt="Database Migration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Installing spatie/laravel-sitemap Package
&lt;/h3&gt;

&lt;p&gt;We are using spatie/laravel-sitemap package to generate our posts sitemap. We are going to install the package via composer using a following command in terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer require spatie/laravel-sitemap


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94w9ufdwxotiymo9tru1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94w9ufdwxotiymo9tru1.png" alt="Installing spatie/laravel-sitemap Package"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The package will automatically register itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configuration Package&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can override the default options for the crawler. First publish the configuration:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will copy the default config to &lt;strong&gt;config/sitemap.php&lt;/strong&gt; where you can edit it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

use GuzzleHttp\RequestOptions;
use Spatie\Sitemap\Crawler\Profile;
return [
    /*
     * These options will be passed to GuzzleHttp\Client when it is created.
     * For in-depth information on all options see the Guzzle docs:
     *
     * http://docs.guzzlephp.org/en/stable/request-options.html
     */
    'guzzle_options' =&amp;gt; [
        /*
         * Whether or not cookies are used in a request.
         */
        RequestOptions::COOKIES =&amp;gt; true,
        /*
         * The number of seconds to wait while trying to connect to a server.
         * Use 0 to wait indefinitely.
         */
        RequestOptions::CONNECT_TIMEOUT =&amp;gt; 10,
        /*
         * The timeout of the request in seconds. Use 0 to wait indefinitely.
         */
        RequestOptions::TIMEOUT =&amp;gt; 10,
        /*
         * Describes the redirect behavior of a request.
         */
        RequestOptions::ALLOW_REDIRECTS =&amp;gt; false,
    ],

    /*
     * The sitemap generator can execute JavaScript on each page so it will
     * discover links that are generated by your JS scripts. This feature
     * is powered by headless Chrome.
     */
    'execute_javascript' =&amp;gt; false,

    /*
     * The package will make an educated guess as to where Google Chrome is installed. 
     * You can also manually pass it's location here.
     */
    'chrome_binary_path' =&amp;gt; '',
    /*
     * The sitemap generator uses a CrawlProfile implementation to determine
     * which urls should be crawled for the sitemap.
     */
    'crawl_profile' =&amp;gt; Profile::class,

];


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 4: Creating Post Model, Migration and Controller
&lt;/h3&gt;

&lt;p&gt;We are going to create posts XML sitemaps. So we are going to create post model, migration and controller for it.&lt;/p&gt;

&lt;p&gt;By running the following command its will create all three model migration and model for post.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan make:model Post -mc


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;-mc&lt;/strong&gt; will create a migraion and controller.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faap65bfwnavsl860f6tm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faap65bfwnavsl860f6tm.png" alt="Creating Post Model, Migration and Controller"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we are going to update it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Models/Post.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    use HasFactory;
    protected $fillable = [
        'title', 
        'slug', 
        'description'
    ];
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foaqu082m3w94atus5fsn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foaqu082m3w94atus5fsn.png" alt="Update Post Model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;database/migrations/create_posts_table.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table-&amp;gt;id();
            $table-&amp;gt;string('title');
            $table-&amp;gt;string('slug');
            $table-&amp;gt;text('description'); 
            $table-&amp;gt;timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbtlp847cgbv30dkoieu8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbtlp847cgbv30dkoieu8.png" alt="Update Post Migration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We don’t need to update &lt;strong&gt;PostController.php&lt;/strong&gt; file for this tutorial because we are going to add dummy records for posts using faker in our next step.&lt;/p&gt;

&lt;p&gt;If you want to add, edit, update or to display posts you can using PostController.php. You can check out our article Laravel 9 Vue JS CRUD App using Vite Example. We had discuss the CRUD in deatils.&lt;/p&gt;

&lt;p&gt;Run the migration to create our new post table.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan migrate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 5: Create Dummy Records for Post
&lt;/h3&gt;

&lt;p&gt;Now we are going to add some dummy records to the post table so that we can generate a XML sitemap for posts.&lt;/p&gt;

&lt;p&gt;You can skip this part if you already create a post.&lt;/p&gt;

&lt;p&gt;So, now we are going to create factory for our post model using the following command.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan make:factory PostFactory --model=Post


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fci22358qdns3yzj9d3ma.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fci22358qdns3yzj9d3ma.png" alt="Creating Post Factory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Update the &lt;strong&gt;database/factories/PostFactory.php&lt;/strong&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory&amp;lt;\App\Models\Post&amp;gt;
 */
class PostFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Post::class;
    /**
     * Define the model's default state.
     *
     * @return array&amp;lt;string, mixed&amp;gt;
     */
    public function definition()
    {
        return [
            'title' =&amp;gt; $this-&amp;gt;faker-&amp;gt;sentence($nbWords = 6, $variableNbWords = true),
            'slug' =&amp;gt; $this-&amp;gt;faker-&amp;gt;randomNumber($nbDigits = NULL, $strict = false),
            'description' =&amp;gt; $this-&amp;gt;faker-&amp;gt;text($maxNbChars = 2000)
        ];
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdo3h02rcshmg4x6cb3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdo3h02rcshmg4x6cb3i.png" alt="Update Post Factory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note for slug we are using some random numbers.&lt;/p&gt;

&lt;p&gt;Now we are going to generate some random post using tinker.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan tinker
Post::factory()-&amp;gt;count(30)-&amp;gt;create()


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;30&lt;/em&gt;&lt;/strong&gt; is the total number of post we want to generate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlv3y210w2c47v7aflta.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlv3y210w2c47v7aflta.png" alt="Dummy Records Create for Post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you can Exit from tinker using exit command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

exit


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 6: Creating Sitemap Command
&lt;/h3&gt;

&lt;p&gt;You can easily create an artisan command to create a sitemap and schedule it to run frequently. This will ensure that new pages and content types will be automatically picked up. To create a sitemap command run the following command.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan make:command GenerateSitemap


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7upl06oa42kn23v52yai.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7upl06oa42kn23v52yai.png" alt="Create Sitemap Command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Update the sitemap command with the following code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Console/Commands/GenerateSitemap.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
use App\Models\Post;
class GenerateSitemap extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sitemap:generate';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Automatically Generate an XML Sitemap';
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $postsitmap = Sitemap::create();
        Post::get()-&amp;gt;each(function (Post $post) use ($postsitmap) {
            $postsitmap-&amp;gt;add(
                Url::create("/post/{$post-&amp;gt;slug}")
                    -&amp;gt;setPriority(0.9)
                    -&amp;gt;setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY)
            );
        });
        $postsitmap-&amp;gt;writeToFile(public_path('sitemap.xml'));
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;sitemap:generate is our command to generate the sitemap.&lt;/li&gt;
&lt;li&gt;“setPriority” — set the post priority.&lt;/li&gt;
&lt;li&gt;“setChangeFrequency” — set the post change frequency with daily, monthly, yearly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can read other options at spatie/laravel-sitemap package.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw7tqhsisgv9ojkr9ugz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw7tqhsisgv9ojkr9ugz3.png" alt="Update Sitemap Command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7: Scheduled Command in Console Kernel
&lt;/h3&gt;

&lt;p&gt;Now we nee to register this command to kernel for automatically generate XML sitemap. It can be scheduled in the console kernel to be run daily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Console/Kernel.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    ...
    $schedule-&amp;gt;command('sitemap:generate')-&amp;gt;daily();
    ...
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbibs4gxb5gm2yhw4v1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbibs4gxb5gm2yhw4v1n.png" alt="Update Kernel File for Schedule Sitemap Automatically"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 8: Testing
&lt;/h3&gt;

&lt;p&gt;We are all set, the sitemap is automatically set to be run daily. To set for now we are going to run the command to generate our sitemap manually by running the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan sitemap:generate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above command will generate the sitemap.xml in public directory.&lt;/p&gt;

&lt;p&gt;Run the following command to start the Artisan development server for laravel.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan serve


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And open the following link in any web browser to open the sitemap.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

http://127.0.0.1:8000/sitemap.xml


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fix6o5ihgdn3u5jdbb9xs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fix6o5ihgdn3u5jdbb9xs.png" alt="How to Automatically Generate an XML Sitemap in Laravel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This concludes our tutorial on How to Automatically Generate an XML Sitemap in Laravel. We hope you found it helpful. If you did, please share this article with your friends or family and leave us a comment to let us know what you think and stay tuned for more tutorials. If you like the tutorial please subscribe our &lt;a href="https://www.youtube.com/channel/UCwIbFoGoht3Dom9Q1NkOS3A" rel="noopener noreferrer"&gt;youtube&lt;/a&gt; channel and follow us on social network &lt;a href="https://www.facebook.com/laraveltutscom" rel="noopener noreferrer"&gt;facebook &lt;/a&gt;and &lt;a href="https://www.instagram.com/laraveltuts/" rel="noopener noreferrer"&gt;instagram&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>sitemap</category>
      <category>seo</category>
      <category>xml</category>
    </item>
    <item>
      <title>Laravel 9 Vue JS CRUD App using Vite Example</title>
      <dc:creator>Laravel Tuts</dc:creator>
      <pubDate>Sun, 24 Jul 2022 06:52:00 +0000</pubDate>
      <link>https://forem.com/laraveltuts/laravel-9-vue-js-crud-app-using-vite-example-7n5</link>
      <guid>https://forem.com/laraveltuts/laravel-9-vue-js-crud-app-using-vite-example-7n5</guid>
      <description>&lt;p&gt;July 24, 2022, Originally published at &lt;a href="https://laraveltuts.com/laravel-9-vue-js-crud-app-using-vite-example/" rel="noopener noreferrer"&gt;laraveltuts.com&lt;/a&gt;・9 min read&lt;/p&gt;

&lt;p&gt;Today, We are going to learn &lt;strong&gt;Laravel 9 Vue JS CRUD App using Vite Example&lt;/strong&gt;. This tutorial will cover on laravel 9 Vue Js CRUD application using Vite latest laravel update with example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vite&lt;/strong&gt; is a modern frontend build tool that provides an extremely fast development environment and bundles your code for production. When building applications with Laravel, you will typically use Vite to bundle your application’s CSS and JavaScript files into production ready assets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vue.js&lt;/strong&gt; is an open-source model–view–view model front end JavaScript framework for building user interfaces and single-page applications. It was created by Evan You, and is maintained by him and the rest of the active core team members.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps for Laravel 9 Vue JS CRUD App using Vite Example:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: Installing fresh new Laravel 9 Application&lt;/li&gt;
&lt;li&gt;Step 2: Creating Database and Configuration&lt;/li&gt;
&lt;li&gt;Step 3: Creating Auth with Breeze&lt;/li&gt;
&lt;li&gt;Step 4: Creating Migration and Model&lt;/li&gt;
&lt;li&gt;Step 5: Creating Route&lt;/li&gt;
&lt;li&gt;Step 6: Creating Controller&lt;/li&gt;
&lt;li&gt;Step 7: Creating Vue Pages&lt;/li&gt;
&lt;li&gt;Step 8: Testing&lt;/li&gt;
&lt;li&gt;Step 9: Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1: Installing fresh new Laravel 9 Application
&lt;/h3&gt;

&lt;p&gt;First, we are installing a fresh new laravel 9 application. To install a laravel application run the following command in terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer create-project laravel/laravel vuejs-crud-vite
cd vuejs-crud-vite


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note: “&lt;strong&gt;vuejs-crud-vite&lt;/strong&gt;” is our laravel application name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faz9dvom1s6o585wrw4gz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faz9dvom1s6o585wrw4gz.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Creating Database and Configuration
&lt;/h3&gt;

&lt;p&gt;Now, we will create a database. First open phpmyadmin and create a database with name “&lt;strong&gt;vuejs-crud-vite&lt;/strong&gt;” (you may use anything you like).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwweovoohk5ohbruuugws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwweovoohk5ohbruuugws.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we are going to add the database details to the laravel application. Open &lt;strong&gt;.env&lt;/strong&gt; file and enter the database details.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=vuejs-crud-vite
DB_USERNAME=root
DB_PASSWORD=


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgwo85574tib6qfvjfrps.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgwo85574tib6qfvjfrps.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Creating Auth with Breeze
&lt;/h3&gt;

&lt;p&gt;Now we are going to create auth with breeze. Run the following command in terminal to install breeze library.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer require laravel/breeze --dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;create authentication with the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan breeze:install vue


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Install Node JS package.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now run the vite command and make it keep running.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm run dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note: If error occurs then updated node to v16.16.0 and it worked&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now open new terminal and run the migration.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan migrate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 4: Creating Migration and Model
&lt;/h3&gt;

&lt;p&gt;Here, We are going to create a migration for Posts table. To create a posts migration run the following command.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan make:migration create_posts_table


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;add the following fields to posts migration file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table-&amp;gt;id();
            $table-&amp;gt;string('title');
            $table-&amp;gt;text('body');
            $table-&amp;gt;timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxcpqzmkhul0278akui1v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxcpqzmkhul0278akui1v.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the migration again to create posts table.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan migrate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So now, We will create Post model. To create a Post model run the following command in terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan make:model Post


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And add the following code to &lt;strong&gt;Post.php&lt;/strong&gt; model.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    use HasFactory;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title', 'body'
    ];
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5osg6gst9870mw8vtpf7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5osg6gst9870mw8vtpf7.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Creating Route
&lt;/h3&gt;

&lt;p&gt;Now we will create a resources route for our CRUD application. Add the following route to &lt;strong&gt;routes/web.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return Inertia::render('Welcome', [
        'canLogin' =&amp;gt; Route::has('login'),
        'canRegister' =&amp;gt; Route::has('register'),
        'laravelVersion' =&amp;gt; Application::VERSION,
        'phpVersion' =&amp;gt; PHP_VERSION,
    ]);
});
Route::get('/dashboard', function () {
    return Inertia::render('Dashboard');
})-&amp;gt;middleware(['auth', 'verified'])-&amp;gt;name('dashboard');
require __DIR__.'/auth.php';
Route::resource('posts', PostController::class);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5i0expw0ipubm4ts2g97.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5i0expw0ipubm4ts2g97.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6: Creating Controller
&lt;/h3&gt;

&lt;p&gt;In this step, we are going to create a Post Controller. Create a new file &lt;strong&gt;PostController.php&lt;/strong&gt; in &lt;strong&gt;app/Http/Controllers&lt;/strong&gt; folder and add the following code inside.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\Post;
use Illuminate\Support\Facades\Validator;

class PostController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function index()
    {
        $posts = Post::all();
        return Inertia::render('Posts/Index', ['posts' =&amp;gt; $posts]);
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function create()
    {
        return Inertia::render('Posts/Create');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function store(Request $request)
    {
        Validator::make($request-&amp;gt;all(), [
            'title' =&amp;gt; ['required'],
            'body' =&amp;gt; ['required'],
        ])-&amp;gt;validate();

        Post::create($request-&amp;gt;all());

        return redirect()-&amp;gt;route('posts.index');
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function edit(Post $post)
    {
        return Inertia::render('Posts/Edit', [
            'post' =&amp;gt; $post
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function update($id, Request $request)
    {
        Validator::make($request-&amp;gt;all(), [
            'title' =&amp;gt; ['required'],
            'body' =&amp;gt; ['required'],
        ])-&amp;gt;validate();

        Post::find($id)-&amp;gt;update($request-&amp;gt;all());
        return redirect()-&amp;gt;route('posts.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function destroy($id)
    {
        Post::find($id)-&amp;gt;delete();
        return redirect()-&amp;gt;route('posts.index');
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1eylo3ir09fc12p1ugx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1eylo3ir09fc12p1ugx.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7: Creating Vue Pages
&lt;/h3&gt;

&lt;p&gt;In this last step we are create a Vue Js Pages — &lt;strong&gt;Index.vue&lt;/strong&gt;, &lt;strong&gt;Create.vue&lt;/strong&gt; and &lt;strong&gt;Edit.vue&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Create a new folder &lt;strong&gt;Posts&lt;/strong&gt; inside &lt;strong&gt;resources/js/Pages&lt;/strong&gt; and create below pages inside &lt;strong&gt;Posts&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpxlizdn54pfai8r03wa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpxlizdn54pfai8r03wa.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;resources/js/Pages/Posts/Index.vue&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;script setup&amp;gt;
import BreezeAuthenticatedLayout from '@/Layouts/Authenticated.vue';
import { Head, Link, useForm } from '@inertiajs/inertia-vue3';
defineProps({
    posts: Array,
});
const form = useForm();
function destroy(id) {
    if (confirm("Are you sure you want to Delete")) {
        form.delete(route('posts.destroy', id));
    }
}
&amp;lt;/script&amp;gt;
&amp;lt;template&amp;gt;
    &amp;lt;Head title="Dashboard" /&amp;gt;
    &amp;lt;BreezeAuthenticatedLayout&amp;gt;
        &amp;lt;template #header&amp;gt;
            &amp;lt;h2 class="font-semibold text-xl text-gray-800 leading-tight"&amp;gt;
                Laravel 9 Vue JS CRUD App using Vite Example - LaravelTuts.com
            &amp;lt;/h2&amp;gt;
        &amp;lt;/template&amp;gt;
        &amp;lt;div class="py-12"&amp;gt;
            &amp;lt;div class="max-w-7xl mx-auto sm:px-6 lg:px-8"&amp;gt;
                &amp;lt;div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"&amp;gt;
                    &amp;lt;div class="p-6 bg-white border-b border-gray-200"&amp;gt;
                        &amp;lt;div className="flex items-center justify-between mb-6"&amp;gt;
                            &amp;lt;Link
                                className="px-6 py-2 text-white bg-green-500 rounded-md focus:outline-none"
                                :href="route('posts.create')"
                            &amp;gt;
                                Create Post
                            &amp;lt;/Link&amp;gt;
                        &amp;lt;/div&amp;gt;
                        &amp;lt;table className="table-fixed w-full"&amp;gt;
                            &amp;lt;thead&amp;gt;
                                &amp;lt;tr className="bg-gray-100"&amp;gt;
                                    &amp;lt;th className="px-4 py-2 w-20"&amp;gt;No.&amp;lt;/th&amp;gt;
                                    &amp;lt;th className="px-4 py-2"&amp;gt;Title&amp;lt;/th&amp;gt;
                                    &amp;lt;th className="px-4 py-2"&amp;gt;Body&amp;lt;/th&amp;gt;
                                    &amp;lt;th className="px-4 py-2"&amp;gt;Action&amp;lt;/th&amp;gt;
                                &amp;lt;/tr&amp;gt;
                            &amp;lt;/thead&amp;gt;
                            &amp;lt;tbody&amp;gt;
                                &amp;lt;tr v-for="post in posts"&amp;gt;
                                    &amp;lt;td className="border px-4 py-2"&amp;gt;{{ post.id }}&amp;lt;/td&amp;gt;
                                    &amp;lt;td className="border px-4 py-2"&amp;gt;{{ post.title }}&amp;lt;/td&amp;gt;
                                    &amp;lt;td className="border px-4 py-2"&amp;gt;{{ post.body }}&amp;lt;/td&amp;gt;
                                    &amp;lt;td className="border px-4 py-2"&amp;gt;
                                        &amp;lt;Link
                                            tabIndex="1"
                                            className="px-4 py-2 text-sm text-white bg-blue-500 rounded"
                                            :href="route('posts.edit', post.id)"
                                        &amp;gt;
                                            Edit
                                        &amp;lt;/Link&amp;gt;
                                        &amp;lt;button
                                            @click="destroy(post.id)"
                                            tabIndex="-1"
                                            type="button"
                                            className="mx-1 px-4 py-2 text-sm text-white bg-red-500 rounded"
                                        &amp;gt;
                                            Delete
                                        &amp;lt;/button&amp;gt;
                                    &amp;lt;/td&amp;gt;
                                &amp;lt;/tr&amp;gt;
                            &amp;lt;/tbody&amp;gt;
                        &amp;lt;/table&amp;gt;
                    &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/BreezeAuthenticatedLayout&amp;gt;
&amp;lt;/template&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;resources/js/Pages/Posts/Create.vue&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;script setup&amp;gt;
import BreezeAuthenticatedLayout from '@/Layouts/Authenticated.vue';
import BreezeLabel from '@/Components/Label.vue';
import BreezeInput from '@/Components/Input.vue';
import BreezeTextArea from '@/Components/Textarea.vue';
import { Head, Link, useForm } from '@inertiajs/inertia-vue3';
const form = useForm({
    title: '',
    body: ''
});
const submit = () =&amp;gt; {
    form.post(route('posts.store'));
};
&amp;lt;/script&amp;gt;
&amp;lt;template&amp;gt;
    &amp;lt;Head title="Dashboard" /&amp;gt;
    &amp;lt;BreezeAuthenticatedLayout&amp;gt;
        &amp;lt;template #header&amp;gt;
            &amp;lt;h2 class="font-semibold text-xl text-gray-800 leading-tight"&amp;gt;
                Create Post
            &amp;lt;/h2&amp;gt;
        &amp;lt;/template&amp;gt;
        &amp;lt;div class="py-12"&amp;gt;
            &amp;lt;div class="max-w-7xl mx-auto sm:px-6 lg:px-8"&amp;gt;
                &amp;lt;div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"&amp;gt;
                    &amp;lt;div class="p-6 bg-white border-b border-gray-200"&amp;gt;
                        &amp;lt;div className="flex items-center justify-between mb-6"&amp;gt;
                            &amp;lt;Link
                                className="px-6 py-2 text-white bg-blue-500 rounded-md focus:outline-none"
                                :href="route('posts.index')"
                            &amp;gt;
                                Back
                            &amp;lt;/Link&amp;gt;
                        &amp;lt;/div&amp;gt;
                        &amp;lt;form name="createForm" @submit.prevent="submit"&amp;gt;
                                &amp;lt;div className="flex flex-col"&amp;gt;
                                    &amp;lt;div className="mb-4"&amp;gt;
                                        &amp;lt;BreezeLabel for="title" value="Title" /&amp;gt;

                                        &amp;lt;BreezeInput 
                                            id="title" 
                                            type="text" 
                                            class="mt-1 block w-full" 
                                            v-model="form.title" 
                                            autofocus /&amp;gt;
                                        &amp;lt;span className="text-red-600" v-if="form.errors.title"&amp;gt;
                                            {{ form.errors.title }}
                                        &amp;lt;/span&amp;gt;
                                    &amp;lt;/div&amp;gt;
                                    &amp;lt;div className="mb-4"&amp;gt;
                                        &amp;lt;BreezeLabel for="body" value="Body" /&amp;gt;

                                        &amp;lt;BreezeTextArea 
                                            id="body" 
                                            class="mt-1 block w-full" 
                                            v-model="form.body" 
                                            autofocus /&amp;gt;
                                        &amp;lt;span className="text-red-600" v-if="form.errors.body"&amp;gt;
                                            {{ form.errors.body }}
                                        &amp;lt;/span&amp;gt;
                                    &amp;lt;/div&amp;gt;
                                &amp;lt;/div&amp;gt;

                                &amp;lt;div className="mt-4"&amp;gt;
                                    &amp;lt;button
                                        type="submit"
                                        className="px-6 py-2 font-bold text-white bg-green-500 rounded"
                                    &amp;gt;
                                        Save
                                    &amp;lt;/button&amp;gt;
                                &amp;lt;/div&amp;gt;
                            &amp;lt;/form&amp;gt;
                    &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/BreezeAuthenticatedLayout&amp;gt;
&amp;lt;/template&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;resources/js/Pages/Posts/Edit.vue&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;script setup&amp;gt;
import BreezeAuthenticatedLayout from '@/Layouts/Authenticated.vue';
import BreezeLabel from '@/Components/Label.vue';
import BreezeInput from '@/Components/Input.vue';
import BreezeTextArea from '@/Components/Textarea.vue';
import { Head, Link, useForm } from '@inertiajs/inertia-vue3';
const props = defineProps({
    post: Object,
});
const form = useForm({
    title: props.post.title,
    body: props.post.body
});
const submit = () =&amp;gt; {
    form.put(route('posts.update', props.post.id));
};
&amp;lt;/script&amp;gt;
&amp;lt;template&amp;gt;
    &amp;lt;Head title="Dashboard" /&amp;gt;
    &amp;lt;BreezeAuthenticatedLayout&amp;gt;
        &amp;lt;template #header&amp;gt;
            &amp;lt;h2 class="font-semibold text-xl text-gray-800 leading-tight"&amp;gt;
                Edit Post
            &amp;lt;/h2&amp;gt;
        &amp;lt;/template&amp;gt;
        &amp;lt;div class="py-12"&amp;gt;
            &amp;lt;div class="max-w-7xl mx-auto sm:px-6 lg:px-8"&amp;gt;
                &amp;lt;div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"&amp;gt;
                    &amp;lt;div class="p-6 bg-white border-b border-gray-200"&amp;gt;
                        &amp;lt;div className="flex items-center justify-between mb-6"&amp;gt;
                            &amp;lt;Link
                                className="px-6 py-2 text-white bg-blue-500 rounded-md focus:outline-none"
                                :href="route('posts.index')"
                            &amp;gt;
                                Back
                            &amp;lt;/Link&amp;gt;
                        &amp;lt;/div&amp;gt;
                        &amp;lt;form name="createForm" @submit.prevent="submit"&amp;gt;
                                &amp;lt;div className="flex flex-col"&amp;gt;
                                    &amp;lt;div className="mb-4"&amp;gt;
                                        &amp;lt;BreezeLabel for="title" value="Title" /&amp;gt;

                                        &amp;lt;BreezeInput 
                                            id="title" 
                                            type="text" 
                                            class="mt-1 block w-full" 
                                            v-model="form.title" 
                                            autofocus /&amp;gt;
                                        &amp;lt;span className="text-red-600" v-if="form.errors.title"&amp;gt;
                                            {{ form.errors.title }}
                                        &amp;lt;/span&amp;gt;
                                    &amp;lt;/div&amp;gt;
                                    &amp;lt;div className="mb-4"&amp;gt;
                                        &amp;lt;BreezeLabel for="body" value="Body" /&amp;gt;

                                        &amp;lt;BreezeTextArea 
                                            id="body" 
                                            class="mt-1 block w-full" 
                                            v-model="form.body" 
                                            autofocus /&amp;gt;
                                        &amp;lt;span className="text-red-600" v-if="form.errors.body"&amp;gt;
                                            {{ form.errors.body }}
                                        &amp;lt;/span&amp;gt;
                                    &amp;lt;/div&amp;gt;
                                &amp;lt;/div&amp;gt;

                                &amp;lt;div className="mt-4"&amp;gt;
                                    &amp;lt;button
                                        type="submit"
                                        className="px-6 py-2 font-bold text-white bg-green-500 rounded"
                                    &amp;gt;
                                        Save
                                    &amp;lt;/button&amp;gt;
                                &amp;lt;/div&amp;gt;
                            &amp;lt;/form&amp;gt;
                    &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/BreezeAuthenticatedLayout&amp;gt;
&amp;lt;/template&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create a new file &lt;strong&gt;Textaarea.vue&lt;/strong&gt; inside &lt;strong&gt;resources/js/Components&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;resources/js/Components/Textarea.vue&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;script setup&amp;gt;
    import { onMounted, ref } from 'vue';
    defineProps(['modelValue']);
    defineEmits(['update:modelValue']);
    const input = ref(null);
    onMounted(() =&amp;gt; {
        if (input.value.hasAttribute('autofocus')) {
            input.value.focus();
        }
    });
&amp;lt;/script&amp;gt;
&amp;lt;template&amp;gt;
    &amp;lt;textarea class="border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm" :value="modelValue" @input="$emit('update:modelValue', $event.target.value)" ref="input"&amp;gt;&amp;lt;/textarea&amp;gt;
&amp;lt;/template&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add a Post Navigation in &lt;strong&gt;Authenticated.vue&lt;/strong&gt; inside &lt;strong&gt;resources/js/Layouts&lt;/strong&gt; folder.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;!-- Navigation Links --&amp;gt;
&amp;lt;div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex"&amp;gt;
  &amp;lt;BreezeNavLink :href="route('dashboard')" :active="route().current('dashboard')"&amp;gt;Dashboard&amp;lt;/BreezeNavLink&amp;gt;
  &amp;lt;BreezeNavLink :href="route('posts.index')" :active="route().current('posts.index')"&amp;gt;Posts&amp;lt;/BreezeNavLink&amp;gt;
&amp;lt;/div&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dqfod947qtzpwinnt7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dqfod947qtzpwinnt7z.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 8: Testing
&lt;/h3&gt;

&lt;p&gt;Now let’s test our Laravel 9 Vue JS CRUD App using Vite Example. Run the following command to start laravel server.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan serve


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Also run Vite in new terminal and keep it running.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm run dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;or build.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm run build


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now open any web browser and enter the following link to test the application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

http://127.0.0.1:8000/


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note: Register a new user and then click posts from navigation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Preview:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgh8eis74zwfkaqprig4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgh8eis74zwfkaqprig4.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnny83r08o5snlsklsx2f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnny83r08o5snlsklsx2f.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg4cwmgmnb3np0tph27yv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg4cwmgmnb3np0tph27yv.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkajlqgk2v7zbar0civsu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkajlqgk2v7zbar0civsu.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91abbf7tvj3kg2rviiav.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91abbf7tvj3kg2rviiav.png" alt="Laravel 9 Vue JS CRUD App using Vite Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 9: Conclusion
&lt;/h3&gt;

&lt;p&gt;Today, We had learn &lt;strong&gt;Laravel 9 Vue JS CRUD App using Vite Example&lt;/strong&gt;. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our &lt;a href="https://www.youtube.com/channel/UCwIbFoGoht3Dom9Q1NkOS3A" rel="noopener noreferrer"&gt;YouTube Channel&lt;/a&gt; and follow us on social network &lt;a href="https://www.facebook.com/laraveltutscom" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; and &lt;a href="https://www.instagram.com/laraveltuts/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>vue</category>
      <category>vite</category>
      <category>crud</category>
    </item>
  </channel>
</rss>
