fixed page detail

This commit is contained in:
Edward Yang 2022-08-11 18:53:46 +08:00
parent d910da229b
commit 71c675fb84
3 changed files with 64 additions and 2 deletions

View File

@ -236,6 +236,23 @@ function currency_format($price, string $currency = '', string $value = '', bool
return CurrencyService::getInstance()->format($price, $currency, $value, $format);
}
/**
* 时间格式化
*
* @param null $datetime
* @return false|string
*/
function time_format($datetime = null)
{
$format = 'Y-m-d H:i:s';
if ($datetime instanceof Illuminate\Support\Carbon) {
return $datetime->format($format);
} elseif (is_int($datetime)) {
return date($format, $datetime);
}
return date($format);
}
/**
* 图片缩放
*

View File

@ -12,10 +12,17 @@
namespace Beike\Shop\Http\Controllers;
use Beike\Models\Page;
use Beike\Shop\Http\Resources\PageDetail;
class PageController extends Controller
{
public function show()
public function show(Page $page)
{
$page->load('description');
$data = [
'page' => (new PageDetail($page))->jsonSerialize()
];
dd($data);
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* PageDetail.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-11 18:45:02
* @modified 2022-08-11 18:45:02
*/
namespace Beike\Shop\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class PageDetail extends JsonResource
{
/**
* @param Request $request
* @return array
* @throws \Exception
*/
public function toArray($request): array
{
$description = $this->description;
return [
'id' => $this->id,
'title' => $description->title,
'content' => $description->content,
'meta_title' => $description->meta_title,
'meta_description' => $description->meta_description,
'meta_keyword' => $description->meta_keyword,
'created_at' => time_format($this->created_at),
'updated_at' => time_format($this->updated_at),
];
}
}