Booking a room
Intro
- Create the deployment diagram of the application
Part 1 – Availability
Deliverable gitlab : otelo/mission6/…
- check that the category foreign key of the room table is called category_id
- display the rooms of a category
class Chambre extends Model{ public function categorie(){ return $this->belongsTo('App\Categorie'); } } class Categorie extends Model{ public function chambres(){ return $this->hasMany('App\Chambre'); } dans controleur $categorie = Categorie::where('id',3)->->firstOrFail(); //obtention des chambres $chambres=$categorie ->chambres(); //renvoie vers vue puis foreach ou dd($categorie ->chambres()) //obtention de la categorie d'une chambre //$chambre = Chambre::where('id',1)->firstOrFail(); //$cat=$chambre->categorie;
- Create the URI GET availabilities which sends back the list of available rooms according to the start date, the end date and the category in JSON.
public function dispo() { $chambres = DB::select('select chambre.id, nbCouchage, porte, etage,libelle ,baignoire from chambre inner join categories on chambre.categorie_id = categories.id where chambre.categorie_id=? and chambre.id not in (select reservation.idChambre from reservation where dateD<? or dateF>?)', [3,'2021-03-03', '2021-03-01'] ); // return view('chambres',['chambres' => $chambres]); return response()->json($chambres); }
- Each line must show the basic price per day and the description of the room. See note to get all the rooms of a category
Part 2 – research
Deliverable gitlab : otelo/mission6/…
- Create the form to search for a room by start date, end date and room category.
- Display the answer. Each line should show the basic price per day and the description of the room.
Part 3 – booking
Deliverable gitlab : otelo/mission6/…
- enable the booking of a room and register it in the database
@create (creation form)
@[email protected] $chambres = chambres (); return view('reservation.create', ['chambres ' => $chambres ]); Route::get('reservation/new',[ReservationController::class, 'create'] )->name('reservation.create'); @reservation/create.blade.php @foreach($chambres as $chambre)
@store (reception of the form create)
Route::post('reservation/store', [ReservationController::class, 'store'])->name('reservation.store');
@[email protected] $reservation=new Reservation(); $reservation->description= $request->input('description'); $reservation->save(); return redirect()->route('zaza')->with('success','réservation enregistrée');;
@chambre/index.blade.php {{$reservation->title}}
Note :
@index
@[email protected] $chambres= Chambre::findAll(); return view('chambre.index', ['chambres' => $chambres]); Route::get('/chambres', [ReservationController::class, 'index'])->name('chambre.index'); @chambre/index.blade.php @foreach($categories as $categorie)
@create
@[email protected] $categories = Categories:all(); return view('chambre.create', ['categories ' => $categories ]); Route::get('chambre/new', [ReservationController::class, 'create'])->name('chambre.create'); @chambre/create.blade.php @foreach($categories as $categorie)
@store and Image storage (create form reception)
form input for image storage, don’t forget enctype in form
@chambre/create.blade.php <form action="#" method="POST" enctype="multipart/form-data"> <input type="file" name="image"/> </form>
Route::post('chambre/store', [ReservationController::class, 'store'])->name('chambre.store');
Symbolic link between storage folder and public folder of the site so that the images are publicly accessible
php artisan storage:link php artisan cache:clear
@[email protected] $chambre=new Chambre(); $chambre->description= $request->input('description'); $image= $request->file("image"); $imageFullName = $image->getClientOriginalName(); //dd($imageFullName); $imageName= pathinfo($imageFullName, PATHINFO_FILENAME); //dd($imageName); $imageExtension= $image->getClientOriginalExtension(); //dd($imageExtension); $file = time() . '_' . $imageName . '.' . $imageExtension; $image->storeAs('public/chambres/',$file); $chambre->image = $file; $chambre->save(); return redirect()->route('zaza')->with('success','chambre enregitrée');
@chambre/index.blade.php <img class="mon-image" src="/storage/chambres/{{$chambre->image}}" alt="{{$chambre->title}}">
@edit
@chambre/index.blade.php <a href="{{route('chambre.edit', $chambre->id)}}"> @[email protected] $chambre= Chambre::find($id); return view('chambre.edit', ['chambre' => $chambre]); Route::get('chambre/{id}/edit', [ReservationController::class, 'edit'])->name('chambre.edit'); @chambre/edit.blade.php {{$chambre->title}}
@update and Image storage (edit form reception)
Route::put('chambre/{id}/update', [ReservationController::class, 'update'])->name('chambre.update'); @chambre/edit.blade.php <form action="{{route('chambre.update', $chambre->id)}} method="POST" @csrf @method("PUT") @[email protected] $chambre= Chambre::find($id); $chambre->title()=$request->input('title'); //... if($request->file('image'){ $image= $request->file("image"); $imageFullName = $image->getClientOriginalName(); $imageName= pathinfo($imageFullName, PATHINFO_FILENAME); $file = time() . '_' . $imageName . '.' . $imageExtension; $image- >storeAs('public/chambres/',$file); $chambre->image = $file; } $chambre->save(); return redirect()->route('chambre.index');
@destroy
Route::delete('chambre/{id}/destroy', [ReservationController::class, 'destroy'])->name('chambre.destroy'); @lien de suppression <a href="{{route('chambre.destroy', $chambre->id)}}"></a> @[email protected] $chambre= Chambre::find($id); $chambre->delete(); return redirect()->route('chambre.index')->with('success','chambre supprimée');
Environment variable
env('DB_DATABASE')