简体   繁体   English

在Laravel 5中使用对象数组填充选择框而没有表单绑定

[英]Populate select box with array of object without form binding in laravel 5

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. $tags用于没有标签的产品,这意味着即使选择了标签,它也会显示所有标签,反之亦然。

Kindly help me. 请帮助我。 Thanks. 谢谢。

UPDATE 1 : After this answer, here's the array that I have got and it didn't helped me out. 更新1 :在答案之后, 是我得到的数组,它并没有帮助我。

array (size=2)
  1 => null
  3 => null

UPDATE 2 : There was a typo in UPDATE 1 and hence the result. 更新2 :更新1中有一个错字,因此是结果。 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)

PS : All the values are coming from the database. PS :所有值都来自数据库。 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. 我正处于学习阶段,这就是为什么我没有使用表单模型绑定的原因,我想逐步进行。

With Tag::lists('name', 'id') you are already doing it the easiest way. 使用Tag::lists('name', 'id')您已经在做最简单的方法了。 Now you have to apply the same to the query for $tag_lists . 现在,您必须将相同的内容应用于$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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM