this is the image controller ImageController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\ProductImage;
class ImageController extends Controller
{
public function index($id)
{
$product = Product::find($id);
$images = $product->images;
return view('admin.products.images.index')->with(compact('product', 'images'));
}
public function store(Request $request, $id)
{
//guardar la imagen en nuestro proyecto
$file = $request->file('photo');
$path = public_path() . '/images/products';
$fileName = uniqid() . $file->getClientOriginalName();
$file->move($path, $fileName);
//crear 1 registro en la tabla product_images
$productImage = new ProductImage();
$productImage->image = $fileName;
//$productImage->featured = false;
$productImage->product_id = $id;
$productImage->save(); //INSERT
return back();
}
public function destroy()
{
}
}
This is the view of the images
@extends('layouts.app')
@section('title', 'Imágenes de producto')
@section('body-class', 'landing-page sidebar-collapse')
@section('content')
<div class="header header-filter" style="background-image: url('{{ asset('img/profile_city.jpg') }}'); height: 300px;">
</div>
<div class="main main-raised">
<div class="container">
<div class="section text-center">
<h2 class="title">Imágenes del producto "{{ $product->name }}"</h2>
<div style="display: block; margin: 30px auto;">
<form action="" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<label class="btn btn-primary btn-round" for="photo">Elige una imagen</label>
<input class="file" type="file" id="photo" name="photo" required>
<button type="submit" class="btn btn-primary btn-round">Subir nueva imagen</button>
<a href="{{ url('/admin/products') }}" class="btn btn-default btn-round">Cancelar</a>
</form>
</div>
<div class="row">
@foreach($images as $image)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-body" style="margin-bottom: 30px;">
<img src="{{ $image->url }}" class="rounded mx-auto d-block">
<button type="submit" class="btn btn-danger btn-round">Eliminar</button>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
</div>
@include('includes.footer')
@endsection
you can do it like this:
now the views