I want to populate select box which will have an array of objects.
Here's the array:
array (size=2)
0 =>
object(stdClass)[208]
public 'tag_identifier' => string 'jewellery' (length=9)
public 'id' => string '1' (length=1)
1 =>
object(stdClass)[207]
public 'tag_identifier' => string 'jewellery-rings' (length=15)
public 'id' => string '3' (length=1)
What I am trying to achieve is that I want to update the products tags part from the edit form and while doing so, I want that whatever tags was inserted while adding the product should be highlighted.
Here's the controller method
public function edit( $id ) {
if(\Auth::guest()) {
return redirect('/admin');
}
$arr = [
'products.id AS product_id', 'products.name AS product_name', 'products.category_id AS cat_id', 'products.description AS product_description', 'products.quantity AS product_quantity', 'products.rate AS product_rate', 'products.discount_rate AS product_discount_rate', 'products.display AS product_display', 'products.approval AS product_approval',
'categories.id AS category_id', 'categories.name AS category_name'
];
$product = \DB::table('products')
->join('categories', 'products.category_id', '=', 'categories.id')
->where('products.id', '=', $id)
->select($arr)
->first();
if ( $product ) {
$category = Category::lists('name', 'id');
$tag_lists = \DB::table('product_tag')
->join('products', 'products.id', '=', 'product_tag.product_id')
->join('tags', 'tags.id', '=', 'product_tag.tag_id')
->where('product_tag.product_id', '=', $id)
->select('tags.name AS tag_identifier', 'tags.id')
->get();
$tags = Tag::lists('name', 'id');
return view('products.edit')
->with('product', $product)
->with('category', $category)
->with('tag_lists', $tag_lists)
->with('tags', $tags);
}
\Session::flash('no_product', 'Sorry! The product that you are looking for could not be found');
return \Redirect::back();
}
And in my views:
<div class="form-group">
{!! Form::label('tag_lists', 'Tags:') !!}
{!! Form::select('tag_lists[]', $tags, [$tag_lists], ['class' => 'form-control input-sm', 'multiple']) !!}
</div>
$tags
is used when there are no products having tags, that means it will show all the tags and vice-versa even when the tags are selected.
Kindly help me. Thanks.
UPDATE 1: After this answer, here's the array that I have got and it didn't helped me out.
array (size=2)
1 => null
3 => null
UPDATE 2: There was a typo in UPDATE 1 and hence the result. Here's what I get after editing the typo..
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in field list is ambiguous (SQL: select `name` as `tag_identifier`, `id` from `product_tag` inner join `products` on `products`.`id` = `product_tag`.`product_id` inner join `tags` on `tags`.`id` = `product_tag`.`tag_id` where `product_tag`.`product_id` = 7)
P.S.: All the values are coming from the database. I am at a learning stage and that is why I have not used form model binding, reason, I want to go step by step.