├── .gitignore ├── README.md ├── composer.json └── src ├── EloquentAbstractRepositoryServiceProvider.php ├── exceptions ├── InvalidDataProvidedException.php └── RepositoryException.php └── repository ├── RepositoryInterface.php └── eloquent └── AbstractEloquentRepository.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel 5 Eloquent Abstract repository 2 | 3 | using repository pattern in laravel with a great base repository 4 | 5 | 6 | ## Table of Contents 7 | 8 | - Installation 9 | - Composer 10 | - Service Provider 11 | - Methods 12 | - RepositoryInterface 13 | 14 | - Usage 15 | - Create a Model 16 | - Create a Repository 17 | - Create service Provider 18 | - Use methods 19 | 20 | 21 | ## Installation 22 | 23 | ### Composer 24 | 25 | Execute the following command to get the latest version of the package: 26 | 27 | ```terminal 28 | composer require ra3oul/eloquent-abstract-repository -dev 29 | ``` 30 | 31 | ### Laravel 32 | 33 | In your `config/app.php` add 34 | `ra3oul\EloquentAbstractRepository\EloquentAbstractRepositoryServiceProvider::class` to the end of the `providers` array: 35 | 36 | ```php 37 | 'providers' => [ 38 | ... 39 | 40 | ra3oul\EloquentAbstractRepository\EloquentAbstractRepositoryServiceProvider::class, 41 | ], 42 | ``` 43 | 44 | ## Methods 45 | 46 | ### ra3oul\EloquentAbstractRepository\repository\RepositoryInterface 47 | 48 | - create($columns = array('*')) 49 | - findOneById($id ) 50 | - findOneBy($key , $value ) 51 | - findManyBy($key,$value]) 52 | - findManyByIds($ids = array()) 53 | - findAll() 54 | - findManyByCredentials($credentials = array()) 55 | - paginateBy($key, $value, $perPage = 10) 56 | - paginate($perPage = 10) 57 | - paginateByCredentials(array $credentials, $perPage = 10) 58 | - updateOneById($id, array $data = []) 59 | - updateOneBy($key, $value, array $data = []) 60 | - updateOneByCredentials(array $credentials, array $data = []'); 61 | - updateManyBy($key, $value, array $data = []); 62 | - updateManyByCredentials(array $credentials = [], array $data = []); 63 | - updateManyByIds(array $ids, array $data = []); 64 | - function deleteOneById($id); 65 | - public function allExist(array $ids); 66 | - deleteOneBy($key, $value); 67 | - deleteOneByCredentials(array $credentials = []); 68 | - deleteManyBy($key, $value); 69 | - deleteManyByCredentials(array $credentials = []); 70 | - deleteManyByIds(array $ids); 71 | - searchByCredentials(array $credentials = [], $perPage); 72 | - with(array $with = []); 73 | - columns(array $columns = ['*']); 74 | - limit($limit = 10); 75 | - orderBy($orderBy, $sort = 'DESC'); 76 | 77 | ## Usage 78 | 79 | ### Create a Model 80 | 81 | Create your model normally, but it is important to define the attributes that can be filled from the input form data. 82 | 83 | ```php 84 | namespace App; 85 | 86 | class Article extends Eloquent { // can be any other class name 87 | protected $fillable = [ 88 | 'name', 89 | 'author', 90 | ... 91 | ]; 92 | 93 | ... 94 | } 95 | ``` 96 | ### Create a RepositoryInteface 97 | ```php 98 | namespace App; 99 | use Foo ; 100 | use ra3oul\EloquentAbstractRepository\repository\RepositoryInterface; 101 | 102 | interface ArticleRepositoryInterface extends RepositoryInterface 103 | { 104 | 105 | } 106 | 107 | ``` 108 | 109 | ### Create a Repository 110 | 111 | ```php 112 | namespace App; 113 | use Foo ; 114 | class ArticleRepository extends AbstractEloquentRepository implements ArticleRepositoryInterface 115 | 116 | 117 | public function __construct(Foo $model) 118 | { 119 | parent::__construct($model); 120 | } 121 | } 122 | ``` 123 | 124 | ### Create Service Provider 125 | in order to bind interfaces to repository classes we should create a simple service provider to bind them 126 | 127 | 128 | ```php 129 | namespace App\Providers; 130 | 131 | use Illuminate\Support\ServiceProvider; 132 | 133 | class RepositoryServiceProvider extends ServiceProvider 134 | { 135 | protected function registeredRepositories() 136 | { 137 | // 'Repository Interface' => 'Implementation' 138 | return [ 139 | '\App\ArticleRepositoryInterface' => 140 | '\App\ArticleRepository', 141 | // you should add other interfaces and their implemented classes below ! 142 | ]; 143 | } 144 | 145 | /** 146 | * Register the service provider. 147 | * 148 | * @return void 149 | */ 150 | public function register() 151 | { 152 | $repos = $this->registeredRepositories(); 153 | 154 | foreach ($repos as $interface => $implemented) { 155 | $this->app->bind($interface, $implemented); 156 | } 157 | } 158 | ``` 159 | 160 | 161 | 162 | ### Use methods 163 | 164 | ```php 165 | namespace App\Http\Controllers; 166 | 167 | use App\ArticleRepositoryInterface; 168 | 169 | class ArticlesController extends BaseController { 170 | 171 | /** 172 | * @var ArticleRepository 173 | */ 174 | protected $repository; 175 | 176 | public function __construct(FooRepositoryInterface $repository){ 177 | $this->repository = $repository; 178 | } 179 | 180 | .... 181 | } 182 | ``` 183 | 184 | Find all results in Repository 185 | 186 | ```php 187 | $articles = $this->repository->findAll(); 188 | ``` 189 | 190 | Find all results in Repository with pagination 191 | 192 | ```php 193 | $aritcles = $this->repository->columns([*])->paginate($limit=10); 194 | 195 | ``` 196 | 197 | Find by result by id 198 | 199 | ```php 200 | $articles = $this->repository->findOneById($id); 201 | ``` 202 | 203 | 204 | 205 | Showing only specific attributes of the model 206 | 207 | ```php 208 | $article = $this->repository->columns(['id', 'state_id'])->findOneById($id); 209 | ``` 210 | 211 | Loading the Model relationships 212 | 213 | ```php 214 | $article = $this->repository->with(['state'])->findOneById($id); 215 | ``` 216 | 217 | Find by result by field name 218 | 219 | ```php 220 | $articles = $this->repository->findOneBy('country_id','15'); 221 | ``` 222 | 223 | Find by result by field 224 | 225 | ```php 226 | 227 | $articles = $this->repository->findManyBy('name','rasoul'); 228 | ``` 229 | 230 | Find by result by multiple values in id 231 | 232 | ```php 233 | $articles = $this->repository->findManyByIds([1,2,3,4,5]); 234 | ``` 235 | 236 | Create new entry in Repository 237 | 238 | ```php 239 | $article = $this->repository->create( Input::all() ); 240 | ``` 241 | 242 | Update entry in Repository 243 | 244 | ```php 245 | $article = $this->repository->updateOneById( $id , Input::all()); 246 | ``` 247 | 248 | Delete entry in Repository 249 | 250 | ```php 251 | $this->repository->deleteOneById($id) 252 | ``` 253 | 254 | 255 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ra3oul/eloquent-abstract-repository", 3 | "description": "laravel eloquent abstract repository to implement repository pattern in easy way", 4 | "authors": [ 5 | { 6 | "name": "rasoul abdollahi", 7 | "email": "ra3oul.abdollahi@gmail.com" 8 | } 9 | ], 10 | "autoload": { 11 | "psr-4": { 12 | "ra3oul\\EloquentAbstractRepository\\": "src/" 13 | } 14 | }, 15 | "require": {}, 16 | "license":"MIT", 17 | "extra": { 18 | "branch-alias": { 19 | "dev-master": "1.0-dev" 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/EloquentAbstractRepositoryServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind('ra3oul\EloquentAbstractRepository\repository\RepositoryInterface', 28 | 'ra3oul\EloquentAbstractRepository\repository\eloquent\AbstractEloquentRepository'); 29 | 30 | 31 | } 32 | 33 | /** 34 | * 35 | */ 36 | public function boot() 37 | { 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/exceptions/InvalidDataProvidedException.php: -------------------------------------------------------------------------------- 1 | model = $model; 40 | $this->orderBy = $model->getKeyName(); 41 | } 42 | 43 | protected function buildCredentialsQuery(array $credentials) 44 | { 45 | $results = $this->makeQuery(); 46 | 47 | if (!empty($credentials)){ 48 | 49 | foreach ($credentials as $key => $_value) { 50 | $value = $_value; 51 | $operator = '='; 52 | 53 | if (is_array($_value)) { 54 | $value = $_value[0]; 55 | $operator = isset($_value[1]) ? $_value[1] : $operator; 56 | 57 | if (is_array($_value[0])) { 58 | foreach ($_value as $__value) { 59 | $value = $__value[0]; 60 | $operator = isset($__value[1]) ? $__value[1] : $operator; 61 | $hasAndOperator = isset($__value[2]) && (strtolower($__value[2]) != 'and') ? false : true; 62 | 63 | if ($hasAndOperator) { 64 | $results = $results->where($key, $operator, $value); 65 | } else { 66 | $results = $results->OrWhere($key, $operator, $value); 67 | } 68 | } 69 | } else { 70 | $results = $results->where($key, $operator, $value); 71 | } 72 | } else { 73 | $results = $results->where($key, $operator, $value); 74 | 75 | 76 | } 77 | 78 | } 79 | } 80 | 81 | return $results; 82 | } 83 | 84 | /** 85 | * @param array $with 86 | * @return $this 87 | * @throws RepositoryException 88 | */ 89 | public function with(array $with = []) 90 | { 91 | if (is_array($with) === false) { 92 | throw new RepositoryException(''); 93 | } 94 | 95 | $this->with = $with; 96 | 97 | return $this; 98 | } 99 | 100 | /** 101 | * @param array $columns 102 | * @return $this 103 | * @throws RepositoryException 104 | */ 105 | public function columns(array $columns = ['*']) 106 | { 107 | if (is_array($columns) === false) { 108 | throw new RepositoryException(''); 109 | } 110 | 111 | $this->columns = $columns; 112 | 113 | return $this; 114 | } 115 | 116 | /** 117 | * @param int $limit 118 | * @return $this 119 | * @throws RepositoryException 120 | */ 121 | public function limit($limit = 10) 122 | { 123 | if (!is_numeric($limit) || $limit < 1) { 124 | throw new RepositoryException('Limit Must be greater than 1'); 125 | } 126 | 127 | $this->limit = $limit; 128 | 129 | return $this; 130 | 131 | } 132 | 133 | /** 134 | * @param $id 135 | * @param $field 136 | */ 137 | public function inc($id, $field) 138 | { 139 | $this->model->findOrFail($id)->increment($field); 140 | } 141 | 142 | /** 143 | * @param $id 144 | * @param $field 145 | */ 146 | public function dec($id, $field) 147 | { 148 | $this->model->find($id)->decrement($field); 149 | } 150 | 151 | /** 152 | * @param int $offset 153 | * @return $this 154 | * @throws RepositoryException 155 | */ 156 | public function offset($offset = 0) 157 | { 158 | if (!is_numeric($offset) || $offset < 0) { 159 | throw new RepositoryException('Offset must be grater than or equal to ZERO'); 160 | } 161 | 162 | $this->offset = $offset; 163 | 164 | return $this; 165 | } 166 | 167 | 168 | /** 169 | * @param $orderBy 170 | * @param string $sort 171 | * @return $this 172 | * @throws RepositoryException 173 | */ 174 | public function orderBy($orderBy = null, $sort = 'DESC') 175 | { 176 | if ($orderBy === null) 177 | return $this; 178 | 179 | $this->orderBy = $orderBy; 180 | 181 | if (!in_array(strtoupper($sort), ['DESC', 'ASC'])) { 182 | throw new RepositoryException(''); 183 | } 184 | 185 | $this->sortMethod = $sort; 186 | 187 | return $this; 188 | } 189 | 190 | protected function makeQuery() 191 | { 192 | return $this->model; 193 | } 194 | 195 | /** 196 | * @param array $data 197 | * @return mixed 198 | */ 199 | public function create(array $data) 200 | { 201 | return $this->model->create($data); 202 | } 203 | 204 | /** 205 | * @param $id 206 | * @return mixed 207 | */ 208 | public function findOneById($id) 209 | { 210 | return $this->makeQuery() 211 | ->with($this->with) 212 | ->findOrFail($id, $this->columns); 213 | } 214 | 215 | /** 216 | * @param array $ids 217 | * @return mixed 218 | */ 219 | public function findManyByIds(array $ids) 220 | { 221 | return $this->makeQuery() 222 | ->with($this->with) 223 | ->whereIn($this->model->getKeyName(), $ids) 224 | ->take($this->limit) 225 | ->skip($this->offset) 226 | ->get($this->columns); 227 | } 228 | 229 | /** 230 | * @param $key 231 | * @param $value 232 | * @return mixed 233 | */ 234 | public function findOneBy($key, $value) 235 | { 236 | return $this->makeQuery() 237 | ->with($this->with) 238 | ->where($key, '=', $value) 239 | ->firstOrFail($this->columns); 240 | } 241 | 242 | /** 243 | * @param $key 244 | * @param $value 245 | * @return mixed 246 | */ 247 | public function findOneByNotRaiseException($key, $value) 248 | { 249 | return $this->makeQuery() 250 | ->with($this->with) 251 | ->where($key, '=', $value) 252 | ->first($this->columns); 253 | } 254 | 255 | 256 | /** 257 | * @param $key 258 | * @param $value 259 | * @return Collection 260 | */ 261 | public function findManyBy($key, $value) 262 | { 263 | return $this->makeQuery() 264 | ->with($this->with) 265 | ->where($key, '=', $value) 266 | ->orderBy($this->orderBy, $this->sortMethod) 267 | ->take($this->limit) 268 | ->skip($this->offset) 269 | ->get($this->columns); 270 | } 271 | 272 | /** 273 | * @return Collection 274 | */ 275 | public function findAll() 276 | { 277 | return $this->makeQuery() 278 | ->with($this->with) 279 | ->orderBy($this->orderBy, $this->sortMethod) 280 | ->get($this->columns); 281 | } 282 | 283 | /** 284 | * @return Collection 285 | */ 286 | public function findMany() 287 | { 288 | return $this->makeQuery() 289 | ->with($this->with) 290 | ->orderBy($this->orderBy, $this->sortMethod) 291 | ->take($this->limit) 292 | ->skip($this->offset) 293 | ->get($this->columns); 294 | } 295 | 296 | /** 297 | * @param array $credentials 298 | * @return Collection 299 | */ 300 | public function findManyByCredentials(array $credentials = []) 301 | { 302 | return $this->buildCredentialsQuery($credentials) 303 | ->with($this->with) 304 | ->orderBy($this->orderBy, $this->sortMethod) 305 | ->take($this->limit) 306 | ->skip($this->offset) 307 | ->get($this->columns); 308 | } 309 | 310 | /** 311 | * @param array $credentials 312 | * @return Collection | ModelNotFoundException 313 | */ 314 | public function findOneByCredentials(array $credentials = []) 315 | { 316 | return $this->buildCredentialsQuery($credentials) 317 | ->with($this->with) 318 | ->firstOrFail($this->columns); 319 | } 320 | 321 | /** 322 | * @param $key 323 | * @param $value 324 | * @param int $perPage 325 | * @return mixed 326 | */ 327 | public function paginateBy($key, $value, $perPage = 10) 328 | { 329 | return $this->makeQuery() 330 | ->with($this->with) 331 | ->where($key, '=', $value) 332 | ->orderBy($this->orderBy, $this->sortMethod) 333 | ->paginate($perPage, $this->columns); 334 | 335 | } 336 | 337 | /** 338 | * @param int $perPage 339 | * @return mixed 340 | */ 341 | public function paginate($perPage = 10) 342 | { 343 | 344 | return $this->makeQuery() 345 | ->with($this->with) 346 | ->orderBy($this->orderBy, $this->sortMethod) 347 | ->paginate($perPage, $this->columns); 348 | } 349 | 350 | /** 351 | * @param $query 352 | * @param int $perPage 353 | * @return mixed 354 | */ 355 | public function paginateQuery($query, $perPage = 10) 356 | { 357 | return $query->orderBy($this->orderBy, $this->sortMethod) 358 | ->paginate($perPage, $this->columns); 359 | } 360 | 361 | /** 362 | * @param $credentials 363 | * @param int $perPage 364 | * @return Paginator 365 | */ 366 | public function paginateByCredentials($credentials, $perPage = 10) 367 | { 368 | return $this->buildCredentialsQuery($credentials) 369 | ->with($this->with) 370 | ->orderBy($this->orderBy, $this->sortMethod) 371 | ->paginate($perPage, $this->columns); 372 | } 373 | 374 | /** 375 | * @param $id 376 | * @param array $data 377 | * @return bool 378 | * @throws InvalidDataProvidedException 379 | */ 380 | public function updateOneById($id, array $data = []) 381 | { 382 | if(!is_array($data) || empty($data)) 383 | throw new InvalidDataProvidedException; 384 | 385 | return $this->makeQuery() 386 | ->findOrFail($id) 387 | ->update($data); 388 | } 389 | 390 | /** 391 | * @param $key 392 | * @param $value 393 | * @param array $data 394 | * @return bool 395 | * @throws InvalidDataProvidedException 396 | */ 397 | public function updateOneBy($key, $value, array $data = []) 398 | { 399 | if(is_array($data) || empty($data)) 400 | throw new InvalidDataProvidedException; 401 | 402 | return $this->makeQuery() 403 | ->where($key, '=', $value) 404 | ->firstOrFail() 405 | ->update($data); 406 | } 407 | 408 | /** 409 | * @param array $credentials 410 | * @param array $data 411 | * @return bool 412 | * @throws InvalidDataProvidedException 413 | */ 414 | public function updateOneByCredentials(array $credentials, array $data = []) 415 | { 416 | if(is_array($data) || empty($data)) 417 | throw new InvalidDataProvidedException; 418 | 419 | return $this->buildCredentialsQuery($credentials) 420 | ->firstOrFail() 421 | ->update($data); 422 | } 423 | 424 | /** 425 | * @param $key 426 | * @param $value 427 | * @param array $data 428 | * @return bool 429 | * @throws InvalidDataProvidedException 430 | */ 431 | public function updateManyBy($key, $value, array $data = []) 432 | { 433 | if(is_array($data) || empty($data)) 434 | throw new InvalidDataProvidedException; 435 | 436 | return $this->makeQuery() 437 | ->where($key, $value) 438 | ->take($this->limit) 439 | ->skip($this->offset) 440 | ->update($data); 441 | } 442 | 443 | 444 | /** 445 | * @param array $ids 446 | * @param array $data 447 | * @return bool 448 | * @throws InvalidDataProvidedException 449 | */ 450 | public function updateManyByIds(array $ids, array $data = []) 451 | { 452 | if(!is_array($data) || empty($data)) 453 | throw new InvalidDataProvidedException; 454 | 455 | return $this->makeQuery() 456 | ->whereIn('id', $ids) 457 | ->update($data); 458 | } 459 | 460 | /** 461 | * @param array $ids 462 | * @return bool 463 | */ 464 | public function allExist(array $ids) 465 | { 466 | return (count($ids) == $this->makeQuery()->whereIn('id',$ids)->count()); 467 | } 468 | 469 | 470 | /** 471 | * @param array $credentials 472 | * @param array $data 473 | * @return boolean 474 | */ 475 | public function updateManyByCredentials(array $credentials = [], array $data = []) 476 | { 477 | return $this->buildCredentialsQuery($credentials)->update($data); 478 | } 479 | 480 | /** 481 | * @param array $credentials 482 | * @param array $data 483 | * @return mixed 484 | */ 485 | public function updateAllByCredentials(array $credentials = [], array $data = []) 486 | { 487 | return $this->buildCredentialsQuery($credentials) 488 | ->update($data); 489 | } 490 | 491 | /** 492 | * @param $id 493 | * @return boolean 494 | */ 495 | public function deleteOneById($id) 496 | { 497 | return $this->makeQuery() 498 | ->findOrFail($id) 499 | ->delete(); 500 | } 501 | 502 | /** 503 | * @param $key 504 | * @param $value 505 | * @return boolean 506 | */ 507 | public function deleteOneBy($key, $value) 508 | { 509 | return $this->makeQuery() 510 | ->where($key, '=', $value) 511 | ->firstOrFail() 512 | ->delete(); 513 | } 514 | 515 | /** 516 | * @param array $credentials 517 | * @return boolean 518 | */ 519 | public function deleteOneByCredentials(array $credentials = []) 520 | { 521 | return $this->buildCredentialsQuery($credentials) 522 | ->firstOrFail() 523 | ->delete(); 524 | } 525 | 526 | /** 527 | * @param $key 528 | * @param $value 529 | * @return boolean 530 | */ 531 | public function deleteManyBy($key, $value) 532 | { 533 | return $this->makeQuery() 534 | ->where($key, $value) 535 | ->take($this->limit) 536 | ->skip($this->offset) 537 | ->delete(); 538 | } 539 | 540 | /** 541 | * @param array $credentials 542 | * @return boolean 543 | */ 544 | public function deleteManyByCredentials(array $credentials = []) 545 | { 546 | return $this->buildCredentialsQuery($credentials) 547 | ->take($this->limit) 548 | ->skip($this->offset) 549 | ->delete(); 550 | } 551 | 552 | /** 553 | * @return mixed 554 | */ 555 | public function deleteMany() 556 | { 557 | return $this->makeQuery() 558 | ->take($this->limit) 559 | ->delete(); 560 | } 561 | 562 | 563 | /** 564 | * @param array $ids 565 | * @return mixed 566 | */ 567 | public function deleteManyByIds(array $ids) 568 | { 569 | return $this->makeQuery() 570 | ->whereIn('id', $ids) 571 | ->delete(); 572 | } 573 | 574 | /** 575 | * @return bool|null 576 | * @throws \Exception 577 | */ 578 | public function deleteAll() 579 | { 580 | return $this->makeQuery() 581 | ->delete(); 582 | } 583 | 584 | /** 585 | * @param $credentials 586 | * @return bool|null 587 | * @throws \Exception 588 | */ 589 | public function deleteAllByCredentials($credentials) 590 | { 591 | return $this->buildCredentialsQuery($credentials) 592 | ->delete(); 593 | } 594 | 595 | 596 | /** 597 | * @param array $credentials 598 | * @param int $perPage 599 | * 600 | * 601 | * samples : 602 | * categories:in_relation:we|wed|wef|edf 603 | * mobile:in:123|23|23|234 604 | * name:=:majid 605 | * age:<:20 606 | * age:>:10 607 | * family:like:%asghar% 608 | * 609 | * @return mixed 610 | * @throws RepositoryException 611 | */ 612 | public function searchByCredentials(array $credentials = [], $perPage = 10) 613 | { 614 | $items = []; 615 | 616 | foreach ($credentials as $key => $value) { 617 | 618 | if (!is_array($value)) { 619 | throw new RepositoryException(); 620 | } 621 | $items[] = ['key' => $key, 'operator' => $value[1], 'value' => $value[0]]; 622 | 623 | } 624 | $result = $this->model; 625 | foreach ($items as $item) { 626 | 627 | if (count($relation = explode('.', $item['key'])) === 2) { 628 | $result = $result->whereHas($relation[0], function ($query) use ($relation, $item) { 629 | $query->where($relation[1], $item['operator'], $item['value']); 630 | }); 631 | }elseif ($item['operator'] == 'in'){ 632 | $result = $result->whereIn($item['key'],explode('|', $item['value']) ); 633 | }elseif ($item['operator'] == 'in_relation'){ 634 | $result = $result->whereHas($item['key'], function($q) use ($item) { 635 | $q->whereIn(str_singular($item['key']).'_id', explode('|', $item['value'])); 636 | }); 637 | } 638 | 639 | else { 640 | $result = $result->Where($item['key'], $item['operator'], $item['value']); 641 | } 642 | } 643 | 644 | return $result->with($this->with)->orderBy($this->orderBy, $this->sortMethod)->paginate($perPage, $this->columns); 645 | 646 | } 647 | 648 | /** 649 | * @param array $data 650 | * @return \Illuminate\Database\Eloquent\Model 651 | */ 652 | public function add(array $data) 653 | { 654 | $item = $this->model; 655 | foreach($data as $k=>$v) 656 | $item->{$k} = $v; 657 | 658 | if(array_key_exists('slug', $data)) 659 | $item->slug = slugify(array_key_exists('name',$data)? $data['name']:$data['title']); 660 | 661 | $item->save(); 662 | 663 | return $item; 664 | } 665 | 666 | 667 | 668 | 669 | } 670 | --------------------------------------------------------------------------------