composer require intervention/image
app.php inside config folder
'providers' => [
...
Intervention\Image\ImageServiceProvider::class
],
'aliases' => [
'Image' => Intervention\Image\Facades\Image::class
]//route
Route::resource('brands', BrandController::class);
//controller
<?php
namespace App\Http\Controllers;
use App\Models\Brand;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
class BrandController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$brands = Brand::all();
return view('backend.brands.index', compact('brands'));
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|unique:brands,name',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:1000'
]);
if ($request->hasFile('image')) {
$brand_image = $request->file('image');
$image_origional_name = $brand_image->getClientOriginalName();
$image_name = date('Y-m-d-H-i-s') . '-' . $image_origional_name;
$store_path = 'upload/brand/';
Image::make($brand_image)->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
})->save($store_path . $image_name);
$last_img = $store_path . $image_name;
} else {
$last_img = null;
}
Brand::create(
[
'name' => $request->name,
'image' => $last_img
]
);
return redirect(route('brands.index'));
//
}
/**
* Display the specified resource.
*
* @param \App\Models\Brand $brand
* @return \Illuminate\Http\Response
*/
public function show(Brand $brand)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Brand $brand
* @return \Illuminate\Http\Response
*/
public function edit(Brand $brand)
{
return view('backend.brands.edit', compact('brand'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Brand $brand
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Brand $brand)
{
$this->validate($request, [
'name' => 'required|unique:brands,name,' . $brand->id,
]);
if ($request->hasFile('image')) {
$this->validate($request, [
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:1000'
]);
if ($brand->image != null && file_exists($brand->image)) {
unlink($brand->image);
}
$brand_image = $request->file('image');
$image_origional_name = $brand_image->getClientOriginalName();
$image_name = date('Y-m-d-H-i-s') . '-' . $image_origional_name;
$store_path = 'upload/brand/';
Image::make($brand_image)->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
})->save($store_path . $image_name);
$last_img = $store_path . $image_name;
} else {
$last_img = $brand->image;
}
$brand->update(
[
'name' => $request->name,
'image' => $last_img
]
);
return redirect(route('brands.index'));
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Brand $brand
* @return \Illuminate\Http\Response
*/
public function destroy(Brand $brand)
{
if ($brand->image != null && file_exists($brand->image)) {
unlink($brand->image);
}
$brand->delete();
return redirect(route('brands.index'));
//
}
}
//index blade
@foreach ($brands as $brand)
<tr>
<th scope="row">{{ $brand->id }}</th>
<td>{{ $brand->name }}</td>
<td><img src="{{ $brand->image }}" alt="" height="50" width="50"></td>
<td>{{ $brand->created_at->diffForHumans() }}</td>
<td><a href="{{ route('brands.edit', $brand->id) }}"
class="btn btn-info btn-sm">Edit</a></td>
<td>
<form action="{{ route('brands.destroy', $brand->id) }}"
method="post">
@csrf
@method('DELETE')
<button type="submit" class=" btn btn-sm btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
//create blade
<form action="{{ route('brands.store') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group row">
<label for="" class="col-sm-4 col-form-label">
Name
</label>
<div class="col-sm-8">
<input type="text" class="form-control @error('name') is-invalid @enderror"
name="name" value="{{ old('name') }}">
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
</div>
<div class="form-group row">
<label for="" class="col-sm-4 col-form-label">
Image
</label>
<div class="col-sm-8">
<input type="file"
class="form-control-file form-control @error('image') is-invalid @enderror"
name="image" value="{{ old('image') }}">
@error('image')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
</div>
<div class="row ">
<div class="col-sm-12 text-center">
<button type="submit" class=" text-center btn btn-success">Submit</button>
</div>
</div>
</form>
//edit blade
<form action="{{ route('brands.update', $brand->id) }}" method="post"
enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="form-group row">
<label for="" class="col-sm-4 col-form-label">
Name
</label>
<div class="col-sm-8">
<input type="text" class="form-control @error('name') is-invalid @enderror"
name="name" value="{{ $brand->name }}">
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
</div>
@if ($brand->image != null)
<div class="form-group row">
<label for="" class="col-sm-4 col-form-label">
Old Image
</label>
<div class="col-sm-8">
<img src="{{ asset($brand->image) }}" height="50" width="50" alt="">
</div>
</div>
@endif
<div class="form-group row">
<label for="" class="col-sm-4 col-form-label">
Image
</label>
<div class="col-sm-8">
<input type="file" class="form-control @error('image') is-invalid @enderror"
name="image" value="{{ $brand->image }}">
@error('image')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
</div>
<div class="row ">
<div class="col-sm-12 text-center">
<button type="submit" class=" text-center btn btn-success">Submit</button>
</div>
</div>
</form>
//
Post a Comment