Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tzak902's avatar

How to fetch data for a specific column if there is a match based on Date

Hello guys, Sorry to bother you, I have this situation: I have a table named: create_vouchers, inside of it there is a column named: arrivaldate I'm trying to fetch all these arrival dates if the current date matches the arrival dates from the database. Example: if there is a record with arrival date 19-08-2022 and today is 19-08-2022 then it should be listed. here is my code:

class ArrivalsDeparturesController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('arrivals_departure_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
        $CurrentDate = date('m-d-Y');
        $Arrivals = DB::table('create_vouchers')
            ->select('id', 'client_name', 'night', 'hotel_name_id')
            ->where('arrivaldate', '=', $CurrentDate)
            ->first();
        //$Arrivals = AppCreateVoucher::All();
        return view('admin.arrivalsDepartures.index', compact('Arrivals'));
    }
}

My index.blade.php View

@extends('layouts.admin')
@section('content')
<div class="content">

    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    {{ trans('cruds.arrivalsDeparture.title') }}
                </div>
                <div class="panel-body">
                    <p>
                        @php
                        echo $Arrivals;
                        @endphp

                    </p>
                </div>
            </div>



        </div>
    </div>
</div>
@endsection

Nothing is see from the view.

0 likes
13 replies
tykus's avatar

Your $CurrentDate will be 08-19-2022, not 19-08-2022.

What is the type of the arrivaldate column in the database? And why are you fetching only one record?

$Arrivals = DB::table('create_vouchers')
            ->select('arrivaldate')
            ->whereDate('arrivaldate', now())
            ->get();
tzak902's avatar

@tykus Great, it does show the result as [{"arrivaldate":"2022-08-19"}] but how to display other data like voucher id, client name, night and hotel name

tykus's avatar

@tzak902 well, I was copying your original query where you selected only the arrivaldate! You can either (i) omit the select method (fetching all columns) or (ii) select specific columns, e.g.

$Arrivals = DB::table('create_vouchers')
            ->select('id', 'client name', 'night', 'hotel name', 'arrivaldate')
            ->whereDate('arrivaldate', now())
            ->get();
tzak902's avatar

@tykus I have a small issue again, I can retrieve the hotel name id since, hotel_name exist only within hotels model and hotels table, how can I call that model based on the hotel_name_id ?

RayC's avatar

@tzak902 Do you have a relationship setup between the models? If so you can use with() or call the related method:

$Arrivals->hotel->hotel_name;
tykus's avatar

@rayc the OP is using the Query Builder currently, so relationships are meaningless.

A JOIN will be enough in this case (ensure the column names are correct in the query below:

$Arrivals = DB::table('create_vouchers')
            ->select('id', 'client name', 'night', 'hotels.hotel_name', 'arrivaldate')
            ->join('hotels', 'hotels.id', 'create_vouchers.hotel_name_id')
            ->whereDate('arrivaldate', now())
            ->get();

@tzak902 I would suggest you take some time to learn the basics of Laravel development -Laravel From Scratch

tzak902's avatar

@tykus Thank you, I got this error:

Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select id, client_name, night, hotels.hotel_name, arrivaldate from create_vouchers inner join hotels on hotels.id = create_vouchers.hotel_name_id where date(arrivaldate) = 2022-08-20)

  $Arrivals = DB::table('create_vouchers')
            ->select('id', 'client_name', 'night', 'hotels.hotel_name', 'arrivaldate')
            ->join('hotels', 'hotels.id', 'create_vouchers.hotel_name_id')
            ->whereDate('arrivaldate', now())
            ->get();
tykus's avatar

@tzak902 use the table alias to fully qualify the selected column names:

$Arrivals = DB::table('create_vouchers')
            ->select('create_vouchers.id', 'client name', 'night', 'hotels.hotel_name', 'arrivaldate')
            ->join('hotels', 'hotels.id', 'create_vouchers.hotel_name_id')
            ->whereDate('arrivaldate', now())
            ->get();
1 like
RayC's avatar

@tykus I noticed that, with the correct relationship couldn't he write it like:

$Arrivals = CreateVoucher::whereDate('arrivaldate', now())
    ->with('hotel')
    ->get();
tzak902's avatar

Do you have an idea why this doesn't add voucher id next to the url:

<th><a href="create-vouchers/">{{$Arrival->id}}</a></th>

it will only display link like: localhost/create-vouchers/ without voucher id at the end after the slash

tykus's avatar

@tzak902 seriously, please go through the basics...

<th>
    <a href="create-vouchers/{{$Arrival->id}}">{{$Arrival->id}}</a>
</th>

And mark the thread solved if you're all set

1 like

Please or to participate in this conversation.