Php 如何在laravel中将数组传递给trait?
我正在尝试重构我的控制器,我只是创建了Trait并在其中加入了一些方法: 我的控制器之前(一种方法): 创建特征后:Php 如何在laravel中将数组传递给trait?,php,laravel,Php,Laravel,我正在尝试重构我的控制器,我只是创建了Trait并在其中加入了一些方法: 我的控制器之前(一种方法): 创建特征后: public function edit(Product $product) { $categories = Category::get(); $main_image = $product->images()->where('main_image', 1)->first(); if ($main_image) { $i
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
如何将空数组发送到trait并使用数据调整数组大小如果您发送
图像
和图像
,但不返回它们,您可以做的是从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;
}
}
Mahmoud兄弟,你能给我一本好书或任何能让我了解PHP OOP的东西吗(我不知道PHP OOP的具体书籍,但我建议您在Lynda上观看这门课程“PHP:面向对象编程”,您可能会在某个地方发现它是免费的,这门课程将让您在PHP中快速启动并运行OOP。您还可以使用公共函数ShowOtherImages($other_images,$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;
}
}