Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Presenter with lists() method #24

Open
bandgeekndb opened this issue Sep 30, 2014 · 5 comments
Open

Using Presenter with lists() method #24

bandgeekndb opened this issue Sep 30, 2014 · 5 comments

Comments

@bandgeekndb
Copy link

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.

@RomainLanz
Copy link

@bandgeekndb

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

@laics1984
Copy link

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.

@usrNotFound
Copy link

Hi @laics1984 have a look at this https://laravel.com/docs/5.3/eloquent-mutators

@laics1984
Copy link

laics1984 commented Mar 29, 2017

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>

@usrNotFound
Copy link

usrNotFound commented May 29, 2017

@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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants