Lecture: 58 Move Image

Posted in CategoryGeneral Discussion
  • Shaun Collins 3 months ago

    <?php

    namespace App\Controllers\Admin;


    useApp\Classes\CSRFToken;

    useApp\Classes\Redirect;

    useApp\Classes\Request;

    useApp\Classes\Session;

    useApp\Classes\UploadFile;

    useApp\Classes\ValidateRequest;

    useApp\Controllers\BaseController;

    useApp\Models\Category;

    useApp\Models\Product;

    useApp\Models\SubCategory;


    class ProductController extends BaseController

    {

    public$table_name='products';

    public$products;

    public$categories;

    public$subcategories;

    public$subcategories_links;

    public$links;

    publicfunction__construct()

    {

    $this->categories=Category::all();

    //$total = Product::all()->count();

    //list($this->products, $this->links) = paginate(10, $total, $this->table_name, new Product);

    }

    publicfunction showCreateProductForm()

    {

    $categories=$this->categories;

    return view('admin/products/create',compact('categories'));

    }

    publicfunction store()

    {

    if(Request::has('post')){

    $request=Request::get('post');

    $file_error=[];

    if(CSRFToken::verifyCSRFToken($request->token)){

    $rules=[

    'name'=>['required'=>true,'minLength'=>3,'maxLength'=>70,'mixed'=>true,'unique'=>$this->table_name],

    'price'=>['required'=>true,'minLength'=>2,'number'=>true],

    'quantity'=>['required'=>true],

    'category'=>['required'=>true],'subcategory'=>['required'=>true],

    'description'=>['required'=>true,'mixed'=>true,'minLength'=>4,'maxLength'=>500,]

    ];

    $validate=newValidateRequest;

    $validate->abide($_POST,$rules);

    $file=Request::get('file');

    isset($file->productImage->name)?$filename=$file->productImage->name:$filename='';

    if(empty($filename)){

    $file_error['productImage']=['The product image is required'];

    }elseif(!UploadFile::isImage($filename)){

    $file_error['productImage']=['The image is invalid, please try again.'];

    }

    if($validate->hasError()){

    $response=$validate->getErrorMessages();

    count($file_error)?$errors=array_merge($response,$file_error):$errors=$response;

    return view('admin/products/create',[

    'categories'=>$this->categories,'errors'=>$errors

    ]);

    }

    $ds=DIRECTORY_SEPARATOR;

    $temp_file=$file->productImage->tmp_name;

    $image_path=UploadFile::move($temp_file,"images{$ds}uploads{$ds}products",$filename)->path();

    //process form data

    Product::create([

    'name'=>$request->name,

    'description'=>$request->description,

    'price'=>$request->price,

    'category_id'=>$request->category,

    'sub_category_id'=>$request->subcategory,

    'image_path'=>$image_path,

    'quantity'=>$request->quantity,

    ]);

    Request::refresh();

    return view('admin/products/create',[

    'categories'=>$this->categories,'success'=>'Record Created'

    ]);

    }

    thrownew\Exception('Token mismatch');

    }

    returnnull;

    }

    publicfunction edit($id)

    {

    if(Request::has('post')){

    $request=Request::get('post');

    if(CSRFToken::verifyCSRFToken($request->token,false)){

    $rules=[

    'name'=>['required'=>true,'minLength'=>3,'string'=>true,'unique'=>'categories']

    ];

    $validate=newValidateRequest;

    $validate->abide($_POST,$rules);

    if($validate->hasError()){

    $errors=$validate->getErrorMessages();

    header('HTTP/1.1 422 Unprocessable Entity',true,422);

    echojson_encode($errors);

    exit;

    }

    Category::where('id',$id)->update(['name'=>$request->name]);

    echojson_encode(['success'=>'Record Update Successfully']);

    exit;

    }

    thrownew\Exception('Token mismatch');

    }

    returnnull;

    }

    publicfunction delete($id)

    {

    if(Request::has('post')){

    $request=Request::get('post');

    if(CSRFToken::verifyCSRFToken($request->token)){

    Category::destroy($id);

    $subcategories=SubCategory::where('category_id',$id)->get();

    if(count($subcategories)){

    foreach($subcategoriesas$subcategory){

    $subcategory->delete();

    }

    }

    Session::add('success','Category Deleted');

    Redirect::to('/admin/product/categories');

    }

    thrownew\Exception('Token mismatch');

    }

    returnnull;

    }

    publicfunction getSubcategories($id)

    {

    $subcategories=SubCategory::where('category_id',$id)->get();

    echojson_encode($subcategories);

    exit;

    }

    }



    <?php

    namespace App\Models;

    useIlluminate\Database\Eloquent\Model;

    useIlluminate\Database\Eloquent\SoftDeletes;

    useCarbon\Carbon;


    class Product extends Model

    {

    useSoftDeletes;

    public$timestamps=true;

    protected$fillable=[

    'name','price','description','category_id','sub_category_id',

    'image_path','quantity'

    ];

    protected$dates=['deleted_at'];

    publicfunction transform($data)

    {

    $products=[];

    foreach($dataas$item){

    $added=newCarbon($item->created_at);

    array_push($products,[

    'id'=>$item->id,

    'name'=>$item->name,

    'price'=>$item->price,

    'quantity'=>$item->quantity,

    'description'=>$item->description,

    'category_id'=>$item->category_id,

    'category_name'=>Category::where('id',$item->category_id)->first()->name,

    'sub_category_id'=>$item->sub_category_id,

    'sub_category_name'=>SubCategory::where('id',$item->sub_category_id)->first()->name,

    'image_path'=>$item->image_path,

    'added'=>$added->toFormattedDateString()

    ]);

    }

    return$products;

    }

    }



    @extends('admin.layout.base')

    @section('title', 'Create Product')

    @section('data-page-id', 'adminProduct')


    @section('content')

    <div class="add-product">

    <div class="row expanded">

    <div class="column medium-11">

    <h2>Add Inventory Item</h2> <hr />

    </div>

    </div>

    @include('includes.message')

    <form method="post"action="/admin/product/create"enctype="multipart/form-data">

    <div class="small-12 medium-11">

    <div class="row expanded">

    <div class="small-12 medium-6 column">

    <label>Product name:

    <input type="text"name="name"placeholder="Product name"

    value="{{\App\Classes\Request::old('post', 'name')}}">

    </label>

    </div>

    <div class="small-12 medium-6 column">

    <label>Product price:

    <input type="text"name="price"placeholder="Product price"

    value="{{\App\Classes\Request::old('post', 'price')}}">

    </label>

    </div>

    <div class="small-12 medium-6 column">

    <label>Product Category:

    <select name="category"id="product-category">

    <option value="{{\App\Classes\Request::old('post', 'category')?: ''}}">

    {{\App\Classes\Request::old('post', 'category')?:"Select Category"}}

    </option>

    @foreach($categories as $category)

    <option value="{{ $category->id }}"> {{ $category->name }}</option>

    @endforeach

    </select>

    </label>

    </div>

    <div class="small-12 medium-6 column">

    <label>Product Subcategory:

    <select name="subcategory"id="product-subcategory">

    <option value="{{\App\Classes\Request::old('post', 'subcategory')?: ''}}">

    {{\App\Classes\Request::old('post', 'subcategory')?:"Select Subcategory"}}

    </option>

    </select>

    </label>

    </div>

    <div class="small-12 medium-6 column">

    <label>Product Quantity:

    <select name="quantity">

    <option value="{{\App\Classes\Request::old('post', 'quantity')?: ''}}">

    {{\App\Classes\Request::old('post', 'quantity') ? : "Select quantity"}}

    </option>

    @for($i = 1; $i <= 50; $i++)

    <option value="{{ $i }}">{{ $i }}</option>

    @endfor

    </select>

    </label>

    </div>

    <div class="small-12 medium-6 column">

    <br />

    <div class="input-group">

    <span class="input-group-label">Product Image:</span>

    <input type=

  • Terry Ogbemudia 3 months ago
    Hi Collins, I think something is missing please share your code let me see
  • Shaun Collins 3 months ago
    When I hit the submit button without any fields filled in I do not get any warning messages, the page does not do anything until I hit refresh, then I get Token Mismatch. I have returned to the beginning of this section and gone through with intense scrutiny to make sure I miss nothing, same result. I then downloaded the create_product_model_save and exchanged the relevant files, same result. I feel like I am losing sanity Terry please help!

Please login or register to leave a response.