Full Stack Software Developer

Laravel install bash script for fortify and laravel-permission

I normally setup my fresh laravel projects with laravel-permission and laravel fortify package. In order to setup the project i have to follow certain steps that are repeatative.

I have now created this simple bash script that installs a fresh laravel installation on a folder and setup the two packages and does all the file modifications by itself.

Below is the bash script which you can also use to create laravel app with fortify and laravel-permission pre-installed.

#!/bin/bash

# Check if folder name is provided
if [ -z "$1" ]; then
  echo "❌ Usage: $0 <folder-name>"
  exit 1
fi

FOLDER_NAME=$1

# Step 1: Create Laravel project
echo "πŸš€ Creating Laravel project in folder: $FOLDER_NAME"
composer create-project laravel/laravel "$FOLDER_NAME"

cd "$FOLDER_NAME" || { echo "❌ Failed to cd into $FOLDER_NAME"; exit 1; }

# Step 2: Install dependencies
echo "πŸ“¦ Installing spatie/laravel-permission..."
composer require spatie/laravel-permission
php artisan migrate

# Step 3: Install fortify
echo "πŸ” Installing laravel/fortify..."
composer require laravel/fortify
php artisan fortify:install
php artisan migrate

# Step 4: Update User model
sed -i "/use Illuminate\\\\Notifications\\\\Notifiable;/a use Spatie\\\\Permission\\\\Traits\\\\HasRoles;" app/Models/User.php
sed -i "s/use HasFactory, Notifiable;/use HasFactory, Notifiable, HasRoles;/" app/Models/User.php

# Step 5: Create UserRoleSeeder
echo "🌱 Creating UserRoleSeeder..."
php artisan make:seeder UserRoleSeeder

cat > database/seeders/UserRoleSeeder.php <<'EOL'
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;

class UserRoleSeeder extends Seeder
{
    public function run(): void
    {
        Role::firstOrCreate(['name' => 'admin']);
        Role::firstOrCreate(['name' => 'member']);
    }
}
EOL

echo "βœ… Seeder created: UserRoleSeeder"

# Step 6: Modify CreateNewUser.php

cat > app/Actions/Fortify/CreateNewUser.php <<'EOL'
<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\CreatesNewUsers;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    /**
     * Validate and create a newly registered user.
     *
     * @param  array<string, string>  $input
     */
    public function create(array $input): User
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                Rule::unique(User::class),
            ],
            'password' => $this->passwordRules(),
        ])->validate();

        $user = User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
        ]);

        $user->assignRole('member');

         return $user;
    }
}
EOL

echo "βœ… User action updated!"

echo "πŸŽ‰ All setup steps completed!"

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.