拉拉维尔 - 多个图像爆炸/内爆



我有多个图像的项目,一切正常。我想要的只是products.blade.php中只有一个图像,productshow.blade.php上的所有图像。所以这是我的ProductsController

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppProduct;
class ProductsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$products = Product::all();
return view('products')->with('products', $products);
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
return view('createprod');
}
/**
* Store a newly created resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
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所有图像都显示(爆炸(,但我只需要显示一张图像,例如第一张图像。我该怎么做?我不太擅长多张图片。请帮忙!谢谢!

正如你所说,你只是想显示来自设置的第一张图像。

在您的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%">

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

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

看起来您要在一列中保存多个图像? 这不是好方法。以产品作为外键制作产品图像的新模型,并将图像保存在那里并使用关系获取。

**现在 ** 你可以这样做

@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],而不是循环它。

最新更新