laracasts/Presenter

Using Presenter with lists() method

Opened this issue · 5 comments

I am attempting to use the Laracasts Presenter with the lists() method like so:

$physicians = Employee::physician()->get()->lists(<formattedName>, 'id');
// <formattedName> would be the column name of the employee's name.
// Since it is stored as first and last separately, I use the Laracasts Presenter to present the concatenated full name.

I have a namespaced Employee model, and a query scope to filter the query results. My issue is, without calling the present() method, I can't access my presenter method for formatting the name. I can not find a place to insert the present() method that makes the lists() method work properly. Has anyone done this before?

My current workaround was setting a custom getter on the model to get the concatenated full name that way, but I would like to use the presenter method I already wrote if that is possible.

@bandgeekndb

If you write on your custom getter a call to your presenter is it good for you?

hi @bandgeekndb , i am running in to the same issue too. Mind sharing how you did with the custom getter method? sorry im new to laravel and what is custom getter? Thanks.

Sorry, i am still unable to get it to work. Can you please have a look at my code? The select option text is still blank. Thanks.


public function setFullNameAttribute(){
    	$this->attributes['fullname'] = $this->first.' '.$this->last;
}

in blade.php:
<div class="col-md-5">
     {!! Form::select('Page', ['' => ''] + $pages->all()->pluck('fullname', 'id')->toArray(), null, ['class' => 'form-control']) !!}
</div>

@laics1984 just return
public getFullNameAttribute { return $this->first . ' ' . $this->last}; But not sure why you want to do this in model. If you are using pesenter all you have to do is public function fullName() { return ucfirst($this->entity->first) . ' ' . ucfirst($this->entity->last);