fideloper / Implementing-Laravel

Companion application to the e-book Implementing Laravel
171 stars 40 forks source link

Question on Eloquent methods on EloquentArticle.php #21

Closed jesseschutt closed 10 years ago

jesseschutt commented 10 years ago

Hi Chris,

Thanks for your very helpful book! I'm implementing a lot of the techniques already.

One thing that has stymied me so far is in regards to using Eloquent methods on an injected model. According to line 96 in EloquentArticle.php you have done the following:

$foundTag = $this->tag->where('slug', $tag)->first();

So you have injected the TagInterface into the EloquentArticle class and are using the "where" method, on the Tag model, within EloquentArticle.

Every time I try to do something similar I get a "call to undefined method where()" which leads me to believe I don't have my eloquent model properly injected.

Can you help me here? Thank you!

fideloper commented 10 years ago

Hey! You are correct in questioning that it appears. I actually thought I updated the book and code base for that already, but that might be in a feature branch :/

Anyway, I'm using a Tag model directly instead of using the TagRepository in that specific instance. That's definitely a mistake. I'll need to find that update :D.

For your own use immediately, you can add a bySlug($tag) method in the TagInterface and EloquentTag and find a tag by slug to return to the ArticleRepository method.

jesseschutt commented 10 years ago

OK, so we are not supposed to use the Eloquent methods of an injected model?

I have a class called "EloquentApplication" and it injects "QuestionInterface" and "AnswerInterface" via the constructor. If I want to find all the "Answers" that have a particular Application_ID field, I'm not able to use $this->answer->where('application_id', 4)->get()?

Is that question clear? I guess I'm asking if I'm supposed to be able to use regular Eloquent methods within the EloquentApplication class, OR if I'm supposed to create a different method within the "EloquentAnswer.php" class such as lookupAnswersByApplicationId() and call it from EloquentApplication?

fideloper commented 10 years ago

Well, keep in mind 'not supposed to' isn't a thing. What I mean is , you can choose to follow what I've done, but these aren't rules set in stone.

Definitely question what you read and definitely experiment in code and see what works for you :D

I generally would do the latter of your example. The application repository should be a place to get and set applications. The answer repository is a place to handle answers. That's the single responsibility of each. If we're going to attempt to 'code to an interface' then it might be better to use a repository when the 2 (applications and answers) need to interact.

This isn't set in stone tho. The point is more that Repositories are useful abstractions in your application code. How they become useful (their implementation details) may change depending in your application.

Good luck!

On Saturday, January 11, 2014, Jesse Schutt wrote:

OK, so we are not supposed to use the Eloquent methods of an injected model?

I have a class called "EloquentApplication" and it injects "QuestionInterface" and "AnswerInterface" via the constructor. If I want to find all the "Answers" that have a particular Application_ID field, I'm not able to use $this->answer->where('application_id', 4)->get()?

Is that question clear? I guess I'm asking if I'm supposed to be able to use regular Eloquent methods within the EloquentApplication class, OR if I'm supposed to create a different method within the "EloquentAnswer.php" class such as lookupAnswersByApplicationId() and call it from EloquentApplication?

— Reply to this email directly or view it on GitHubhttps://github.com/fideloper/Implementing-Laravel/issues/21#issuecomment-32104692 .