Laravel-多个图像爆炸/爆炸

时间:2020-01-13 18:32:15

标签: php sql laravel image

我有多个图像的项目,并且一切正常。我只想在products.blade.php中拥有一张图像,而在productshow.blade.php中拥有所有图像。这就是我的ProductsController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

class ProductsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $products = Product::all();
        return view('products')->with('products', $products);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('createprod');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input=$request->all();
        $images=array();
        if($files=$request->file('images')){
            foreach($files as $file){
                $name=$file->getClientOriginalName();
                $file->move('storage/image',$name);
                $images[]=$name;
            }
        }
        /*Insert your data*/

        Product::insert( [
            'images'=>  implode("|",$images),
            'title' =>$input['title'],
            //you can put other insertion here
        ]);

        return redirect('/products');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $product = Product::find($id);
        return view('productshow')->with('product', $product);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

这是我的products.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">All products
                    <a href="/products/create" class="btn btn-primary" style="margin-left: 70%">New product +</a></div>
                <hr>
                <div class="card-body">
                    @foreach($products as $product)
                        <h2>{{$product->title}}</h2>

                        @foreach (explode('|', $product->images) as $image)
                            <br>                          
                            <a class="image-popup-vertical-fit" href="/storage/image/{{$image}}">
                                <img src="/storage/image/{{$image}}" style="width:100%">
                            </a>
                        @endforeach
                        <br>
                        <hr style="border: 1.5px solid grey">
                    @endforeach
                    <small><?php echo now() ?></small>

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

因此,在我的products.blade.php中,所有图像都显示(爆炸),但是我只需要显示一个图像,例如第一个图像。我怎样才能做到这一点?我不太擅长多幅图像。请帮忙!谢谢!

3 个答案:

答案 0 :(得分:1)

{{ explode('|', $product->images)[0] }}

但是,我建议您在有空的时候看看Laravel的ORM HasMany关系。

答案 1 :(得分:1)

正如您所说,您只想显示场景中的第一张图像。

在您的products.blade.php中而不是@foreach中使用-

@php $img = explode('|', $product->images); @endphp

删除-

@foreach (explode('|', $product->images) as $image)
<br>                          
 <a class="image-popup-vertical-fit" href="/storage/image/{{$image}}">
  <img src="/storage/image/{{$image}}" style="width:100%">
 </a>
@endforeach

然后添加

 <img src="/storage/image/{{$img[0]}}" style="width:100%">

答案 2 :(得分:0)

好像您要在一列中保存多张图像? 那不是好方法。用产品作为外键来制作产品图像的新模型,然后将图像保存在那里并使用 Relationship

**现在**,您可以这样做

@foreach($products as $product)
    <h2>{{$product->title}}</h2>
   <?php 
       $allImages=explode('|', $product->images) 
      ?>
    <br>                          
   <a class="image-popup-vertical-fit" href="/storage/image/{{$allImages[0]->images}}">
        <img src="/storage/image/{{$allImages[0]->images}}" style="width:100%">
     </a>
    <br>
    <hr style="border: 1.5px solid grey">
@endforeach

转换为数组后,获取数组的第一个索引[0] ,而不是对其进行循环。