简体   繁体   中英

Laravel - Edit product with polymorphic relation

I have a system where to user needs to be able to change a product. But the problem is, This product has a polymoprhic relation with theme. This is the build-up

Products
    - ID
    - productable_id (For instance "1")
    - productable_type (Is either "App\Theme"or "App\Plugin")
    - name (Just a name)
    - description (Just a description)
    - etc
Theme
    - id (1)
    - composer_package

Now I need the user to be able to change a product. Currently, I have it done like this in the controller

public function update(Request $request, $id)
{
    $product = Product::find($id);
    $product->fill($request->all());

    $product->save();

    return redirect('/products')->with('flash', $product->name . ' is succesvol bewerkt');
}

But since its a polymorphic relation I don't know how to change for example the "composer_package" that is associated with the selected product. Say, for instance, I want to change the product with the ID of 1. Then I want to also be able to change the composer_package with the id 1 because that one is associated with that selected product. Also, How can I change for instance, the type of product. The selected product is an App\\Theme and I want to change that to App\\Plugin . How can I achieve the above two things with this polymorphic relation

This is the form that is collecting the data. It's quite large

<div class="modal fade-scale" id="createProduct" tabindex="-1" role="dialog" aria-labelledby="IetsAnders" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="m-0">Product aanmaken</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-12">
                            <form method="POST" action="{{ route('addProduct') }}">
                                {{ csrf_field() }}
                                <div class="form-group">
                                    <label for="name">Naam</label>
                                    <input type="text" name="name" class="form-control" placeholder="Naam">
                                </div>
                                <div class="form-group">
                                    <label for="description">Omschrijving</label>
                                    <textarea name="description" id="" class="form-control" cols="30" rows="5"></textarea>
                                </div>
                                <div class="form-group">
                                    <label for="productable_type">Product type</label>
                                    <select name="type" id="productable_type" class="form-control">
                                        <option selected>Kies een type...</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="price">Prijs</label>
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text" id="currency" style="background-color: white; border-right: none">€</span>
                                        </div>
                                        <input type="number" step="0.01" name="price" class="form-control" value="" aria-describedby="currency" style="border-left: none" placeholder="00.00">
                                    </div>
                                </div>
                                <hr>
                                <div class="form-group">
                                    <label for="period_id">Betalings periode</label>
                                    <select name="period_id" id="" class="form-control">
                                        <option selected>Kies een periode</option>
                                        @foreach($plans as $plan)
                                            <option>{{ $plan->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="periodically_price">Prijs per gekozen periode</label>
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text" id="currency" style="background-color: white; border-right: none">€</span>
                                        </div>
                                        <input type="text" name="periodically_price" class="form-control" value="" aria-describedby="currency" style="border-left: none" placeholder="00.00">
                                    </div>
                                </div>
                                <div class="form-group mb-3">
                                    <label for="thumbnail">Foto van product</label>
                                    <input type="file" name="thumbnail" class="form-control">
                                </div>
                                <div class="form-group mb-3">
                                    <label for="composer_package">Composer package</label>
                                    <input type="text" name="composer_package" class="form-control" placeholder="Composer package">
                                </div>
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Sluiten</button>
                                <button type="submit" class="btn btn-orange">Bewerken</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

Thanks in advance!

EDIT

Product model

class Product extends Model
{

protected $fillable = [
    'productable_type', 'productable_id', 'name', 'description', 'thumbnail', 'price', 'periodically_price', 'period_id'
];

public function productable()
{
    return $this->morphTo();
}

public function order_items()
{
    return $this->hasMany(Orderitems::class);
}

public function plan() {
    return $this->belongsTo(Plan::class, 'period_id');
}

}

Theme Model

class Theme extends Model
{

protected $fillable = [

];

public function webshops()
{
    return $this->hasMany(Webshop::class);
}

public function products()
{
   return $this->morphMany(Product::class, 'productable');
}
}

Add composer_package to Theme::$fillable .

Adjust the HTML form: name="productable[composer_package]"

Then you can update the theme like this:

$product->productable->update($request->get('productable'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM