简体   繁体   English

如何将数组传递给 laravel 中的特征?

[英]how to pass array to trait in laravel?

I'm trying to refactor my controller I just create Trait and put some method in it:我正在尝试重构我的 controller 我只是创建了 Trait 并在其中放入了一些方法:

my controller before(one method):我之前的 controller(一种方法):

public function edit(Product $product)
{
    $categories = Category::get();

    $main_image = $product->images()->where('main_image', 1)->first();
    if ($main_image) {
        $image = [];
        $FileUploader = new \FileUploader('other_images',[
            'uploadDir' => public_path('/uploads/product_images/'),
            'title' => 'auto'
        ]);
        $images = $FileUploader->upload();
        foreach ($images['files'] as $img) {
            ProductImage::create([
                'image' => $img['name'],
                'product_id' => $product->id,
                'main_image' => false,
            ]);
        }
    }

    $other_images = $product->images()->where('main_image', 0)->get();
    if ($other_images) {
        $images = [];
        $image[] = [
            "name"  => $main_image->image,
            "type"  => \FileUploader::mime_content_type($main_image->image_path),
            "size"  => filesize('uploads/product_images/' . $main_image['image']),
            "file"  => $main_image->image_path,
            "local" => $main_image->image_path,
            'data' => [
                "id"  => $main_image->id,
            ],
        ];
    }

    return view('merchant.product.update',compact(
        'product',
        'categories',
        'image',
        'images'
    ));
}

After create Trait:创建特征后:

public function edit(Product $product)
{
    $categories = Category::get();

    $main_image = $product->images()->where('main_image', 1)->first();

    if ($main_image) {
        $image = [];
        $this->ShowMainImage($main_image, $image);
    }

    $other_images = $product->images()->where('main_image', 0)->get();

    if ($other_images) {
        $images = [];
        $this->ShowOtherImages($other_images,$images);
    }

    return view('merchant.product.update',compact(
        'product',
        'categories',
        'image',
        'images'
    ));
}

MY trait:我的特点:

trait ProductTrait{

    public function ShowMainImage($main_image,$image)
    {
        $image[] = [
            "name"  => $main_image->image,
            "type"  => \FileUploader::mime_content_type($main_image->image_path),
            "size"  => filesize('uploads/product_images/' . $main_image['image']),
            "file"  => $main_image->image_path,
            "local" => $main_image->image_path,
            'data' => [
                "id"  => $main_image->id,
            ],
        ];
    }

    public function ShowOtherImages($other_images,$images)
    {
        foreach ($other_images as $image) {
            $images[] = [
                "name"  => $image->image,
                "type"  => \FileUploader::mime_content_type($image->image_path),
                "size"  => filesize('uploads/product_images/' . $image['image']),
                "file"  => $image->image_path,
                "local" => $image->image_path,
                'data' => [
                    "id"  => $image->id,
                ],
            ];
        }
    }

}

The first which is before is working but the second on is not!之前的第一个正在工作,但第二个没有! the problem with the array's of $image and $images $image$images数组的问题

How can I send empty array to trait and resice the array with data如何将空数组发送到特征并用数据重新调整数组

You send image and images but you are not returning them back, what you can do is return image and images from Trait methods instead of sending them您发送imageimages但没有返回它们,您可以做的是从 Trait 方法返回imageimages而不是发送它们

public function edit(Product $product)
{
   $categories = Category::get();

   $main_image = $product->images()->where('main_image', 1)->first();

   if ($main_image) {
       $image = $this->ShowMainImage($main_image);;
   }

   $other_images = $product->images()->where('main_image', 0)->get();

   if ($other_images) {
      $images = $this->ShowOtherImages($other_images);
  }

  return view('merchant.product.update',compact(
    'product',
    'categories',
    'image',
    'images'
   ));
}

and your trait will be你的特质将是

trait ProductTrait{

public function ShowMainImage($main_image)
{
    return [
        "name"  => $main_image->image,
        "type"  => \FileUploader::mime_content_type($main_image->image_path),
        "size"  => filesize('uploads/product_images/' . $main_image['image']),
        "file"  => $main_image->image_path,
        "local" => $main_image->image_path,
        'data' => [
            "id"  => $main_image->id,
        ],
    ];
}

public function ShowOtherImages($other_images)
{
   $images = [];
    foreach ($other_images as $image) {
        $images[] = [
            "name"  => $image->image,
            "type"  => \FileUploader::mime_content_type($image->image_path),
            "size"  => filesize('uploads/product_images/' . $image['image']),
            "file"  => $image->image_path,
            "local" => $image->image_path,
            'data' => [
                "id"  => $image->id,
            ],
        ];
    }

   return $images;
}
}

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

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