Code Monkey home page Code Monkey logo

Comments (2)

CarterBland avatar CarterBland commented on July 30, 2024

I believe it'd read $object->some_date instead of being appended to $object->order_at, but I could be wrong..

// Model
protected $dates = ['ordered_at', 'created_at', 'updated_at']
public function getSomeDateAttribute()
{
    return $this->ordered_at->format('m-d');
}

// View
{{ $object->ordered_at->toDateString() }}
{{ $object->some_date }}

Here's what I'd expect
https://laravel.com/docs/5.8/eloquent-mutators#defining-an-accessor

from laravel-best-practices.

wize-wiz avatar wize-wiz commented on July 30, 2024

@CarterBland @clemblanco True, it should be $object->some_date to actually access the accessor. $object->ordered_at will simply return a Carbon instance.

This example also lacks understanding from many perspectives.

  • It makes no sense to call $object->ordered_at->toDateString() if there was no format applied.
  • It would make no sense to override the attribute ordered_at with getOrderedAtAttribute where the original value of ordered_at becomes inaccessible on first sight. This could be solved by using $object->attributes['ordered_at'], but that is the raw state of the attribute which only makes sense if it is intentional.

If 3 dates are available, and to format one date, it should be named similar to the original attribute, e.g.

protected $dates = ['ordered_at', 'created_at', 'updated_at']
public function getOrderedAtFormatAttribute()
{
    return $this->ordered_at->format('m-d');
}

// View
{{ $object->ordered_at_format }}
{{ $object->ordered_at_format->toDateString() }}

from laravel-best-practices.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.