Laravel Tutorial Step By Step With Organize Way

Tools Needed :

  • Xampp Download Link
  • Composer Download Link
  • npm
  • Any Code Editor I Am Using Visual Studio Download Link

Installing Laravel :

Go to Any Directory In Your Pc And Create A Folder With Your Project Name Open Folder In Visual Studio And Run Below Code In Visual Studio Code Terminal .

composer create-project laravel/laravel Example

Php Artisan Command Laravel

Name Of Command	                                        : Work Of Command
php artisan make:controller UserController              : Make A Controller File In App\Http\Controllers/UserController.php
// Resource Controller
php artisan make:controller UserController –resource	: Make Controller With Create,Update,delete And Edit Methde

php artisan make:model Photo	                        : Make A Model File In App/Http/Model/Photo.php
php artisan make:migration photo	                    : make A Migration File In database/migration/migration file
php artisan make:seeder Bookseeder	                    : Make A Seeder File Database/Seeders Folder
php artisan make:middleware checkage	                : make A Middlewire File In App/Http/Middleware Folder
php artisan optimize	                                : Clear cache And Optimize Laravel File

Laravel Ui :Install Bootstrap

Let’s run bellow command to install laravel ui package by bellow command:

composer require laravel/ui

Next, you have to install the laravel UI package command for creating auth scaffolding using bootstrap 5. so let’s run the bellow command:

php artisan ui bootstrap

Now let’s run bellow command for install npm:

npm install && npm run dev

Laravel ui bootstrap Linking With View Using Vite

Laravel 9 use vite For Linking Bootstrap File Link Any View With Below Code

    @vite(['resources/sass/app.scss', 'resources/js/app.js'])

Connecting your Laravel project to MySQL database

Step1: Create a new database via phpmyadmin

Navigate to domain name.dev/ PHPMyAdmin, Click on the Databases tab and create a new database with your wanted name. Clicking on create will create a new database in your XAMPP MySQL.

Step2: Changes in.env configuration file

Once the database is created, you need to tell your laravel project the details about the database. Open .env file And Change Below Property With Your database Information .

DB_CONNECTION=mysql
DB_HOST=190.0.0.1
DB_PORT=4008
DB_DATABASE=testProject
DB_USERNAME=root
DB_PASSWORD=

Laravel Migration :

Create table Using Migration Command:

Create A table names posts : Open terminal And Run Below Code

php artisan make:migration create_post_table

After run above command, you can see created new file In Database/Migrations/2022_09_15_111008_create_post_table.php .

Open The File You See Like This

<?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('post', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('post');
    }
};

you have to add new column for string, integer, timestamp and text data type as like bellow:

Add Column name title,body and is_publish

<?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('post', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('title');
            $table->text('body');
            $table->boolean('is_publish')->default(0);
            

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('post');
    }
};

What Is Migration Up And Down Function

Laravel Give Us Two Type Of Migration Process 1. Migrate And Role back . When Our Migration Turn into Next Then Work the Up Function . If We need To Go Preview Step Need Role back

    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

When We Run Migration migrate . Run up function If We See Above Code Inside Up Method Some Instruction When We Migrate Migration Command Create A Table Like Above Instruction .

  public function down()
    {
        Schema::drop('flights');
    }

When We Run Migration Role back . Run down function

Migrate Database

Run Migrate Command In Terminal Or Cmd

php artisan migrate
Laravel Migration Default Value For Column

After Creating migration File column Structure Like Below

$table->text('body')->default('NO BODY');
Laravel Migration Default Value Null:
$table->string('title')->nullable();
Laravel Migration Default Value Boolean:
$table->boolean('displayed')->default(0);
Laravel Migration Default Value Current Date:
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
Laravel Migration Column Length :
$table->string('title', 50);
Laravel Migration Drop Column From Database Table
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropColumn('body');
        });
    }

Laravel Database : Raw Query

For Best Support Follow Laravel Official Website link : Basic Database Usage

Laravel Raw Query : Insert Statement

Open Web.php : Include DB Class And Set Route

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
Route::get('/', function () {
     DB::insert('insert into table_post (title, body) values(?,?)',['Olee','Ahmmed  ashik']);
});
?>

Write Above Code In Web.php . After Run php artisan optimize Command Reload Route Page In browser one Record Will be Insert in database .

Laravel Raw Query : Select Statement

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
Route::get('/', function () {
    $data =  DB::select('select title,body from table_post where title = ? ',['olee']);
    return $data;
});
?>

Laravel Raw Query : Update Statement

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
Route::get('/', function () {
   $data = DB::update('update table_post set title = "good boy" where id = ?',[1]);
   return  $data ;
});
?>

Laravel Raw Query : Delete Statement

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
Route::get('/', function () {
   $data = DB::delete('delete from table_post where title = ?',['good boy']);
   return  $data ;
});

?>

Leave a comment

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