Coder Social home page Coder Social logo

Comments (10)

jonspalmer avatar jonspalmer commented on July 4, 2024

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.

jonspalmer avatar jonspalmer commented on July 4, 2024

Closing out a long ago asked question. With more details and a PR we can revisit.

from single-table-inheritance.

darron1217 avatar darron1217 commented on July 4, 2024

@jonspalmer
I need the same functionality in my project.
What is the best-practice for creating Model from form request?

from single-table-inheritance.

jonspalmer avatar jonspalmer commented on July 4, 2024

@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.

darron1217 avatar darron1217 commented on July 4, 2024

@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.

jonspalmer avatar jonspalmer commented on July 4, 2024

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.

darron1217 avatar darron1217 commented on July 4, 2024

@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.

jonspalmer avatar jonspalmer commented on July 4, 2024

What would the API be on those methods? Can you Pseudo code it? A PR with tests would be welcomed.

from single-table-inheritance.

darron1217 avatar darron1217 commented on July 4, 2024

Okay, I'll create PR for it.

BTW, would you re-open this issue?

from single-table-inheritance.

darron1217 avatar darron1217 commented on July 4, 2024

@jonspalmer I created PR. Please check

from single-table-inheritance.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.