This post is from the professional Laravel developer community, original link: learnku.com/laravel/t/2…
I was recently refactoring code for a project, and I found myself writing the same method on the Eloquent model to define the relationship of the Account class. And just for your information, I prefer to use getters and setters instead of accessing magic properties directly.
So I’m going to demonstrate this with a Post model that looks like this:
<? php namespace App; use Illuminate\Database\Eloquent\Model; /** * Class Post * * @package App */ class Post extends Model { /** * @return string
*/
public function getTitle()
{
return $this->getAttribute('title');
}
/**
* @param string $title
* @return $this
*/
public function setTitle(string $title)
{
$this->setAttribute('title'.$title);
return $this; } / * * * @return string
*/
public function getPost()
{
return $this->getAttribute('post');
}
/**
* @param string $post
* @return $this
*/
public function setPost(string $post)
{
$this->setAttribute('post'.$post);
return $this;
}
/**
* @param Account $account
* @return $this
*/
public function setAccount(Account $account)
{
$this->account()->associate($account);
return $this; } / * * * @return Account|null;
*/
public function getAccount()
{
return $this->getAttribute('account'); } / * * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function account()
{
return $this->belongsTo(Account::class, 'account_id'.'id'); }}Copy the code
We defined some methods, partly for the Post property and partly for the correlation relationship.
Now, when we add another model associated with Account, we add the same correlation method. This is time consuming, and if you want to change the methods, you need to change the methods on all the models that have relationships.
The use of Traits
Definition of traits:
Traits are a code reuse mechanism for single-inheritance languages like PHP. Traits reduce the limitations of a single-inheritance language by giving developers the freedom to reuse methods in separate classes within different hierarchies. The semantics of Trait and Class combinations define a way to reduce complexity and avoid the typical problems associated with traditional multiple inheritance and Mixin classes.
Traits are similar to classes, but merely aim to combine functionality in a fine-grained and consistent way. Traits cannot be instantiated by themselves. It adds a combination of horizontal features to traditional inheritance; That is, there is no need for inheritance between the classes of the application.
This is a perfect reason to use traits. We create a Trait called HasAccountTrait, which will hold all methods of all relationships that belong to an Account:
<? php namespace App; /** * Class HasAccountTrait * * @package App */ trait HasAccountTrait { /** * @param Account$account
* @return $this
*/
public function setAccount(Account $account)
{
$this->account()->associate($account);
return $this; } / * * * @return Account|null;
*/
public function getAccount()
{
return $this->getAttribute('account'); } / * * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function account()
{
return $this->belongsTo(Account::class, 'account_id'.'id'); }}Copy the code
We then use a Trait to rearrange the Post model code above, so it looks something like this:
<? php namespace App; use Illuminate\Database\Eloquent\Model; /** * Class Post * * @package App */ class Post extends Model { use HasAccountTrait; / * * * @return string
*/
public function getTitle()
{
return $this->getAttribute('title');
}
/**
* @param string $title
* @return $this
*/
public function setTitle(string $title)
{
$this->setAttribute('title'.$title);
return $this; } / * * * @return string
*/
public function getPost()
{
return $this->getAttribute('post');
}
/**
* @param string $post
* @return $this
*/
public function setPost(string $post)
{
$this->setAttribute('post'.$post);
return $this; }}Copy the code
conclusion
Using traits means writing code that doesn’t repeat itself when defining relationships for the same model. It can also speed up development, depending on how many relationships exist in the application.