javascript,  laravel,  mysql,  php

Laravel-date format variable SQL query for API value

given date value in string format , getting error in SQL query , so have to change it into date format in Laravel using Carbon function ( for date time )

laravel controller sample work to get values in json [RestAPI]
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Resources\OrdersCollection;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use app\Orders;
use Response;
use Carbon\Carbon;

class OrdersController extends Controller
{
    //
    public function index(Request $request)
    {
        $fdate = $request->query('fdate');
        $gdate = $request->query('gdate');


        $users = DB::table('abctable')
            ->select(DB::raw('count(*) as count, city'))
            ->join('n3d_sales_flat_order_address', 
         'abctable.entity_id', '=', 'xyztable.parent_id')
            ->whereBetween('abctable.created_at',[new Carbon($fdate),new Carbon($gdate)])
            ->groupBy('xyztable.city')
            ->get();
           
       return Response::json($users);
    }
}

Carbon function

$users = DB::table(‘abctable’)
->select(DB::raw(‘count(*) as count, city’))
->join(‘n3d_sales_flat_order_address’,
‘abctable.entity_id’, ‘=’, ‘xyztable.parent_id’)
->whereBetween(‘abctable.created_at’,[new Carbon($fdate),new Carbon($gdate)])
->groupBy(‘xyztable.city’)
->get();

Leave a Reply

Your email address will not be published. Required fields are marked *

19 + nineteen =