8000 [Request] Extend Eloquent plurality to hasMany() · Issue #89 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[Request] Extend Eloquent plurality to hasMany() #89

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

Closed
jackmcdade opened this issue Jan 18, 2013 · 12 comments
Closed

[Request] Extend Eloquent plurality to hasMany() #89

jackmcdade opened this issue Jan 18, 2013 · 12 comments

Comments

@jackmcdade
Copy link

Obviously not a game-breaker, but Eloquent's smart plurality does not extend to hasMany(), which just feels awkward gramatically. Example:

<?php

class Group extends Eloquent {

    public function subscribers()
    {
        return $this->hasMany('Subscriber');
    }

}
@taylorotwell
Copy link
Member

What do you mean? You mean you want to do this?

return $this->hasMany('Subscribers');

@jackmcdade
Copy link
Author

Yep, that's it. Probably just my Rails side leaking over, but it would "feel" more right to me.

@Anahkiasen
Copy link
Contributor

I kind of feel silly agreeing with this but this has always bugged me as well. Not top priority of course.

@franzliedke
Copy link
Contributor

-1. I think that's too much magic, too easy to break, and useless with namespaces for model classes.

@jasonlewis
Copy link
Contributor

👎

1 similar comment
@Sopamo
Copy link
Contributor
Sopamo commented Jan 18, 2013

👎

@Anahkiasen
Copy link
Contributor

Ah didn't think about namespaced models yeah.

@JoostK
Copy link
Contributor
JoostK commented Jan 18, 2013

I never li 8000 ke using a class name as a string. I would much rather use:

return $this->hasMany(Subscriber::class());

Now it is clear that a Model is being referred to, and you can also forget to specify the namespace, as now PHP will do that for you. It's a pity PHP doesn't implement the class method by default but it's very easy to add it:

public static function class()
{
    return get_called_class();
}

This can also be done with routing:

Route::get('home/test', HomeController::at('test'));

Automatic namespace resolution and early errors (class does not exists maybe) are the biggest benefits of this approach.

@franzliedke
Copy link
Contributor

@JoostK I am confused. You still have to import the class. And not unlike with 'Subscriber', you still have to write the class name when doing Subscriber::class().

@JoostK
Copy link
Contributor
JoostK commented Jan 18, 2013

For me it's just clearer. Importing is only necessary once per file, then you're good to go for multiple statements.

Also, models are mostly all in the same namespace, so you don't even have to import the namespace/class and it will automatically be correct when e.g. the namespace changes. With just as string you don't have that benefits.

As it just generates a string you can still use a string directly, if you want though.

@taylorotwell
Copy link
Member

Yeah, it's always just been the model name. Think it would be more confusing to switch at this point.

@franzliedke
Copy link
Contributor

Thanks for the clarification, @JoostK. Makes sense now.

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

7 participants
0