0

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.

1 Answer 1

2

With Tag::lists('name', 'id') you are already doing it the easiest way. Now you have to apply the same to the query for $tag_lists. This should work:

$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 AS tag_id')
                ->lists('tag_identifier', 'tag_id');
Sign up to request clarification or add additional context in comments.

4 Comments

Have a look at the updated question after your answer.
Kind of saw that coming... lists ignores the specified table and only name is ambiguous. Try my updated answer please.
Nope, when I use var_dump( $tag_lists ) in my view just above the select drop down, I get the following: array (size=1) '' => string 'jewellery-rings' (length=15) and nothing gets selected in the select box.
There is a typo in your answer, instead of tags_id in the lists() method, it should be tag_id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.