public function __get($name) { if ($name == 'author') { if (!empty($this->author_id) && $this->author_id > 0) { $this->author = Author::findById($this->author_id); } } return parent::__get($name); }
/** * Метод - чтение из недоступного свойства * @param $name имя недоступного свойства * @return object | bool | null */ public function __get($name) { switch ($name) { case 'author': return Author::findById($this->author_id); break; default: return null; } }
/** @test */ public function it_deletes_all_specified_relations() { $user = User::create([]); $author = Author::create(['user_id' => $user->id]); $post = Post::create(['author_id' => $author->id]); $comment = Comment::create(['post_id' => $post->id]); $user = $user->fresh(); $this->assertEquals($user->author()->count(), 1, 'author'); $this->assertEquals($user->author()->first()->posts()->count(), 1, 'posts'); $this->assertEquals($user->author()->first()->posts()->first()->comments()->count(), 1, 'comments'); $user->delete(); $this->assertEquals(Post::where('author_id', 1)->count(), 0, 'post 0'); $this->assertEquals(Comment::where('post_id', 1)->count(), 0, 'comment 0'); $this->assertEquals(Author::where('user_id', 1)->count(), 0, 'user 0'); }
/** @test */ public function it_records_model_update() { $user = User::find(1); Auth::login($user); $user->name = "Yossi"; $user->save(); Auth::logout(); $author = Author::find(1); $author->update(['role' => 'admin']); $log = $user->logs()->wasUpdated()->first(); $this->assertEquals($log->before, ['name' => 'Dolly']); $this->assertEquals($log->after, ['name' => 'Yossi']); $this->assertEquals($log->user_id, 1); $log = $author->logs()->wasUpdated()->first(); $this->assertEquals($log->before, ['role' => 'editor']); $this->assertEquals($log->after, ['role' => 'admin']); $this->assertEquals($log->user_id, null); }