Guest User

Untitled

a guest
Nov 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. <?php
  2. class Image extends Eloquent {
  3.  
  4. public function products() {
  5. return $this->belongs_to('Product');
  6. }
  7.  
  8. public static function input($product_id, $field = 'image', $directory='public/pictures')
  9. {
  10. $rules = array( $field => 'image' );
  11. $validator = Validator::make( Input::file(), $rules );
  12. if ($validator->fails())
  13. {
  14. return $validator->errors;
  15. }
  16. $file = Input::file( $field );
  17. $ext = pathinfo( $file['name'], PATHINFO_EXTENSION );
  18. $name = md5($file['name'] ) . '.' . $ext;
  19.  
  20. $directory .= "/{$product_id}";
  21.  
  22. Input::upload($field, $directory, $name);
  23. $image = new static;
  24. $image->product_id = $product_id;
  25. $image->filename = $name;
  26. $image->directory = $directory;
  27. return $image;
  28. }
  29.  
  30. public function set_temporary( $temp )
  31. {
  32. $this->set_attribute('temp', (int) (bool) $temp );
  33. }
  34. }
Add Comment
Please, Sign In to add comment