---
# System prepended metadata

title: Einstieg Laravel
tags: [Laravel]

---

# Einstieg Laravel

## Autoren

- OK, RM, JK

TODO Lizenz CC0

- [x] JK
- [ ] OK
- [x] RM

Dieser Überblick soll den Einstieg in Laravel-Projekte vereinfachen.
Verbesserungen sind gern gesehen und können hinzugefügt werden.

Vorschläge? / TODOs:

- [ ] Einstig in deutsch und englisch?
- [ ] Struktur prüfen und überarbeiten/kommentieren

## Start mit Laravel 11

:::info
Prerequirement:

- php
- composer
- node.js

Installed and useable in your IDE
:::

Setup new laravel example-app:

    composer create-project laravel/laravel example-app

Open a terminal in your IDE
Start **build-in server**:

    php artisan serve

To install all necessary npm packages,
open new terminal in IDE and use:

    npm install

When everything is installed
start the **vite server** with:

    npm run dev

The **vite server** serve direct assets changes like .css and .js

Some usefull comand:

    php artisan list
    php artisan --help

Try it out to use the --help switch with other commands like:

    php artisan serve --help

Navigate to the welcome view in your project structure.
By default can be find:

    resources/view/welcome.blade.php

Make a backup of this file, so it can be easily restored later.
Delete everything inside, and provide simple html
h1 with a "Welcome" inside.

Inspect it in the browser with the F12 dev tools.
It is not best practice to render the view like that.
The controller should handle what to do and
for that there should be a route.

Open a third terminal and
create the WelcomeController:

    php artisan make:controller WelcomeController

An empty controller is created in app/http/controllers.
Create a public function with name "welcome" and a return value,
which is the view.

    public function welcome() {
        return view('welcome');
    }
:::info
*The file extension ".blade.php" is not required.*
:::

Change directory to routes/web.php.
Provide a route that use the WelcomeController instead
of the function that returns the view and a action
(method created in controller) and a name.
Looks like:

    Route::get('/', [WelcomeController::class, 'welcome'])->name('welcome');

:::info
*Provide "use" dierective to WelcomeController.*
:::

Generate models and migration:

    php artisan make:model Note -m

the switch -m creates a migration file for that model.
Two files are created:

- ..Models/Note.php and
- ..Database/migration/..notes_table.php

Open the migration file add two rows in the "up" function
to the table

```php
$table->longText('note');
$table->foreignId('user_id')->constrained('users');
```

Run the migration:

    php artisan migrate 

That create the note table like defined.
Use the command again and see what happen.
Conclusion?

To see the created tables:

    php artisan model:show Note

Generate factory create seed data:

    php artisan make:factory NoteFactory --model=Note

The switch tells laravel that the note model is the model for the factory
Open noteFactory add:

```php
'note'    =>fake()->realText(2000),
'user_id' => 1
```

This generate a fake-text that is a real-text
lenght 2000 for user with id=1.

Nav to database/seeders/DatabaseSeeder.php
and open the file
then add:

```php
'id'         => 1,
'password'   => bcrypt('pass1234')
```

Create with this factory 100 notes for user with id=1:

```php
Note::factory(100)->create();
```

:::info
*Make sure this line is inside the run function.
And dont forget the "use" directive App\Models\Note*
:::

Will do a rollback and seed data into database make sure to use the right command here if you have already data in db

    php artisan migrate:refresh --seed

> DO NOT HARDCODE USER_IDs OR PASSWORDS
> THIS IS AN EXAMPLE TO HAVE A TEST USER!

Create views:
Create folder Note inside resources/views with artisan:

    php artisan make:view note.index

The note dot index say laravel to create a folder name note
and a index.blade.php inside.
Go on with edit, create and show there are 4 main views now.
Inside each view create a div element
and a h1 with the name of each action.
To use the data first create a controller for that resource.
Here for model Note:

    php artisan make:controller NoteController --resource --model=Note

Nav to ./NoteController.php
This file brings 7 empty methods well commented.
Where to place the logic

- index
- create
- store
- show
- edit
- update
- destroy

For now return inside index, create, show and edit method
the name of the method as string.
Hop into web.php to define the CRUDS for all 7 actions

:::info
*Dont forget to add the NoteController as use directive into web.php*
:::

Inspect the routes been created:

    in browser and running server input
    localhost:8000/note        should show the name of index method
    localhost:8000/note/1      should display the name of the show method
    same with edit and create

In the end there should be 7 routes in web.php by now.

:::success
There is a shorthand which need only one line of code for all 7 routes. (:
:::

Change inside the NoteController to return the view:

    return view('note.index');   

e.g.
the result should be the same as inspected before
decide witch layout you need or want to use
x-layout or x-app-layout and implement it to the views.

The views need a form with action to the specific routes,
the specific method that will be executed,
and some area to show the generated text.

Have fun.

## Starter-Kit Modul integration into running Project

breeze installation

First "web.app" & "app.css" create a backup of this two files.

Open a Terminal:

    composer require laravel/breeze --dev

if succsesfully, then:

    php artisan breeze/install

This require some input from user:

- choose "blade" the breeze stack to be installed
- darkmode support? y/n
- testing framework? pest/phpunit

After the installation all routes and the css is replaced with default.
With the backup files created a restoration go fast.

To be able to use the E-Mail verification,
uncomment inside user.php the "MustVerifyEmail" part.
Dont forget to implement the "MustVerifyEmail" to the user class.

Default settings the E-Mail shipment is saved in

    storage/logs/laravel.logs

Now it is possible to route throw middleware to authentificate an verifiy users.

:::info
Dont forget to set the csrf (cross.site-request-forgery) dierectives.
:::
