Laravel - 编辑具有多态关系的产品

时间:2018-04-04 12:23:58

标签: php laravel laravel-5 polymorphic-associations

我有一个系统,用户需要能够更改产品。但问题是,这个产品与主题有多态关系。这是积累

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

现在我需要用户能够更改产品。目前,我在控制器中完成了这样的操作

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');
}

但由于它的多态关系,我不知道如何改变,例如" composer_package"与所选产品相关联的。比方说,我想更改ID为1的产品。然后我还希望能够更改id为1的composer_package,因为那个与所选产品相关联。另外,我如何更改产品的类型。所选产品为App\Theme,我想将其更改为App\Plugin。如何通过这种多态关系实现上述两件事

这是收集数据的表单。它非常大

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

提前致谢!

修改

产品型号

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');
}

}

主题模型

class Theme extends Model
{

protected $fillable = [

];

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

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

1 个答案:

答案 0 :(得分:0)

composer_package添加到Theme::$fillable

调整HTML表单:name="productable[composer_package]"

然后你可以像这样更新主题:

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