下面是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
这里是控制器:
<?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'
]);
}
}
所以我想从数据库列UPDATE_AT获取日期和时间到Blade 文件,首先输出是这样的2022-12-20T13:20:51.000000Z,然后我try 将输出格式设置为‘日-月-年’,但是,如何才能将输出格式设置为‘日-月-年时间’呢? 例如:2023年9月14日至2023年9月14日07:20:00