简体   繁体   中英

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:

my controller before(one method):

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

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

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;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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