Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript – how can i get the date and time from database to blade

here the done.blade.php :

@extends('layout.app')

@section('title', 'Done Data')

@section('content')

<div class="card shadow">
    <div class="card-header">
        <h4 class="card-title">
            List of request that has been done
        </h4>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-striped">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Request Date</th>
                        <th>Invoice</th>
                        <th>Member</th>
                        <th>Total</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</div>
    
@endsection

@push('js')
    <script>
        $(function(){
            
            function date(date) {
                var date = new Date(date);
                var day = date.getDate(); //Date of the month: 2 in our example
                var months = ["January", "February", "March", "April", "May", "June", "July",
         "August", "September", "October", "November", "December"];
                var month = months[date.getMonth()]; //Month of the Year: 0-based index, so 1 in our example
                var year = date.getFullYear();

                return `${day}-${month}-${year}`;
            }

            const token = localStorage.getItem('token')
            $.ajax({
                url : '/api/order/done',
                headers: {
                    "Authorization": "Bearer" + token
                },
                success : function ({data}) {
                    

                    let row;
                    data.map(function (val,index) {
                       row += `
                       <tr>
                            <td>${index+1}</td>
                            <td>${date(val.updated_at)}</td>
                            <td>${val.invoice}</td>
                            <td>${val.member.member_name}</td>
                            <td>${val.order_total}</td>
                        </tr>
                       `;
                    });
                    
                    $('tbody').append(row)
                }
            });

        });
    </script>
@endpush

here the controller :

<?php

namespace App\Http\Controllers;

use App\Models\Order;
use App\Models\OrderDetail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;

class OrderController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth')->only(['list','approved_list','ongoing_list','done_list']);
        $this->middleware('auth:api')->only(['store','update','destroy','status_change','requested','approved','ongoing','done']);

    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $orders = Order::with('member')-> get();

        return response() -> json([
            'data' => $orders
        ]);
    }

    public function list() {

        return view ('order.index');
    }
    
    public function approved_list() {

        return view ('order.approved');
    }

    public function ongoing_list() {

        return view ('order.ongoing');
    }

    public function done_list() {

        return view ('order.done');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request -> all(), [
            'member_id' => 'required'
        ]);
        if ($validator -> fails()){
            return response() -> json(
                $validator -> errors(), 422
            );
        }

        $input = $request -> all();
        $order = Order::create($input);

        for ($i=0; $i < count($input['id_product']); $i++) {
            OrderDetail::create([
                'id_order' => $order['id'],
                'id_product' => $input['id_product'][$i],
                'quantity' => $input['quantity'][$i],
                'requirements' => $input['requirements'][$i],
                'total' => $input['total'][$i],
            ]);
        }

        return response() -> json([
            'data' => $order
        ]);
    }

    /**
     * Display the specified resource.
     */
    public function show(Order $order)
    {
        return response() -> json([
            'data' => $order
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Order $order)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Order $order)
    {
        $validator = Validator::make($request -> all(), [
            'member_id' => 'required',
            'description' => 'required'
        ]);
        if ($validator -> fails()){
            return response() -> json(
                $validator -> errors(), 422
            );
        }

        $input = $request -> all();
        $order -> update($input);

        OrderDetail::where('id_order',$order['id'])->delete();

        for ($i=0; $i < count($input['id_product']); $i++) {
            OrderDetail::create([
                'id_order' => $order['id'],
                'id_product' => $input['id_product'][$i],
                'quantity' => $input['quantity'][$i],
                'requirements' => $input['requirements'][$i],
                'total' => $input['total'][$i],
            ]);
        }

        return response() -> json ([
            'message' => 'Order Updated',
            'data' => $order
        ]);
    }

    public function status_change(Request $request, Order $order)
    {
        
        $order->update([
            'status'=>$request -> status
        ]);
        
        return response() -> json ([
            'message' => 'Order Updated',
            'data' => $order
        ]);
    }

    public function requested()
    {
        $orders = Order::with('member') -> where('status','Requested')->get();

        return response() -> json([
            'data' => $orders
        ]);
    }


    public function approved()
    {
        $orders = Order::with('member') -> where('status','Approved')->get();

        return response() -> json([
            'data' => $orders
        ]);
    }

    public function ongoing()
    {
        $orders = Order::with('member') -> where('status','Ongoing')->get();

        return response() -> json([
            'data' => $orders
        ]);
    }

    public function done()
    {
        $orders = Order::with('member') -> where('status','Done')->get();

        return response() -> json([
            'data' => $orders
        ]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Order $order)
    {
        File::delete('uploads/'. $order -> image);
        $order -> delete();

        return response() -> json([
            'message' => 'Order Deleted'
        ]);
    }
}

so i want to get the date and time from database column updated_at to the blade file, first the output are like this 2022-12-20T13:20:51.000000Z, then i tried and made the output format to ‘day-month-year’ but, how can i make the output format to be ‘day-month-year time’?
for example : from ’14-September-2023′ to ’14-September-2023 07:20:00′

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can use formatDate the function like this

    function formatDate(date) {
        var dateObject = new Date(date);
        var day = dateObject.getDate();
        var months = ["January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December"];
        var month = months[dateObject.getMonth()];
        var year = dateObject.getFullYear();
        var hours = dateObject.getHours();
        var minutes = dateObject.getMinutes();
        var seconds = dateObject.getSeconds();

        // Add leading zeros if needed
        if (hours < 10) {
            hours = "0" + hours;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }

        return `${day}-${month}-${year} ${hours}:${minutes}:${seconds}`;
    }

and then in blade use it like this

    data.map(function (val, index) {
        row += `
        <tr>
            <td>${index + 1}</td>
            <td>${formatDate(val.updated_at)}</td>
            <td>${val.invoice}</td>
            <td>${val.member.member_name}</td>
            <td>${val.order_total}</td>
        </tr>
        `;
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading