Creating a Laravel 10 project from scratch

touch database/database.sqlite

Assumption



Create a directory for your project

mkdir laravel_10
cd laravel_10
composer create-project  --prefer-dist  laravel/laravel . -vvv

npm install

this will create a new directory laravel_10/node_modules/ with all the necessary packages like vite

 

run the following command

php artisan serve

visit http://127.0.0.1:8000/ in your browser

The browser is displaying your local version of your project. The rendered page can be found in welcome.php

laravel_10/resources/views/welcome.blade.php

You will most likely want to edit this page and also put in a common templating system and use custom CSS and Javascript. Bootstrap 5 is a good choice.

 

Although this is a local dev environment, with Laravel and Vite -- the default--- you need to run 

npm run dev or npm run build

to avoid this error: Vite manifest not found at: ../public/build/manifest.json

npm run dev will create a temporary manifest.json as long as the Vite dev server is up and allow for hot reloading

npm run build -- although generally used for production will remove the error

Use Bootstrap 5 from CDN

Include Bootstrap’s CSS and JS. Place the <link> tag in the <head> for our CSS, and the <script> tag for our JavaScript bundle (including Popper for positioning dropdowns, poppers, and tooltips) before the closing 


You will need to connect to a database next. It is common to use mysql with PHP applications but there are plenty of others, So far we have been building a local environment and you could set up a local database. I chose to create a database on the same remote server as production to ensure I have the same version.

Replace

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

with  

DB_CONNECTION=mysql
DB_HOST=db-mysql-your-account-your-provider.com
DB_PORT=your_port
DB_DATABASE=your_database_name_local
DB_USERNAME=your_username
DB_PASSWORD=your_password

 

Run the following command to run the default Laravel migrations

$ php artisan migrate

   INFO  Preparing database.

  Creating migration table ................................................................................. 85ms DONE

   INFO  Running migrations.

  2014_10_12_000000_create_users_table .................................................................... 195ms DONE
  2014_10_12_100000_create_password_reset_tokens_table .................................................... 133ms DONE
  2019_08_19_000000_create_failed_jobs_table .............................................................. 136ms DONE
  2019_12_14_000001_create_personal_access_tokens_table ................................................... 201ms DONE

Create Login and Registration

 composer require laravel/ui
 php artisan ui bootstrap --auth
 npm install && npm run build

If you are using Laravel say vs Wordpress or Drupal there is a good chance you want user to be able to login and register.



You can also use Laravel Fortify but the you need to create all of your views 

 

 

Make our models and migration

 

$ php artisan make:model Link --migration

   INFO  Model [app/Models/link.php] created successfully.

   INFO  Migration [database/migrations/2023_03_05_223113_create_links_table.php] created successfully.

 We are asking Laravel to make a new eloquent model called Link, and the '--migration' flag tells the command to generate a migration. 

This will have created a new migration for you inside database/migrations as well as s file for the model located at app/Models/Link.php

update the up() method in database/migrations/2023_03_05_223113_create_links_table.php to the following:

public function up(): void
    {
        Schema::create('links', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('url');
            $table->text('description')->nullable();
            $table->foreignId('user_id'->constrained(->index()->cascadeOnDelete();
        });
    }
$ php artisan make:controller LinkController --resource

   INFO  Controller [app/Http/Controllers/LinkController.php] created successfully.

This will create our resource controller with all the methods we need.

 

Actions Handled By Resource Controller

Verb URI Action Route Name
GET /links index links.index
GET /links/create create links.create
POST /links store links.store
GET /links/{link} show linksshow
GET /links/{link}/edit edit linksedit
PUT/PATCH /links/{link} update links.update
DELETE /links/{link} destroy links.destroy

 

add the following to routes/web.php

use App\Http\Controllers\LinkController;

Route::resource('links', LinkController::class);

this will create all the routes you need for the actions above.

run php artisan route:list

GET|HEAD        links ................................................................................................................................................................................................................................................................ links.index › LinkController@index
  POST            links ................................................................................................................................................................................................................................................................ links.store › LinkController@store
  GET|HEAD        links/create ....................................................................................................................................................................................................................................................... links.create › LinkController@create
  GET|HEAD        links/{link} ........................................................................................................................................................................................................................................................... links.show › LinkController@show
  PUT|PATCH       links/{link} ....................................................................................................................................................................................................................................................... links.update › LinkController@update
  DELETE          links/{link} ..................................................................................................................................................................................................................................................... links.destroy › LinkController@destroy
  GET|HEAD        links/{link}/edit ...................................................................................................................................................................................................................................................... links.edit › LinkController@edit

Each GET request will require a view.