laravel published_at->format Trying to get property of non-object

- 1 answer

Ad

All of a sudden I have problems with formating a date in my view.

<h5>{{ $post->published_at->format('M jS Y g:ia') }}</h5>

Trying to get property of non-object

Any field is accessable like an object

{{ $post->title }}

But $post->published_at is a string....

I've been this article again: https://laracasts.com/discuss/channels/general-discussion/how-to-carbonparse-in-ddmmyyyy-format

And my model looks allright:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model 
{
    protected $dates = ['published_at'];
    protected $table = 'posts';
    protected $fillable = [
      'slug',
      'title',
      'content',
      'published_at'
    ];

    public function setTitleAttribute($value)
    {
      $this->attributes['title'] = $value;

      if (! $this->exists) {
        $this->attributes['slug'] = str_slug($value);
      }
    }

    public function taxonomy()
    {
        return $this->belongsTo('App\Taxonomies', 'taxonomy_posts', 'post_id', 'taxonomy_id');
    }

    public function categories()
    {
        return $this->belongsToMany('App\Categories', 'categories_posts', 'post_id', 'category_id');
    }

    public function media()
    {
        return $this->belongsToMany('App\Media', 'media_posts', 'post_id', 'media_id');
    }

    public function images()
    {
        return $this->belongsToMany('App\Images', 'post_images', 'post_id', 'image_id')->withPivot('type');
    }

    public function user()
    {
        return $this->belongsTo('App\User', 'author_id');
    }

    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

When I do a var_dump() I get the following:

object(App\Post)#210 (23) {
  ["dates":protected]=>
  array(1) {
    [0]=>
    string(12) "published_at"
  }
  ["table":protected]=>
  string(5) "posts"
  ["fillable":protected]=>
  array(4) {
    [0]=>
    string(4) "slug"
    [1]=>
    string(5) "title"
    [2]=>
    string(7) "content"
    [3]=>
    string(12) "published_at"
  }
  ["connection":protected]=>
  NULL
  ["primaryKey":protected]=>
  string(2) "id"
  ["perPage":protected]=>
  int(15)
  ["incrementing"]=>
  bool(true)
  ["timestamps"]=>
  bool(true)
  ["attributes":protected]=>
  array(9) {
    ["id"]=>
    int(42)
    ["slug"]=>
    string(35) "magni-harum-dolorem-dolorem-laborum"
    ["title"]=>
    string(36) "Magni harum dolorem dolorem laborum."
    ["content"]=>
    string(848) "Quia amet cupiditate dolores perferendis autem minus. Dolor cumque non vel culpa cum eligendi. Aut illo animi ab tenetur corporis ducimus delectus.

Numquam laborum tempora perspiciatis. Veniam assumenda soluta tempora. Non fuga aut molestiae vel. Voluptas maxime accusamus nam quia.

Quod aut ipsum recusandae rem. Ut et repellendus ut mollitia et. Ducimus sed asperiores sed at aspernatur est. Consequatur omnis tempore quia error.

Alias amet corrupti et eligendi aut reiciendis aut labore. Id voluptatem minima modi voluptatem est dolorem tempora. Laudantium enim iure voluptas. Non sit aliquid voluptatem officiis vero quia sed.

Ipsam minima sed nihil ut. Ut sed laudantium libero odit quia culpa.

Dolor doloremque aspernatur dolores explicabo quos ratione libero debitis. Sunt et itaque sit. Quis distinctio et aut repellendus doloribus et."
    ["author_id"]=>
    int(0)
    ["active"]=>
    int(1)
    ["created_at"]=>
    string(19) "2015-12-17 19:44:49"
    ["updated_at"]=>
    string(19) "2015-12-17 19:44:49"
    ["published_at"]=>
    string(19) "2015-12-02 12:56:06"
  }
  ["original":protected]=>
  array(9) {
    ["id"]=>
    int(42)
    ["slug"]=>
    string(35) "magni-harum-dolorem-dolorem-laborum"
    ["title"]=>
    string(36) "Magni harum dolorem dolorem laborum."
    ["content"]=>
    string(848) "Quia amet cupiditate dolores perferendis autem minus. Dolor cumque non vel culpa cum eligendi. Aut illo animi ab tenetur corporis ducimus delectus.

Numquam laborum tempora perspiciatis. Veniam assumenda soluta tempora. Non fuga aut molestiae vel. Voluptas maxime accusamus nam quia.

Quod aut ipsum recusandae rem. Ut et repellendus ut mollitia et. Ducimus sed asperiores sed at aspernatur est. Consequatur omnis tempore quia error.

Alias amet corrupti et eligendi aut reiciendis aut labore. Id voluptatem minima modi voluptatem est dolorem tempora. Laudantium enim iure voluptas. Non sit aliquid voluptatem officiis vero quia sed.

Ipsam minima sed nihil ut. Ut sed laudantium libero odit quia culpa.

Dolor doloremque aspernatur dolores explicabo quos ratione libero debitis. Sunt et itaque sit. Quis distinctio et aut repellendus doloribus et."
    ["author_id"]=>
    int(0)
    ["active"]=>
    int(1)
    ["created_at"]=>
    string(19) "2015-12-17 19:44:49"
    ["updated_at"]=>
    string(19) "2015-12-17 19:44:49"
    ["published_at"]=>
    string(19) "2015-12-02 12:56:06"
  }
  ["relations":protected]=>
  array(5) {
    ["user"]=>
    NULL
    ["comments"]=>
    object(Illuminate\Database\Eloquent\Collection)#207 (1) {
      ["items":protected]=>
      array(0) {
      }
    }
    ["media"]=>
    object(Illuminate\Database\Eloquent\Collection)#211 (1) {
      ["items":protected]=>
      array(0) {
      }
    }
    ["images"]=>
    object(Illuminate\Database\Eloquent\Collection)#233 (1) {
      ["items":protected]=>
      array(0) {
      }
    }
    ["categories"]=>
    object(Illuminate\Database\Eloquent\Collection)#206 (1) {
      ["items":protected]=>
      array(0) {
      }
    }
  }
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["appends":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["dateFormat":protected]=>
  NULL
  ["casts":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["with":protected]=>
  array(0) {
  }
  ["morphClass":protected]=>
  NULL
  ["exists"]=>
  bool(true)
  ["wasRecentlyCreated"]=>
  bool(false)
}
Ad

Answer

Ad

It appears the error came from another line then Laravel told me. Another field was empty.

Ad
source: stackoverflow.com
Ad