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

tzak9022's avatar

Can't pull data from DB

Hi guys, is it possible please to give this code a look, I'm trying to pull data from DB, but I got no data pulled

my controller:

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Gate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;

class VouchersAccountingController extends Controller
{
    public function index(Request $request)
    {
        abort_if(Gate::denies('vouchers_accounting_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
        $from = $request->from;
     $to = $request->to;

        $Reports = DB::table('create_vouchers')->whereNull('create_vouchers.deleted_at')
            ->select('create_vouchers.id', 'client_name', 'night', 'hotels.hotel_name', 'arrivaldate', 'departuredate', 'total_amount', 'number_of_room', 'payment_mode')
            ->join('hotels', 'hotels.id', 'create_vouchers.hotel_name_id')
            ->whereBetween('arrivaldate', [$from, $to])->get();

        return view('admin.vouchersAccountings.index', compact('Reports', 'from', 'to'));
    }

    public function search(Request $request)
    {

    
    }


my index.blade.php

@extends('layouts.admin')
@section('content')
<link href="{{ asset('css/arrivaldeparture.css') }}" rel="stylesheet" />
<div class="content">

    <div class="row">
        <div class="col-md-12 mt-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    {{ trans('cruds.vouchersAccounting.title') }}

                </div>

                <div class="panel-body">



                    <div class="card">
                        <div class="card-header">
                            <h4 class="Green">Reports</h4>

                            <form name="search" id="search" method="post" action="{{ route('admin.vouchers-accountings.search') }}">
                                @csrf
                                <div class="form-group">
                                    From
                                    <div class='input-group date'>
                                        <input id="date-from" name="from" type='text' class="form-control" value="{{ $from }}">
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    To
                                    <div class='input-group date' id='CalendarDateTime'>
                                        <input id="date-to" name="to" type='text' class="form-control" value="{{ $to }} ">
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                    </div>
                                </div>
                                <button type="submit" class="btn btn-primary">Search</button>
                            </form>
                            <div class="card-body">
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>Voucher #</th>
                                            <th>Client</th>
                                            <th>Hotel</th>
                                            <th>Arrival</th>
                                            <th>Departure</th>
                                            <th>Nights</th>
                                            <th>Rooms</th>
                                            <th>Payment Mode</th>
                                            <th>Paid Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($Reports as $Report)
                                        <tr>
                                            <th> {{$Report->id}} </th>
                                            <th> {{$Report->client_name}} </th>
                                            <th> {{$Report->hotel_name}} </th>
                                            <th> {{$Report->arrivaldate}} </th>
                                            <th> {{$Report->departuredate}} </th>
                                            <th> {{$Report->night}} </th>
                                            <th> {{$Report->number_of_room}}</th>
                                            <th> {{$Report->payment_mode}}</th>
                                            <th> {{$Report->total_amount}}</th>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

my route:

// Voucher Accounting
    Route::resource('vouchers-accountings', 'VouchersAccountingController');
    Route::get('vouchers-accountings', 'VouchersAccountingController@index')->name('vouchers-accountings.index');
    Route::post('vouchers-accountings', 'VouchersAccountingController@search')->name('vouchers-accountings.search');
});
0 likes
15 replies
tykus's avatar

Can you post the full contents of the error message - especially the part where it describes where the error occurs?

tzak9022's avatar

@tykus Ops sorry there is no error, actually, no data is pulled back I don't know why

tduffy's avatar

What data are you expecting it to return? Did you make sure that the database has all the data it needs to return something? What happens when you just dd(var_dump()) the results of the query from within your controller?

tzak9022's avatar

@tduffy Yes actually I have the data because when I hardcode $from and $to to a specific date it would display all the data based on that date range, in this situation I have a from and to datepicker you select a "from" date and "to" date, when you click Search it should populate the page with the data

tzak9022's avatar

@tduffy the only issue I'm seeing is, if I'm setting

  $from = date('01-07-2022');
  $to = date('01-09-2022');

like this, it wouldn't fetech the data, I have to change it to

  $from = date('2022-07-01');
  $to = date('2022-09-01');

Here is my main.js

$(document).ready(function() {
    window._token = $('meta[name="csrf-token"]').attr('content')

    moment.updateLocale('en', {
        week: { dow: 1 } // Monday is the first day of the week
    })

    $('.date').datetimepicker({
        format: 'DD-MM-YYYY',
        locale: 'en'
    })

    $('.datetime').datetimepicker({
        format: 'DD-MM-YYYY HH:mm:ss',
        locale: 'en',
        sideBySide: true
    })

    $('.timepicker').datetimepicker({
        format: 'HH:mm:ss'
    })

    $('.select-all').click(function() {
        let $select2 = $(this).parent().siblings('.select2')
        $select2.find('option').prop('selected', 'selected')
        $select2.trigger('change')
    })
    $('.deselect-all').click(function() {
        let $select2 = $(this).parent().siblings('.select2')
        $select2.find('option').prop('selected', '')
        $select2.trigger('change')
    })

    $('.select2').select2()

    $('.treeview').each(function() {
        var shouldExpand = false
        $(this).find('li').each(function() {
            if ($(this).hasClass('active')) {
                shouldExpand = true
            }
        })
        if (shouldExpand) {
            $(this).addClass('active')
        }
    })

    $('a[data-toggle^="push-menu"]').click(function() {
        setTimeout(function() {
            $($.fn.dataTable.tables(true)).DataTable().columns.adjust();
        }, 350);
    })
})

on line 8 it shows as DD-MM-YYYY, I tried to change it to YYYY-MM-DD but it didn't work

I'm not sure what's the problem

tzak9022's avatar

@jlrdw Like this

$from = $request->from->format('Y-m-d');
$to = $request->to->format('Y-m-d');

error:

Error
Call to a member function format() on null 
tduffy's avatar

@tzak9022

Try

$from = DateTime::createFromFormat('d-m-y', $request->from)->format('Y-m-d');
$to = DateTime::createFromFormat('d-m-y', $request->to)->format('Y-m-d');
tzak9022's avatar

@tduffy Thank you for your help

I noticed that after clicking Search to fetch the data, the datepicker will change the date by itself to a range of between 2001 and 2031 why is that?

jlrdw's avatar

@tzak9022 look at the documentation for the date picker.

Also notice that @tduffy example is following the example given. You could have worked that out by following the example.

Please or to participate in this conversation.