Make your Laravel Seeder Using Model Factories

Make your Laravel Seeder Using Model Factories

You must have just started development of a Laravel based project and you need to fill it with data. Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory...

You must have just started development of a Laravel based project and you need to fill it with data. Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory...

Database Seeding is important when developing applications whether it's going to be a small app or a large scale application.

Writing Seeders and Model Factories

Table of Contents

  • Pratical Example
  • Generating Model and Migration
  • Write Migration and Model Factory
  • Generating Seeder
  • Write Database Seeder
  • Conclusion

To create a Seeder the make:seeder artisan command is executed. Read Laravel docs here for more instructions

php artisan make:seeder TestSeeder

A model Factory is generated by executing the make:factory artisan command.

php artisan make:factory PostFactory --model=Post

Read more here about using model factories in Laravel.

Pratical Example

Let's seed a simple Film sharing and commenting application, where users can share films belonging to a particular genre. So there will be a User, Film, Comment and Genre Model .

Generating Model and Migration

Laravel default comes with a User model and migration which is good enough for this example. Let's go ahead and create the other models and migration.

php artisan make:model Film -m 
 ## create film model and migration

php artisan make:model Comment -m 
 ## create comment model and migration

php artisan make:model Genre -m
 ## create genre model and migration

Write Migration and Model Factory

User migration and model factory comes prewritten … so we can skip ....

Write Genre Migration and Factory

// Genre Migration
   public function up()
    {
        Schema::create('genres', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

php artisan make:factory GenreFactory --model=Genre

// Genre Factory
<?php

use Faker\Generator as Faker;

$factory->define(App\Genre::class, function (Faker $faker) {
    return [
        'name' => $faker->word,
    ];
});

Write Film Migration and Factory

//Film Migration 
public function up()
    {
        Schema::create('films', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('title');
            $table->text('description');
            $table->date('release_date');
            $table->enum('rating',[ 1, 2, 3, 4, 5] );
            $table->integer('genre_id');
            $table->string('photo');
            $table->string('slug');
            $table->timestamps();

        });
    }

php artisan make:factory FilmFactory --model=Film

<?php

use Faker\Generator as Faker;

$factory->define(App\Film::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence(5),
        'description' => $faker->realText(rand(80, 600)),
        'release_date'  => $faker->date(),
        'rating' => rand(1,5),
        'genre_id' => function () {
            // Get random genre id
            return App\Genre::inRandomOrder()->first()->id;
        },
        'photo'  => 'https://via.placeholder.com/350x150',
        'slug'   => str_replace('--', '-', strtolower(preg_replace('/[^a-zA-Z0-9]/', '-', trim($faker->sentence(5))))),
    ];
});

*Write Comment Migration and Factory *

// Comment Migration
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('film_id');
            $table->text('comment');
            $table->timestamps();
        });
    }

php artisan make:factory CommentFactory --model=Comment

<?php

use Faker\Generator as Faker;

$factory->define(App\Comment::class, function (Faker $faker) {
    return  [
        'comment' => $faker->realText(rand(10, 300)),
      ];
});

Generating Seeder

Write Genre Seeder

php artisan make:seeder GenreTableSeeder

public function run()
    {
        // How many genres you need, defaulting to 10
        $count = (int)$this->command->ask('How many genres do you need ?', 10);

        $this->command->info("Creating {$count} genres.");

        // Create the Genre
        $genres = factory(App\Genre::class, $count)->create();

        $this->command->info('Genres Created!');

    }

Write User and Film Seeder

php artisan make:seeder UserFilmSeeder

Since this is a film sharing application, every user tends to share a certain number of film on this app. So our seeder should look like ....

public function run()
    {
        // How many genres you need, defaulting to 10
        $userCount = (int)$this->command->ask('How many users do you need ?', 10);

        // Ask range for film per user needed
        $r = 0 . '-' . 10;
        $filmRange = $this->command->ask('How many films per user do you need ?', $r);

        $this->command->info("Creating {$userCount} users each having a film range of {$filmRange}.");

        // Create the Users 
        $users = factory(App\User::class, $userCount)->create();

        // Create a range of films for each users
        $users->each(function($user) use ($filmRange){
            factory(App\Film::class, $this->count($filmRange))
                    ->create(['user_id' => $user->id]);
        });

        $this->command->info('Users and Films Created!');

    }

     // Return random value in given range
    function count($range)
    {
        return rand(...explode('-', $range));
    }

Write Comment Seeder

php artisan make:seeder CommentTableSeeder

public function run()
    {
        $r = 0 . '-' . 10;
        $commentRange = $this->command->ask('How many comments per film do you need ?', $r);

        $films = App\Film::all();

        $this->command->info("Creating a range of {$commentRange} comments for {$films->count()} films .");

        $films->each(function($film) use ($commentRange){
            factory(App\Comment::class, $this->count($commentRange))
                ->create([
                       'film_id' => $film->id,
                       'user_id' => App\User::all()->random()->id
                    ]);
        });

        $this->command->info('Comments Created!');

    }

      // Return random value in given range
    function count($range)
    {
        return rand(...explode('-', $range));
    }

Write Database Seeder

Let's invoke generated seeders in the DatabaseSeeder class using the call method

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{

    public function run()
    {
        Eloquent::unguard();

        // Ask for db migration refresh, default is no
        if ($this->command->confirm('Do you wish to refresh migration before seeding, it will clear all old data ?')) {

            // Call the php artisan migrate:fresh using Artisan
            $this->command->call('migrate:fresh');

            $this->command->line("Database cleared.");
        }

        $this->call(GenreTableSeeder::class);
        $this->call(UserFilmSeeder::class);
        $this->call(CommentTableSeeder::class);

        $this->command->info("Database seeded.");

        // Re Guard model
        Eloquent::reguard();

    }
}

Conclusion

If you run php artisan db:seed it will ask questions and seed the data accordingly.

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

In Laravel 6 tutorial, we learn how to use PHP artisan command interface (CLI) to clear the cache from Laravel 6.8 application. How To Clear Cache in Laravel 6.8 Application using Artisan Command Line Interface (CLI)? How to clear route cache using php artisan command? How to easily clear cache in Laravel application? How to clear config cache in PHP Laravel via artisan command? How to clear Laravel view cache? How to Reoptimized class in Laravel via artisan CLI?

Today in this tutorial, we are going to learn how to clear route cache, laravel application cache, config cache, view cache and reoptimized class in a Laravel 6.8 application using artisan command-line interface.

I’m pretty sure many of you may have found yourself gotten into the situation where you do not see changes in the view after making the changes in the app.

Laravel application serves the cached data so caching problem occurs due to the robust cache mechanism of Laravel.

But, if you are still facing this issue, then you do not have to worry further. Let me do the honour of introducing you some of the best artisan commands to remove the cache from your Laravel app via PHP artisan command line interface.

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Table of Contents

  • Clear Route Cache in Laravel
  • Clear Laravel Application Cache
  • Clear Config Cache via PHP Artisan
  • Clear Laravel View Cache
  • Reoptimized Class
Clear Route Cache in Laravel

Laravel caching system also takes routes in consideration, to remove route cache in Laravel use the given below command:

php artisan route:cache
Clear Application Cache in Laravel

Run the following command to clear application cache:

php artisan cache:clear
Clear Config Cache in Laravel

Run the following command to clear config cache:

php artisan config:cache
Clear View Cache in Laravel

Run the following command to clean your view cache:

php artisan view:clear
Reoptimize Class

Run the below command to reoptimize the class loader:

php artisan optimize

Conclusion

We have completed this Laravel 6 tutorial, In this tutorial we learned how to use php artisan command to clear the cache from your Laravel application. We have answered the following questions in this article.

  • How to clear route cache using php artisan command?
  • How to easily clear cache in Laravel application?
  • How to clear config cache in PHP Laravel via artisan command?
  • How to clear Laravel view cache?
  • How to Reoptimized class in Laravel via artisan CLI?

Now, it’s your time to let me know what do you think about this laravel 6 article. Go forth and try these super awesome artisan commands and let me know how these commands are helping you.

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather. Laravel Weather is a good package which we can use to get weather data. It's a wrapper around Open Weather Map API (Current weather). A wrapper around Open Weather Map API (Current weather)

🌤️ A wrapper around Open Weather Map API (Current weather)

Installation

You can install the package via composer:

source-shell
composer require gnahotelsolutions/laravel-weather
Usage
text-html-php
$weather = new Weather();

// Checking weather by city name
$currentWeatherInGirona = $weather->get('girona,es');

// You can use the city id, this will get you unambiguous results
$currentWeatherInGirona = $weather->find('3121456');

Units

By default the package uses metric for Celsius temperature results, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inUnits('imperial')->get('girona,es');

Language

By default the package uses es for the description translation, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inLanguage('en')->get('girona');

Guzzle Client Instance

If you need to use another instance of Guzzle, to modify headers for example:

text-html-php
$weather = new Weather();

$guzzle = $this->getSpecialGuzzleClient();

$currentWeatherInGirona = $weather->using($guzzle)->get('girona');

Testing

source-shell
composer test

Laravel Sweet Alert | How to use the SweetAlert package in Laravel PHP

Laravel Sweet Alert | How to use the SweetAlert package in Laravel PHP

Sweet Alert is a laravel wrapper around #sweetalert javascript library for showing alert or notification message. This is a great package created by rashid, and this laravel package is very famous because it has tonnes of features you can choose from.

Sweet Alert is a laravel wrapper around #sweetalert javascript library for showing alert or notification message.

This is a great package created by rashid, and this laravel package is very famous because it has tonnes of features you can choose from.