Database – retrieval to Laravel





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

<table class="table table-hover table-sm">
    <thead class="thead-dark">
            <th> id</th>
            <th> nbCouchage</th>
            <th> porte  </th>
            <th> etage </th>
            <th> idCategorie</th>
            <th> baignoire </th>

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


  • 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



  • 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->number; // "FR 900"


  • Display the id of a random category