So what is the problem? Wrong rooms? Wrong dates? Something else?
Nov 19, 2019
28
Level 1
How can i show only free rooms in Laravel
I am new in Laravel and doing hotel booking application,in position find room i try to checked dates from booking table(time_from and time_to) to show only free rooms.I think i have some mistakes in my Controller.I tried write whereRaw but it doesn't work too.Help me please
Its my FindRoomsController
<?php
namespace App\Http\Controllers\Admin;
use App\Room;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use Validator;
class FindRoomsController extends Controller
{
public function index(Request $request)
{
if (!Gate::allows('find_room_access')) {
return abort(401);
}
$time_from = $request->input('time_from');
$time_to = $request->input('time_to');
if ($request->isMethod('POST')) {
$rooms = Room::with('booking')->whereHas('booking', function ($q) use ($time_from, $time_to) {
$q->where(function ($q2) use ($time_from, $time_to) {
$q2->where('time_from', '>=', $time_to)
->orWhere('time_to', '<=', $time_from);
});
})->orWhereDoesntHave('booking')->get();
} else {
$rooms = null;
}
return view('admin.find_rooms.index', compact('rooms', 'time_from', 'time_to'));
}
}
My find room blade
@if (isset($rooms) && is_null($rooms))
<div class="form-group" style="text-align: center">
<label>@lang('quickadmin.find-room.no_rooms_found')</label>
</div>
@endif
@foreach ($rooms as $room)
<tr data-entry-id="{{ $room->id }}">
<td></td>
<td field-key='room_number'>{{ $room->room_number }}</td>
<td field-key='floor'>{{ $room->floor }}</td>
<td field-key='description'>{!! $room->description !!}</td>
<td field-key='category_id'>{{ $room->category->name or ''}}</td>
<td field-key='price'>{!! $room->price !!}</td>
<td>
@can('booking_create')
<button class="btn btn-danger">
<a style="color: #ffffff;" href="{{ route('admin.bookings.create',
['room_id' => $room->id,'time_from' => $time_from, 'time_to' => $time_to]) }}">
{!!trans('quickadmin.find-room.book_room')!!}</a>
</button>
@endcan
</td>
</tr>
@endforeach
Booking table in database
if(! Schema::hasTable('bookings')) {
Schema::create('bookings', function (Blueprint $table) {
$table->increments('id');
$table->datetime('time_from')->nullable();
$table->datetime('time_to')->nullable();
$table->integer('diff_days')->nullable();
$table->text('additional_information')->nullable();
$table->string('first_name');
$table->string('last_name');
$table->string('address')->nullable();
$table->string('phone')->nullable();
$table->string('email');
$table->integer('all_price')->nullable();
$table->timestamps();
$table->softDeletes();
$table->index(['deleted_at']);
});
}
Schema::table('bookings', function(Blueprint $table) {
if (!Schema::hasColumn('bookings', 'room_id')) {
$table->integer('room_id')->unsigned()->nullable();
$table->foreign('room_id', '110461_5a676fa239ffd')->references('id')->on('rooms')->onDelete('cascade');
}
});
Room table in database
public function up()
{
if(! Schema::hasTable('rooms')) {
Schema::create('rooms', function (Blueprint $table) {
$table->increments('id');
$table->string('room_number');
$table->integer('floor')->nullable();
$table->text('description')->nullable();
$table->integer('price');
$table->timestamps();
$table->softDeletes();
$table->index(['deleted_at']);
});
}
}
Level 8
Okay, haven't given this much testing. But I think it may work. Though, needs lots of testing:
$rooms = App\Room::whereNotIn('id', function($query) use ($time_from, $time_to) {
$query->from('bookings')
->select('room_id')
->where('time_from', '<=', $time_to)
->where('time_to', '>=', $time_from);
})->get();
If you get an error try replacing bookings with just booking:
$query->from('booking')
Good luck
Please or to participate in this conversation.