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

melx's avatar
Level 4

fetch Data by Date interval

this code does not work correct, i want to select by date and id

           public function stock_summary(Request $request)
  {


      $id=$request->id;
      $from=$request->from;
      $to=$request->to;

      

   $products=Products::pluck('name','id');

    $inwards = CashSales::with('items')
      ->whereBetween('created_at', [new Carbon($from), new Carbon($to)])

      ->where('item_id', $id)

    ->get();
     dd($inwards);

   return view('stock_summary.show',compact('inwards','products'));


    }

date format in form 'd-m-Y'

date format in DB 'Y-m-d'

how can i reverse my code and work fine

0 likes
6 replies
Snapey's avatar

Is this the standard created_at field? In which case it is a timestamp column.

You should be able to do it as you are doing, but use carbon's createFromFormat and tell it specifically what format your dates are in (including the separator)

eg

$fromdate = Carbon::createFromFormat('d-m-Y',$from);

check these values with dd or use laravel debugbar to check the query being created.

melx's avatar
Level 4

got this error

             The separation symbol could not be found Trailing data
Snapey's avatar

Check the format of the string you get in the request.

melx's avatar
Level 4

@snapey ,

from string format (m-d-Y)

           "01/25/2020 - 01/25/2020"


             public function stock_summary(Request $request)
           {
         $inwards = CashSales::with('items')
          ->where('item_id', $request->id)
        
              ->whereBetween('created_at', [$request->from, $request->to])
              ->get();
             dd($request->from);

HTML

                        <div class=" col-md-4">
          <label for="from">Item Name</label>

        <select class="form-control " name="id" >
          <option data-tokens="">~items~</option>
          @foreach($products as $key=>$type)
          <option value="{{$key}}">{{$type}}</option>
          @endforeach
        </select>
           </div>
          <div class="col-md-6">
          <label for="from">From</label>
          <input type="text" id="from" name="from" class="daterange form-control">
        </div>

am using date range

Snapey's avatar

details matter

01/25/2020 is NOT m-d-Y - it is m/d/Y

Please or to participate in this conversation.