/laravel-thenetninja

Laravel 6 Tutorials by TheNetNinja with code adjustment tips for Laravel 9.

Primary LanguageJavaScript

Laravel Tutorials by TheNetNinja

This project is based from Laravel 6 Tutorial for Beginners by TheNetNinja, but I am providing some tips to those who want to go along with that tutorial using Laravel 9.

Lessons

These are the different lessons in that tutorial, which are linked to a specific commit in this repository. Adjustment tips are provided below a lesson that needs them.

1. Introduction


After installing Laravel, you need to replace the contents of resources/views/welcome.blade.php by the contents of this file.










You will encounter an error saying:

ERROR: Target class [PizzaController] does not exist.

To solve this, follow these steps:

  1. Import PizzaController by adding this line on top of routes/web.php.

    use App\Http\Controllers\PizzaController;
  2. Then use the new Laravel 9 syntax in registering actions in routes:

    REPLACE

    Route::get('/pizzas', 'PizzaController@index');
    Route::get('/pizzas/{id}', 'PizzaController@show');

    WITH

    Route::get('/pizzas', [PizzaController::class, 'index']);
    Route::get('/pizzas/{id}', [PizzaController::class, 'show']);


  • Do not worry about missing class names in the migration files. They are not required anymore since Laravel 9.


  • You can skip adding a value on the price column. Nevermind the error that will appear on phpMyAdmin.
  • Models will be created inside the app/Models folder instead.

Again, the new syntax these route actions are:

[PizzaController::class, 'index']
[PizzaController::class, 'show']
[PizzaController::class, 'create']



Again, the new syntax for registering this action in a route is:

Route::post('/pizzas', [PizzaController::class, 'store']);

If you get an error saying

SQLSTATE[HY000]: General error: 1364 Field 'price' doesn't have a default value

Just add a value for price during record creation:

...

$pizza->price = 0;
$pizza->save();


Again, the new syntax for registering this action in a route is:

Route::post('/pizzas/{id}', [PizzaController::class, 'destroy']);

Please follow these steps:

  1. Replace the contents of your package.json with the contents of this file.
  2. Replace the contents of your vite.config.js with the contents of this file.
  3. Create the sass folder inside resources.
  4. Create the main.scss file and copy the content of public/css/main.css to it.
  5. Replace the contents of resources/main.scss with the contents of this file.
  6. In your resources/views/layouts/layout.blade.php, replace
    <link rel="stylesheet" href="/css/main.css">
    with:
    @vite('resources/sass/main.scss')
  7. Run npm install command.
  8. Run npm run dev command on a different terminal session.

  1. In your resources/sass/app.scss, replace

    // Bootstrap
    @import '~bootstrap/scss/bootstrap';

    with:

    // Bootstrap
    @import './bootstrap/scss/bootstrap';
  2. In your resources/views/layouts/app.blade.php, replace

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>

    with:

    <!-- Scripts -->
    @vite('resources/js/app.js')

    also replace

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <link rel="stylesheet" href="/css/main.css">

    with:

    <!-- Styles -->
    @vite('resources/sass/app.scss')
    @vite('resources/sass/main.scss')
  3. Run npm run dev again.






29. File Generation Shortcut


30. Wrap Up & Next Steps