За последние 24 часа нас посетили 20862 программиста и 1138 роботов. Сейчас ищут 372 программиста ...

appends & hasmany

Тема в разделе "Laravel", создана пользователем Exort, 11 май 2019.

  1. Exort

    Exort Активный пользователь

    С нами с:
    30 апр 2016
    Сообщения:
    100
    Симпатии:
    2
    PHP:
    1. class Post extends Model
    2. {
    3.     public $guarded = ['tags'];
    4.     public $appends = ['datetime', 'tags', 'comments'];
    5.     protected $hidden = ['created_at', 'updated_at'];
    6.     protected $dates = ['datetime'];
    7.  
    8.     public function getDatetimeAttribute()
    9.     {
    10.         return $this->created_at->format('h:i d.m.Y');
    11.     }
    12.  
    13.     public function getTagsAttribute()
    14.     {
    15.         return $this->tags();
    16.     }
    17.  
    18.     public function getCommentsAttribute()
    19.     {
    20.         return $this->comments();
    21.     }
    22.  
    23.     public function tags()
    24.     {
    25.         return $this->hasMany('App\Tag', 'post_id', 'id');
    26.     }
    27.  
    28.     public function comments()
    29.     {
    30.         return $this->hasMany('App\Comment', 'post_id', 'id');
    31.     }
    32. }
    tags & posts возвращают пустой массив, данные в базе есть. не отрабатывают $this->tags(). что делать? спасибо. даже если я делаю $this->tags не методом, а свойством, он напишет - Undefined Proprty.
     
    #1 Exort, 11 май 2019
    Последнее редактирование: 11 май 2019