Eshop-Laravel/app/Product.php

113 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2019-10-18 09:25:37 +00:00
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cart;
use Session , Image , DB;
class Product extends Model
{
static public function getProducts($cat_url , &$data){
if ($category = Categorie::where('url','=',$cat_url)->first()) {
$category = $category ->toArray();
$data['title'] .= $category['title'] . ' Products';
$data['cat_url'] = $cat_url;
if ($products = Categorie::find($category['id'])->products) {
$data['products'] = $products->toArray();
}else {
//No Products Found
}
}else{
abort(404);
}
}
static public function addToCart($data){
if(!empty ($data['id']) && is_numeric($data['id'])){
// if(! Cart::get($data['id'])){
if($product = self::find($data['id'])){
$product = $product -> toArray();
Cart::add(date('H.i.s') , $product['title'] , $product['price'] , $data['quantity'], ['size' => $data['size'] ,'img' => $product['image'] ]);
Session::flash('sm' , $product['title'] . ' Added To Cart!');
}
// }else{
// Session::flash('sm-error' , 'Already In Cart');
// }
}
}
static public function save_new($request){
$img = self::loadImage($request);
//defult Image
$image_name = $img ? $img : 'noimage.png';
//save the pic in the DB
$product = new self();
$product ->categorie_id = $request['categorie_id'];
$product ->title = $request['title'];
$product ->url = $request['url'];
$product ->price = $request['price'];
$product ->stock = $request['stock'];
$product ->sale = $request['sale'];
$product ->body = $request['body'];
$product ->image = $image_name;
$product ->save();
Session::flash('sm' , 'Product As been Saved!');
}
static public function find_item($id){
$sql = 'SELECT * FROM products p WHERE p.id = ?';
$item = DB::select($sql,[$id] );
return $item;
}
static public function update_item($request , $id){
$image_name =self::loadImage($request);
//save the pic in the DB
$product = self::find($id);
$product ->categorie_id = $request['categorie_id'];
$product ->title = $request['title'];
$product ->url = $request['url'];
$product ->price = $request['price'];
$product ->stock = $request['stock'];
$product ->sale = $request['sale'];
$product ->body = $request['body'];
if ($image_name) {
$product ->image = $image_name;
}
$product ->save();
Session::flash('sm' , $request['title'].' Product As been Updated!');
}
static private function loadImage($request){
$image_name = '';
if ($request->hasFile('image') && $request->file('image')->isValid()) {
//take the user pic to our server
$file = $request->file('image');
$image_name = date('Y.m.d.H.i.s') . '-' . $file->getClientOriginalName();
$request->file('image')->move( public_path() . '/images' , $image_name);
//take the origin pic and make a new one by using Image Class to provent viruses
$img = Image::make(public_path() . '/images'.'/' . $image_name);
//resize the pic to 300 Width
$img->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});
//save the new pic and overWrite the pic
$img ->save(public_path() . '/images'.'/' . $image_name);
}
return $image_name;
}
}