dongxi7609 2018-04-04 12:23 采纳率: 0%
浏览 88

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

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');
}
}
  • 写回答

1条回答 默认 最新

  • douxie3625 2018-04-05 13:58
    关注

    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'))
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序