Adding Another Presenter Type

Like I said above, by default the type will be deafult but, you can add more types as you need.

Here is an example:

1use App\Presenters\UserPresenter;
2use Coderflex\LaravelPresenter\Concerns\CanPresent;
3use Coderflex\LaravelPresenter\Concerns\UsesPresenters;
4// ...
5 
6class User extends Authenticatable implements CanPresent
7{
8 use UsesPresenters;
9 
10 protected $presenters = [
11 'default' => UserPresenter,
12 'setting' => UserSettingPresenter,
13 ];
14}

Generate new UserSettingPresenter

1php artisan presenter:make UserSettingPresenter

Add anything to UserSettingPresenter method

1...
2class UserSettingPresenter extends Presenter
3{
4 public function lang()
5 {
6 return $this->model->settings->defaultLang;
7 }
8}
9...

Finally, set setting as a type:

1$user->present('setting')->lang;

By that, you can split your logic and make your code base even cleaner.

Join our newsletter

Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

We care about the protection of your data. Read our Privacy Policy.