Database – retrieval to Laravel

 

laravel

controller

Intro

Part 1 – Display from a template

Deliverable : otelo/mission5/…

  • in the room controller, index function, send back the list of rooms
$chambres = Chambre::all();
return view('chambres',['chambres' => $chambres]);

 

  • in the Chambres view, display the table
@extends('layouts.app')

@section('content')
<table class="table table-hover table-sm">
    <thead class="thead-dark">
        <tr>
            <th> id</th>
            <th> nbCouchage</th>
            <th> porte  </th>
            <th> etage </th>
            <th> idCategorie</th>
            <th> baignoire </th>
        </tr>
    </thead>
    <tbody>
   

         @foreach($chambres as $chambre)
          <tr>
          <td> {{$chambre->id}} </td>
              <td> {{$chambre->nbCouchage}} </td>
              <td> {{$chambre->porte}} </td>
              <td> {{$chambre->etage}} </td>
              <td> {{$chambre->idCategorie}} </td>
              <td> {{$chambre->baignoire}} </td>
             
          </tr>
         @endforeach
   </tbody>
</table>
@stop

 

  • Create the uri chambres
Route::get('/chambres',[ChambreController::class, 'index'] );

 

 

 

  • Display the list of rooms with the category label. To do this you can retrieve a category array and replace the room category id in the loop.
    You will perform a function of your choice in the controller, the view and the necessary route.

 

Part 2 – running an SQL query

  • Create an SQL query that displays the premium rooms that have a bathtub and a price below 100
  • In the ChambreController, create a function that returns the table
use Illuminate\Support\Facades\DB;

 

class UserController extends Controller
{
    /**
     * Show a list of all of the application's users.
     *
     * @return Response
     */
    public function index()
    {
        $users = DB::select('select * from users where active = ?', [1]);

        return view('user.index', ['users' => $users]);
    }
}
  • create a view that displays the table
  • Add the route and test

 

Notes

  • Refresh model data from the database, fresh and refresh
$flight = App\Flight::where('number', 'FR 900')->first();

$flight->number = 'FR 456';
$freshFlight = $flight->fresh();
$flight->number; // "FR 456"
$flight = App\Flight::where('number', 'FR 900')->first();

$flight->number = 'FR 456';
$flight->refresh();
$flight->number; // "FR 900"

 

  • Display the id of a random category
app/category::all()->random(1)->first()->id;