Comments (10)
Can you illustrate what it is that you really need? What is it that requires:
I need to be able to create a record directly from the base class because of the code base that I have.
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.
from single-table-inheritance.
Closing out a long ago asked question. With more details and a PR we can revisit.
from single-table-inheritance.
@jonspalmer
I need the same functionality in my project.
What is the best-practice for creating Model from form request?
from single-table-inheritance.
@darron1217 Can you elaborate on the need. Some example code would be helpful to be able to offer advice on the right solution.
from single-table-inheritance.
@jonspalmer
As I said, Form Request situation need it.
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');
}
from single-table-inheritance.
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);
...
from single-table-inheritance.
@jonspalmer
That's exactly what I'm doing.
Then, how about adding it as function like Vehicle::createSubclass()
and Vehicle::createManySubclass()
?
cuz it's really common situation when using this package.
from single-table-inheritance.
What would the API be on those methods? Can you Pseudo code it? A PR with tests would be welcomed.
from single-table-inheritance.
Okay, I'll create PR for it.
BTW, would you re-open this issue?
from single-table-inheritance.
@jonspalmer I created PR. Please check
from single-table-inheritance.
Related Issues (20)
- "Field 'type' doesn't have a default value" when saving a model HOT 3
- Update Readme HOT 1
- One record two types. HOT 3
- Can we set the type of model using the base model? HOT 5
- hasMany relation doesn't work for parent class
- Broken link in README - Mark Smith's website is gone HOT 1
- Laravel 5.7 Support HOT 6
- Type is not inserted when using Laravel factories HOT 1
- Travis Builds are failing HOT 2
- Laravel 5.8 support
- Support the Eloquent `retrieved` event HOT 1
- Cannot insert record HOT 6
- V0.8.6 causes incorrect attributes to be set HOT 11
- Laravel 6.0 support HOT 1
- Eager loading child model relationship HOT 2
- Enhence performance when querying root model of subclasses HOT 5
- PHP hangs when parent and child inheritance is not setted properly. HOT 3
- `newFromBuilder` does not maintain mutated attributes HOT 2
- onlyTrashed()->forceDelete() seems to lose the SingleTableInheritance scope, deletes way more than intended HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from single-table-inheritance.