Laravel created_at返回对象代替数据库中的日期格式?

时间:2017-12-26 10:46:58

标签: php mysql laravel laravel-5 php-carbon

我正在尝试从Laravel中的JSON响应中的users表返回<button id="buyButton">Buy</button> <script> /** * Builds PaymentRequest for credit cards, but does not show any UI yet. * * @return {PaymentRequest} The PaymentRequest oject. */ function initPaymentRequest() { let networks = ['amex', 'diners', 'discover', 'jcb', 'mastercard', 'unionpay', 'visa', 'mir']; let types = ['debit', 'credit', 'prepaid']; let supportedInstruments = [{ supportedMethods: networks, }, { supportedMethods: ['basic-card'], data: {supportedNetworks: networks, supportedTypes: types}, }]; let details = { total: {label: 'Donation', amount: {currency: 'USD', value: '55.00'}}, displayItems: [{ label: 'Original donation amount', amount: {currency: 'USD', value: '65.00'}, },{ label: 'Friends and family discount', amount: {currency: 'USD', value: '-10.00'}, } ] }; return new PaymentRequest(supportedInstruments, details); } /** * Invokes PaymentRequest for credit cards. * * @param {PaymentRequest} request The PaymentRequest object. */ function onBuyClicked(request) { request.show().then(function(instrumentResponse) { sendPaymentToServer(instrumentResponse); }) .catch(function(err) { ChromeSamples.setStatus(err); }); } /** * Simulates processing the payment data on the server. * * @param {PaymentResponse} instrumentResponse The payment information to * process. */ function sendPaymentToServer(instrumentResponse) { // There's no server-side component of these samples. No transactions are // processed and no money exchanged hands. Instantaneous transactions are not // realistic. Add a 2 second delay to make it seem more real. window.setTimeout(function() { instrumentResponse.complete('success') .then(function() { document.getElementById('result').innerHTML = instrumentToJsonString(instrumentResponse); }) .catch(function(err) { ChromeSamples.setStatus(err); }); }, 2000); } /** * Converts the payment instrument into a JSON string. * * @private * @param {PaymentResponse} instrument The instrument to convert. * @return {string} The JSON string representation of the instrument. */ function instrumentToJsonString(instrument) { let details = instrument.details; details.cardNumber = 'XXXX-XXXX-XXXX-' + details.cardNumber.substr(12); details.cardSecurityCode = '***'; return JSON.stringify({ methodName: instrument.methodName, details: details, }, undefined, 2); } const payButton = document.getElementById('buyButton'); payButton.setAttribute('style', 'display: none;'); if (window.PaymentRequest) { let request = initPaymentRequest(); payButton.setAttribute('style', 'display: inline;'); payButton.addEventListener('click', function() { onBuyClicked(request); request = initPaymentRequest(); }); } else { ChromeSamples.setStatus('This browser does not support web payments'); } </script> datetime。

在我的数据库中,它显示的值为 created_at但是当我尝试返回JSON api时,它会转换为

2016-07-18 00:00:00

如何解决此问题。

4 个答案:

答案 0 :(得分:10)

默认情况下,created_atupdated_atCarbon个对象,因此您可以这样做:

$object->created_at->toDateTimeString();

再次采用格式Y-m-d H:i:s

答案 1 :(得分:6)

您在Laravel框架中拥有Carbon,可以使用以下代码帮助您根据需要制作日期时间。

        $created_at = new Carbon($value)->toDateTimeString();

现在在$created_at的地方传递created_at。 用户表的示例代码(注意:未经测试请检查自己是否需要传递对象或数组)

        $user = User::find(1);
        $user->created_at = new Carbon($user->created_at)->toDateTimeString();

答案 2 :(得分:2)

您可以通过添加serializeUsing方法来更改该行为在AppServiceProvider类中更新日期的json序列化格式,它只影响api调用(docs),这是一个示例:

use Illuminate\Support\Carbon;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Carbon::serializeUsing(function ($carbon) {
            return $carbon->format('Y-m-d H:i:s');
        });
    }

    ...

}

答案 3 :(得分:2)

由于您希望获得JSON响应,因此可以使用Eloquent API Resource将Carbon对象转换为任何格式,例如:

return [
    'created_at' => $this->created_at->toDateTimeString(),
    ....
];