Updated V2 - 3/3/2024

  1. Create the laravel Jetstream application with a bash script
  2. Open terminal to where you want to install this app.
  3. rm -f laravel-with-jetstream-bash-script.sh
  4. curl -o ./laravel-with-jetstream-bash-script.sh https://www.webune.com/forums/laravel-with-jetstream-bash-script.html
  5. chmod +x laravel-with-jetstream-bash-script.sh
  6. ./laravel-with-jetstream-bash-script.sh

Optional versions:

  • 5112: https://www.webune.com/forums/laravel-with-login.html
  • 5154: https://www.webune.com/forums/create-deploy-new-laravel-react-app-login-breeze-inertia-authentication.html
  • 5293: https://www.webune.com/forums/new-mdb-laravel-inertia-breeze-to-vite-react-application-project.html

 

Previous Version:

https://www.webune.com/forums/create-deploy-new-laravel-react-app-login-breeze-inertia-authentication.html

Old Version 1

The following are commands you can execute to create a laravel with jetstream. Otherwise, you can create a bash script which is provided at thebottom of this page.

Source: https://jetstream.laravel.com/installation.html

  1. $ mkdir app-name
  2. $ cd app-name
  3. $ composer require laravel/jetstream
  4. $ php artisan jetstream:install livewire
  5. $ npm run build
  6. $ php artisan migrate

 Bash Script

#!/bin/bash

# CREATE A NEW LARAVEL WITH JETSTREAM
#https://youtu.be/WLY196kq07U
#Laravel 9 Jetstream API Demo
#commands:
read -p "1. Enter App Name: " APP_NAME
#composer create-project --prefer-dist laravel/laravel $DBNAME
#or
composer create-project laravel/laravel $APP_NAME
cd $APP_NAME
composer require laravel/jetstream
php artisan jetstream:install livewire

code .
code .env

EDIT_FILE_NAME=".env"
DBNAME_DEFAULT="laravel"
read -p "Please create a database in Mysql with PHPMyAdmin. Enter database Name: [$DBNAME_DEFAULT] " DBNAME
if [[ $DBNAME == "" ]]; then
    DBNAME=$DBNAME_DEFAULT
    set -e
fi
sed -i "s/DB_DATABASE=$DBNAME_DEFAULT/DB_DATABASE=$DBNAME/g " $EDIT_FILE_NAME

DBUSER_DEFAULT="root"
read -p "2. Enter database User: [$DBUSER_DEFAULT] " DBUSER
if [[ $DBUSER == "" ]]; then
    DBUSER=$DBUSER_DEFAULT
    set -e
fi
sed -i "s/DB_USERNAME=$DBUSER_DEFAULT/DB_USERNAME=$DBUSER/g " $EDIT_FILE_NAME

DBPASS_DEFAULT=""
read -p "3. Enter database Password: [$DBPASS_DEFAULT] " DBPASS
if [[ $DBPASS == "" ]]; then
    DBPASS=$DBPASS_DEFAULT
    set -e
fi
sed -i "s/DB_PASSWORD=$DBPASS_DEFAULT/DB_PASSWORD=$DBPASS/g " $EDIT_FILE_NAME

php artisan migrate
npm install

#https://youtu.be/WLY196kq07U?t=165
php artisan make:model Post --all --api

EDIT_FILE_NAME="database/migrations/*create_posts_table.php"

colName=("title" "slug" "body" "user_id")
colType=("string" "string" "text" "foreignId")
colProp=("" "unique()" "" "constrained()")

for i in ${!colName[@]}; do
    #sed -i '/$table->id();/a \\t\t\t$table->string("title");' database/migrations/*create_posts_table.php
    #fi ($colName[$i])
    #echo '$table->'${colType[$i]}'("'${colName[$i]}'");'
    PROP=""
    if [[ ${colProp[$i]} != "" ]]
    then

        PROP='->'${colProp[$i]}
       # echo $PROP
    fi
    # SED COMMAND TO UPDATE FILE AND ADD DATABASE COLUMNS
    sed -i '/$table->id();/a \\t\t\t$table->'${colType[$i]}'("'${colName[$i]}'")'$PROP';' $EDIT_FILE_NAME
done

# FACTORIES
EDIT_FILE_NAME="database/factories/PostFactory.php"
useArr=('use Illuminate\\Support\\Str;' 'use Illuminate\\Foundation\\Auth\\User;')
for i in ${!useArr[@]}; do
    sed -i "/Factory;/a ${useArr[$i]}" $EDIT_FILE_NAME
done
fakerArr=('$slug = Str::slug($title, "-")' '$body = $this->faker->paragraph()' '$user_id = User::all()->random()' '$title=$this->faker->sentence()')
for i in ${!fakerArr[@]}; do
    sed -i "/    {/a \\\\t\\t${fakerArr[$i]};" $EDIT_FILE_NAME
done

for i in ${!colName[@]}; do
    sed -i "/        return /a  \\\\t\\t\\t'${colName[$i]}' => \$${colName[$i]} ," $EDIT_FILE_NAME
done

# SEEDERS
php artisan make:seed userSeeder
EDIT_FILE_NAME="database/seeders/UserSeeder.php"

sed -i "/use Illuminate\\\\Database\\\\Seeder;/a  use Illuminate\\\\Foundation\\\\Auth\\\\User;" $EDIT_FILE_NAME

for i in ${!colName[@]}; do
    userCode="User::factory()->create(['name'=>'user_$i','email'=>'user_$i@example.com','password'=>bcrypt('password'),]);"
    sed -i "/    {/a  \\\\t\\t\\t$userCode" $EDIT_FILE_NAME
done

EDIT_FILE_NAME="database/seeders/PostSeeder.php"
sed -i "/use Illuminate\\\\Database\\\\Seeder;/a  use App\\\\Models\\\\Post;" $EDIT_FILE_NAME

sed -i "/    {/a \\\\t\\t\\tPost::factory(10)->create(); " $EDIT_FILE_NAME

EDIT_FILE_NAME="database/seeders/DatabaseSeeder.php"

sed -i "/    {/a \\\\t\\t\$this->call([UserSeeder::class,PostSeeder::class])" $EDIT_FILE_NAME

#https://youtu.be/WLY196kq07U?t=392
php artisan make:seed UserSeeder

EDIT_FILE_NAME="app/Models/User.php"
sed -i "/use TwoFactorAuthenticatable;/a \\\\tpublic function posts(){return \$this->hasMany(Post::class);}" $EDIT_FILE_NAME

EDIT_FILE_NAME="app/Models/Post.php"

sed -i "/use HasFactory;/a \\\\tprotected \$fillable = ['title','slug','body']; \\n\\tpublic function user(){return \$this->belongsTo(User::class);}" $EDIT_FILE_NAME

# https://youtu.be/WLY196kq07U?t=473
php artisan make:resource PostResource

EDIT_FILE_NAME="app/Http/Resources/PostResource.php"
returnVal="return['id'=>\$this->\$id, 'title'=>\$this->\$title, 'slug'=>\$this->\$slug, 'body'=>\$this->\$body, 'author'=>\$this->\$user->name,]"
# FIND AND REPLACE IN FILE WITH SED BASH
sed -i "s/return parent::toArray(\$request);/$returnVal/g " $EDIT_FILE_NAME


#PostController

EDIT_FILE_NAME="app/Http/Controllers/PostController.php"

sed -i '/use App\\Http\\Requests\\UpdatePostRequest;/a use App\\Http\\Resources\\PostResource;' $EDIT_FILE_NAME

newCode="public function __construct(){\n\t\t\$this->authorizedResource(Post::class, 'post');\n\t}"
sed -i "/public function index()/i $newCode" $EDIT_FILE_NAME

newCode='return PostResource::collection(auth()->user()->posts);'
sed -i "s/public function index()/public function index()\\n\\t{\\n\\t$newCode\\n\\n}\\n\\tfunction index_old()/" $EDIT_FILE_NAME


newCode='\$post = auth()->user()->\posts->create($request->all()); return new PostResource(\$post);'
sed -i "s/public function store(StorePostRequest \$request)/public function store(StorePostRequest \$request)\\n\\t{\\n\\t$newCode\\n\\n}\\n\\tfunction store_old()/" $EDIT_FILE_NAME

newCode='return new PostResource(\$post);'
sed -i "s/public function show(Post \$post)/public function show(Post \$post)\\n\\t{\\n\\t$newCode\\n\\n}\\n\\tfunction show_old()/" $EDIT_FILE_NAME

# UPDATE
newCode='\$post->update(\$request->all()); return new PostResource(\$post);'
Find='public function update(UpdatePostRequest \$request, Post \$post)'
Old='function update_old()'
sed -i "s/$Find/$Find\\n\\t{\\n\\t$newCode\\n\\n}\\n\\t$Old/" $EDIT_FILE_NAME

# DELETE public function destroy(Post $post)
newCode='return \$post->delete();'
Find='public function destroy(Post \$post)'
Old='function destroy_old()'
sed -i "s/$Find/$Find\\n\\t{\\n\\t$newCode\\n\\n}\\n\\t$Old/" $EDIT_FILE_NAME

#UpdatePostRequest
EDIT_FILE_NAME="app/Http/Requests/UpdatePostRequest.php"

sed -i '/use Illuminate\\Foundation\\Http\\FormRequest/a use Illuminate\\Validation\\Rule;' $EDIT_FILE_NAME

sed -i "s/return false;/return true;/g " $EDIT_FILE_NAME
newCode="'title'=> 'string|max:255', 'slug'=> ['string','max:255', Rule::unique('posts')->ignore(\$this->post)], 'body'=> 'string' "
sed -i "/return \[/a $newCode" $EDIT_FILE_NAME


#StorePostRequest
EDIT_FILE_NAME="app/Http/Requests/StorePostRequest.php"
sed -i "s/return false;/return true;/g " $EDIT_FILE_NAME
newCode="'title'=> 'required|string|max:255', 'slug'=> 'required|string|max:255|unique:posts', 'body'=> 'required|string' "
sed -i "/return \[/a $newCode" $EDIT_FILE_NAME

# POLICIES

EDIT_FILE_NAME="app/Policies/PostPolicy.php"

newCode="return \$user->tokenCan('post:read');"
Find='public function viewAny(User $user): bool'
Old='public function viewAny_OLD(User $user): bool'
sed -i "s/$Find/$Find\\n\\t{\\n\\t$newCode\\n\\n}\\n\\t$Old/" $EDIT_FILE_NAME

php artisan serve