Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Illuminate\Database\Eloquent\Model;
  6.  
  7. class Product extends Model
  8. {
  9.     protected $fillable = ['name', 'description', 'picture', 'slug'];
  10.  
  11.     public function setSlugAttribute($value)
  12.     {
  13.         dd($value);
  14.         $this->attributes['slug'] = strtoupper($value);
  15.         // replace non letter or digits by -
  16.         $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  17.  
  18.         // trim
  19.         $text = trim($text, '-');
  20.  
  21.         // transliterate
  22.         $cyr  = ['а','б','в','г','д','е','ж','з','и','ѝ','й','к','л','м','н','о','п','р','с','т','у',
  23.             'ф','х','ц','ч','ш','щ','ъ','ь', 'ю','я','А','Б','В','Г','Д','Е','Ж',
  24.             'З','И','Ѝ','Й','К','Л','М','Н','О','П','Р','С','Т','У',
  25.             'Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ь', 'Ю', 'Я'];
  26.         $lat = ['a','b','v','g','d','e','zh','z','i','i','y','k','l','m','n','o','p','r','s','t','u',
  27.             'f' ,'h' ,'ts' ,'ch','sh' ,'sht' ,'a' ,'y' ,'yu' ,'ya','A','B','V','G','D','E','Zh',
  28.             'Z','I','I','Y','K','L','M','N','O','P','R','S','T','U',
  29.             'F' ,'H' ,'Ts' ,'Ch','Sh' ,'Sht' ,'A' ,'Y' ,'Yu' , 'Ya'];
  30.         $text = str_replace($cyr, $lat, $text);
  31.  
  32.         // lowercase
  33.         $text = strtolower($text);
  34.  
  35.         // remove unwanted characters
  36.         $text = preg_replace('~[^-\w]+~', '', $text);
  37.  
  38.         if (empty($text)) {
  39.             return 'n-a';
  40.         }
  41.  
  42.         return $text;
  43.  
  44.     }
  45.  
  46.  
  47.     public function shops()
  48.     {
  49.         return $this->belongsToMany(Shop::class);
  50.     }
  51.  
  52.     public function categories()
  53.     {
  54.         return $this->belongsToMany(Category::class);
  55.     }
  56.  
  57.     public function production()
  58.     {
  59.         return $this->hasMany(Production::class);
  60.     }
  61.  
  62.     public function houses()
  63.     {
  64.         return $this->belongsToMany(House::class);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement