-
Notifications
You must be signed in to change notification settings - Fork 50
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
Can't create a new record directly form the base class #50
Comments
Can you illustrate what it is that you really need? What is it that requires:
I general these use cases are a code smell of some other factoring problem. As stated in #47 there are numerous edge cases that quickly evolve from this kind of approach that are normally solved better in you application logic than they are in this trait. |
Closing out a long ago asked question. With more details and a PR we can revisit. |
@jonspalmer |
@darron1217 Can you elaborate on the need. Some example code would be helpful to be able to offer advice on the right solution. |
@jonspalmer Imagine that you are registering vehicle with type option. HTML <form>
<select name="type">
<option>car</option>
<option>truck</option>
</select>
<input type="text" name="color" />
</form> PHP public function store(Request $request)
{
Vehicle::create($request);
return redirect()->route('vehicle.index');
} |
As mentioned in #47 the trick is you really need to create an instance of the concrete class. Perhaps you can use the type map to do that it would be something like $childTypes = Vehicle::getSingleTableTypeMap();
$requestType = $request['type'];
$requestTypeClass = $childTypes[$requestType];
(new $requestTypeClass)->newInstance([], true);
... |
@jonspalmer Then, how about adding it as function like |
What would the API be on those methods? Can you Pseudo code it? A PR with tests would be welcomed. |
Okay, I'll create PR for it. BTW, would you re-open this issue? |
@jonspalmer I created PR. Please check |
I need to be able to create a record directly from the base class because of the code base that I have.
Obviously I can't refactor everything with out a step by step process. And also I need to support a type field without a
class
, like 'other' for instance. I know this issue/proposal was also asked here: #47 and I know the trade offs this could imply. We love this trait but as I've already said, having a large base code we need some way to add functionality in small cycles. I'm not the only one in this situation.Right now we have a workaround idea. For those how have the same problem.
A
Base Class
thatextends
fromModel
in which we have all of our configuration then the normalclass
extends
from it and then the inheritances. So we can call thebase
anywhere without problems.Anyway, This is a great tool. Thanks for the work
The text was updated successfully, but these errors were encountered: