Database – recording from Laravel
Laravel uses the ORM Eloquent.
Intro
- Create the class diagram of the following
Part 1 – creation of the base
Deliverable : otelo/mission2/…
- Create the following database
- In the .env file, modify the settings to connect to your database
DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=otelo DB_USERNAME=root DB_PASSWORD=
- observe in database/migrations, the php create user file
- executer the following command to launch the migrations files
php artisan migrate
Part 2 – link the table chambre with Laravel
Livrable : otelo/mission2/…
- define the controller for the rooms via the following command:
php artisan make:controller -r ChambreController
- create the Chambre model
php artisan make:model Chambre
- add the route
Route::get('/chambre',[ChambreController::class, 'store'] );
- in the Chambre class, add
Class Chambre extends Model { protected $table = "chambre"; protected $fillable = [ 'nbCouchage' ,'porte','etage','idCategorie','baignoire','prixBase' ]; }
- in the store function of ChambreController
public function store(Request $request) { $chambre= Chambre::create([ 'nbCouchage' =>2, 'porte' => 'B', 'etage' => 10, 'idCategorie' => 1, 'baignoire' => 0, 'prixBase' => 50 ]); //$chambre = new Chambre; $chambre->porte='C'; //$chambre->etage=$request->etage; $chambre->save(); }
- add the use App\Models\Chambre in the controller
- refresh the cache Laravel
php artisonfig:clear
- name the uri /chambre
- add the missing columns to your table to allow the addition of the data.
Part 3 – cration of the booking table via Laravel
Deliverable : otelo/mission2/…
- create your own migration for the booking table
php artisan make migration create_reservation_table
- in the migration file
public function up() { Schema::create('test', function (Blueprint $table) { $table->id(); $table->date('dateD'); $table->date('dateF'); $table->integer('idPeriode'); $table->timestamps(); }); }
- execute the migration
php artisan migrate
In case of error on the chains sizes, in AppServiceProvider.php
use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); }
Part 4- creation of chambres from Laravel
Deliverable : otelo/mission2/…
- create a seeder
php artisan make:seed ChambreSeeder
- in the run function
$chambre= Chambre::create([ 'nbCouchage' =>2, 'porte' => 'B', 'etage' => 10, 'idCategorie' => 1, 'baignoire' => 0, 'prixBase' => 50 ]); //affichage en console dd($chambre);
- in the run function of database/seeds/DatabaseSeeder.php
$this->call(ChambreSeeder::class);
- test with the command and observe if a data is created
php artisan db:seed
- enable to generate 30 rooms randomly, all the fields champs will be ramdom with php code.
We will seelater how to retrieve data from the base.
Notes
- error on chains size when importing, in AppServiceProvider.php
public function boot() { Schema::defaultStringLength(171); }
- cancel migration
php artisan migrate:rollback
- cancel all migrations
php artisan migrate:reset
- cancel all migrations and re-execute
php artisan migrate:refresh
- create a model with migration
php artisan make:model Chambre -m php artisan migrate