douweinu8562 2015-09-21 06:31
浏览 40

编辑时未看到的产品列表,使用laravel 5.1中的表单模型绑定

In my ecommerce project, I have Product and Carousel Model.

Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $fillable = [
        'code', 'name', 'description', 'special_note', 'sort', 'display', 'weight', 'enquiry'
    ];


    public function carousels()
    {
        return $this->belongsToMany('App\Carousel')->withTimestamps();
    }
}

Carousel.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Carousel extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $fillable = ['name', 'display', 'sort'];

    public function products()
    {
        return $this->belongsToMany('App\Product')->withTimestamps();
    }

    public function getProductListAttribute()
    {
        return $this->products->lists('id');
    }
}

Here's the controller:

public function create()
{
    $products = Product::lists('name', 'id');

    return view('admin.carousels.create', compact('products'));
}

public function store(Request $request)
{
    $carousel = Carousel::create($request->all());

    $carousel->products()->attach($request->input('product_list'));

    return redirect()->back();
}

public function edit($id)
{
    $carousel = Carousel::findOrFail($id);

    $products = Product::lists('name', 'id');

    return view('admin.carousels.edit', compact('carousel', 'products'));
}

public function update(Request $request)
{
    dd($request->all());
}

The carousel form:

<div class="col-md-4 col-sm-4">
    <div class="form-group">
        {!! Form::label('name', 'Name:') !!}
        {!! Form::text('name', null, ['class' => 'form-control input-sm', 'id' => 'name']) !!}
    </div>
</div>

<div class="col-md-4 col-sm-4">
    <div class="form-group">
        {!! Form::label('sort', 'Sort:') !!}
        {!! Form::text('sort', null, ['class' => 'form-control input-sm', 'id' => 'sort']) !!}
    </div>
</div>

<div class="col-md-4 col-sm-4">
    <div class="form-group">
        {!! Form::label('display', 'Display:') !!}
        {!! Form::select('display', ['Disabled' => 'Disabled', 'Enabled' => 'Enabled'], null, ['class' => 'form-control input-sm', 'id' => 'display']) !!}
    </div>
</div>

<div class="col-md-12 col-sm-12">
    <div class="form-group">
        {!! Form::label('product_list', 'Products:') !!}
        {!! Form::select('product_list[]', $products, null, ['class' => 'form-control input-sm', 'multiple']) !!}
    </div>
</div>

<div class="col-md-12 col-sm-12">
    <div class="form-group">
        {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary btn-block m_top_15', 'id' => $submitButtonId]) !!}
    </div>
</div>

Now, the problem is that, when I use form model binding to edit the carousel form, I get no product displayed in the select box, though I can see all the products to select from. The selected products are not seen for the particular carousel.

What is the mistake that I have made ? Kindly help me out.

Thanks.

P.S: I am using Laravel 5.1 and came across this issue for the first time. Earlier I used to do this in Laravel 5 without any issue.

  • 写回答

1条回答 默认 最新

  • dongren2128 2015-09-22 20:37
    关注

    This may be related to how lists() is handled in 5.1. From the upgrade guide -

    The lists Method

    The lists method now returns a Collection instance instead of a plain array for Eloquent queries. If you would like to convert the Collection into a plain array, use the all method:

    User::lists('id')->all(); Be aware that the Query Builder lists method still returns an array.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?