php - Products list not seen while editing, using form model binding in laravel 5.1 -
in ecommerce project, have product , 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 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, problem that, when use form model binding edit carousel form, no product displayed in select box, though can see products select from. selected products not seen particular carousel.
what mistake have made ? kindly me out.
thanks.
p.s: using laravel 5.1 , came across issue first time. earlier used in laravel 5 without issue.
this may related how lists()
handled in 5.1. upgrade guide -
the lists method
the lists method returns collection instance instead of plain array eloquent queries. if convert collection plain array, use method:
user::lists('id')->all(); aware query builder lists method still returns array.
Comments
Post a Comment