PHP code example of m2quared / eloquent-depot

1. Go to this page and download the library: Download m2quared/eloquent-depot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

m2quared / eloquent-depot example snippets


'providers' => [
    ...
    M2quared\Repository\Providers\RepositoryServiceProvider::class,
],

$app->register(M2quared\Repository\Providers\LumenRepositoryServiceProvider::class);

namespace App;

class Post extends Eloquent { // or Ardent, Or any other Model Class

    protected $fillable = [
        'title',
        'author',
        ...
     ];

     ...
}

namespace App;

use M2quared\Repository\Eloquent\BaseRepository;

class PostRepository extends BaseRepository {

    /**
     * Specify Model class name
     *
     * @return string
     */
    function model()
    {
        return "App\\Post";
    }
}

namespace App\Http\Controllers;

use App\PostRepository;

class PostsController extends BaseController {

    /**
     * @var PostRepository
     */
    protected $repository;

    public function __construct(PostRepository $repository){
        $this->repository = $repository;
    }

    ....
}

$posts = $this->repository->all();

$posts = $this->repository->paginate($limit = null, $columns = ['*']);

$post = $this->repository->find($id);

$post = $this->repository->hidden(['country_id'])->find($id);

$post = $this->repository->visible(['id', 'state_id'])->find($id);

$post = $this->repository->with(['state'])->find($id);

$posts = $this->repository->findByField('country_id','15');

$posts = $this->repository->findWhere([
    //Default Condition =
    'state_id'=>'10',
    'country_id'=>'15',
    //Custom Condition
    ['columnName','>','10']
]);

$posts = $this->repository->findWhereIn('id', [1,2,3,4,5]);

$posts = $this->repository->findWhereNotIn('id', [6,7,8,9,10]);

$posts = $this->repository->scopeQuery(function($query){
    return $query->orderBy('sort_order','asc');
})->all();

$post = $this->repository->create( Input::all() );

$post = $this->repository->update( Input::all(), $id );

$this->repository->delete($id)
shell
php artisan vendor:publish