как передать массив в трейт в laravel? - PullRequest
0 голосов
/ 14 июля 2020
• 1000 1006 *
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'
    ));
}

МОЯ черта:

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

}

Первое, что было раньше, работает, а второе - нет! проблема с массивами $image и $images

Как я могу отправить пустой массив в черту и пересчитать массив с данными

1 Ответ

1 голос
/ 14 июля 2020

Вы отправляете image и images, но не возвращаете их обратно, вы можете вернуть image и images из методов 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);;
   }

   $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'
   ));
}

и ваша черта будет

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;
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...