【问题标题】:Laravel implode function on images array图像数组上的 Laravel 内爆函数
【发布时间】:2019-04-27 01:54:49
【问题描述】:

如何使用图像数组使其成为内爆函数?我收到以下错误:

implode():传递的参数无效

我的控制器:

    public function store(Request $request)
{
    $this->validate($request, [
        'promotion_image' => 'required'
    ]);

    if ($request->has('promotion_image'))
    {   
        //Handle File Upload

        $promotion = [];
        foreach ($request->file('promotion_image') as $key => $file)
        {
            // Get FileName
            $filenameWithExt = $file->getClientOriginalName();
            //Get just filename
            $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
            //Get just extension
            $extension = $file->getClientOriginalExtension();
            //Filename to Store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            //Upload Image
            $path = $file->storeAs('public/promotion_images',$fileNameToStore);
            array_push($promotion, $fileNameToStore);
        }

        $fileNameToStore = serialize($promotion);
    }
    else
    {
        $fileNameToStore='noimage.jpg';
    }

    foreach ($promotion as $key => $value) {
        $promotionImage = new Promotion;
        $promotionImage->promotion_image = implode(' , ',$value);
        $promotionImage->save();
    }
    return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
}

我的看法:

 @foreach($promotions as $promotion)
           <tr>  

      //HERE IS WHERE THE IMAGE ARE VIEWED      <th><img src="{{ asset('storage/promotion_images/' . $promotion->promotion_image) }}" style="width:50px;height:50px;"></th>
            <th><a href="/admin/airlineplus/promotions/{{ $promotion->id  }}/edit" class="fa fa-edit btn btn-primary btn-lg"></a></th>

         </tr> 
         @endforeach

【问题讨论】:

  • 要让它工作,你应该确定$value 实际上是一个数组,因为implode() 函数只接受数组作为第二个参数。将第一行放在 foreach 块 dd($value); 中,以检查这是什么类型的变量。
  • 先生,您能编辑一下代码吗? :( 自从昨晚以来我一直在努力,甚至无法获得正确的代码。
  • 但我想要在我的代码中将图像插入到我的数据库的一列中,这就是它具有内爆功能的原因
  • 我告诉你从哪里开始调试给定的错误。同样,在第二个 foreach 循环的第一行中输入 var_dump($value); exit; 或更短的 dd($value) 并告诉已经得到了什么 - 实际上是什么 $value。它应该是一个数组来消除内爆错误消息。
  • 它在 dd($value) 之后显示一个文本 .."Desert_01.jpg"

标签: mysql arrays laravel implode


【解决方案1】:

如果我猜对了,请尝试不使用 foreach 循环

if (count($promotion)) {
    $implodedPromotion = implode(' , ', $promotion);
    $promotionImage = new Promotion;
    $promotionImage->promotion_image = $implodedPromotion;
    $promotionImage->save();

    return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
}

return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');

补充

如果您的视图中有该值并希望显示这些图像,您应该执行以下操作:

@foreach($promotions as $promotion)
    @php
        $imagesImploded = $promotion->promotion_image;
        $imagesExploded = explode(',', $imagesImploded);
    @endphp
    <tr>
        @foreach($imagesExploded as $image)  
        <th><img src="{{ asset('storage/promotion_images/' . trim($image)) }}" style="width:50px;height:50px;"></th>
        @endforeach
        <th><a href="/admin/airlineplus/promotions/{{ $promotion->id  }}/edit" class="fa fa-edit btn btn-primary btn-lg"></a></th>
    </tr> 
@endforeach

【讨论】:

  • 先生现在一切正常,但我的最后一个问题是我无法在我的视图中显示图像,请帮助最后一次完成我的项目
  • @Tpojka 先生,我编辑上面的代码添加了 VIEW。在那里你会看到我如何查看我的图像。请最后一次查看并帮助我,以便我将您的答案标记为已解决
猜你喜欢
  • 2019-07-26
  • 1970-01-01
  • 2011-06-18
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
相关资源
最近更新 更多