Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel 5.2 Admin Controller
<?php
namespace App\Http\Controllers\Admin;
use App\Manufacturer;
use App\Category;
use App\Product;
use App\ProductImage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use App\Utils\Uploader;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
Cache::forget('products');
if ($request->has('q')) {
$name = $request->q;
// Find post where name LIKE search string
$products = Product::where('title', 'like', "%$name%")
->paginate(15);;
$request->session()->flash('status-search', "Вы искали <strong>{$name}</strong>");
return view('admin.products.index', compact('products', 'q'));
}
$products = Product::paginate(15);
return view('admin.products.index', [
'products' => $products
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$categories = Category::all(['id', 'title']);
$manufacturers = Manufacturer::all(['id', 'title']);
return view('admin.products.create', compact('categories', 'manufacturers'));
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @param Uploader $uploader
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request, Uploader $uploader)
{
$this->validate($request, [
'title' => 'required|unique:products|max:255',
'body' => 'required',
'price' => 'required|numeric',
'category_id' => 'required',
'quantity' => 'numeric',
'manufacturer_id' => 'required',
'weight' => 'required',
'image.*' => 'image',
'view' => 'max:255|string',
'application' => 'max:255|string'
]);
$slug = str_slug($request->title);
if ($request->file('image')) {
$files = $request->file('image');
}
$product = Product::create([
'title' => $request->title,
'body' => $request->body,
'slug' => $slug,
'weight' => $request->weight,
'price' => $request->price,
'quantity' => $request->quantity ? $request->quantity : 1,
'category_id' => $request->category_id,
'manufacturer_id' => $request->manufacturer_id,
'view' => $request->view,
'application' => $request->application,
'confirmed' => $request->confirmed,
'seo_title' => $request->seo_title,
'seo_description' => $request->seo_description,
'seo_keywords' => $request->seo_keywords,
]);
if (!empty($files) && count($files) > 0 && $files[0] != null) {
foreach($files as $file) {
$string = $slug.str_random(5);
$params = [
'file' => $file,
'filename' => $string,
];
$ext = $file->getClientOriginalExtension();
$uploader->upload($params, 'products');
ProductImage::create(['parent_id' => $product->id, 'width'=> '350', 'image' => $string.'_350_350.'.$ext]);
ProductImage::create(['parent_id' => $product->id, 'width'=> '1024', 'image' => $string.$ext]);
}
}
return redirect()->back();
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$product = Product::findOrFail($id);
$categories = Category::all(['id', 'title']);
$manufacturers = Manufacturer::all(['id', 'title']);
$images = ProductImage::where('parent_id', $id)
->where('width', '=', 350)
->get();
return view('admin.products.edit', compact('categories', 'manufacturers', 'product', 'images'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Uploader $uploader, $id)
{
$product = Product::findOrFail($id);
$this->validate($request, [
'title' => 'required|max:255|unique:products,title,'.$product->id,
'body' => 'required',
'price' => 'required|numeric',
'category_id' => 'required',
'quantity' => 'numeric'
]);
$slug = str_slug($request->title);
if ($request->file('image')) {
$files = $request->file('image');
}
if (!empty($files) && count($files) > 0 && $files[0] != null) {
foreach($files as $file) {
$string = $slug.str_random(5);
$params = [
'file' => $file,
'filename' => $string,
];
$ext = $file->getClientOriginalExtension();
$uploader->upload($params, 'products');
ProductImage::create(['parent_id' => $product->id, 'width'=> '350', 'image' => $string.'_350_350.'.$ext]);
ProductImage::create(['parent_id' => $product->id, 'width'=> '1024', 'image' => $string.$ext]);
}
}
$product->slug = $request->slug ? $slug : 0;
$product->weight = $request->weight;
$product->quantity = $request->quantity ? $request->quantity : 1;
$product->price = $request->price;
$product->title = $request->title;
$product->body = trim($request->body);
$product->category_id = $request->category_id;
$product->manufacturer_id = $request->manufacturer_id ? $request->manufacturer_id : 0;
$product->view = trim($request->view);
$product->application = trim($request->application);
$product->confirmed = $request->confirmed;
$product->seo_title = $request->seo_title;
$product->seo_description = trim($request->seo_description);
$product->seo_keywords = $request->seo_keywords;
$product->save();
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$deletedRows = ProductImage::where('parent_id', $id)->delete();
Product::destroy($id);
return redirect()->route('admin.products.index')->with('status_delete', 'Продукт удалн!');
}
public function changePrice(Request $request, $id)
{
$product = Product::findOrFail($id);
$product->price = $request->price;
$product->save();
return redirect()->route('admin.products.index')->with('status', 'Продукт сохранен!');
}
public function imageDelete($id, Uploader $uploader)
{
$image = ProductImage::findOrfail($id);
$image2 = ProductImage::findOrfail($id+1);
$file_name = $image->image;
$file_name2 = $image2->image;
File::delete(['images/products/'.$file_name, 'images/products/'.$file_name2]);
ProductImage::destroy([$id, $id+1]);
return redirect()->back();
}
public function imageSort(Request $request, $id)
{
$image = ProductImage::findOrfail($id);
$image2 = ProductImage::findOrfail($id+1);
$image->sort = $request->sort;
$image->save();
$image2->sort = $request->sort;
$image2->save();
return redirect()->back();
}
public function savePrices(Request $request)
{
//dd($request->input('prices'));
$prices = $request->input('prices');
foreach ($prices as $key){
Product::where('id', '=', $key['id'])->update(['price' => $key['price']]);
}
return redirect()->back();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.