PHP code example of touhidurabir / laravel-model-repository

1. Go to this page and download the library: Download touhidurabir/laravel-model-repository 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/ */

    

touhidurabir / laravel-model-repository example snippets


namespace App\Repositories;

use Touhidurabir\ModelRepository\BaseRepository;
use App\Models\User;

class UserRepository extends BaseRepository {

	/**
     * Constructor to bind model to repo
     *
     * @param  object<App\Models\User> $user
     * @return void
     */
    public function __construct(User $user) {

        $this->model = $user;

        $this->modelClass = get_class($user);
    }

}

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;

class UserController extends Controller {

    /**
     * The resource repository instance
     *
     * @var mixed<object{\App\Repositories\UserRepository}|null>
     */
    protected $userRepository;

	/**
     * create a new controller instance
     *
     * @param  \App\Repositories\UserRepository         $userRepository
     * @return void
     */
    public function __construct(UserRepository $userRepository) {

        $this->userRepository = $userRepository;
    }
}

namespace App\Http\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
...

$userRepository = new UserRepository(new User);

$userRepository = UserRepository::withModel(new User);

$this->userRepository->create([
    ...
]);

$primaryKeyValue = 10;

$this->userRepository->update([
    ...
], $primaryKeyValue);

$user; // the already retrived model record instance

$this->userRepository->update([
    ...
], $user);

$this->userRepository->find(1); // find the id(primary key) of 1
$this->userRepository->find([1,2,3]); // find the id(primary key) of 1,2 and 3

$this->userRepository->find(['email' => '[email protected]']);

$this->userRepository->find(1, ['profile']); // find the id(primary key) of 1
$this->userRepository->find([1,2,3], ['profile']); // find the id(primary key) of 1,2 and 3

$this->userRepository->find(1, ['profile'],  true); // find the id(primary key) of 1
$this->userRepository->find([1,2,3], [], true); // find the id(primary key) of 1,2 and 3

$this->userRepository->all();

$this->userRepository->delete(1);

$this->userRepository->delete($user); // delete the alredt retrived $user model instance
$this->userRepository->delete(1); // delete user id of 1
$this->userRepository->delete([1,2,3]); // delete user id of 1,2 and 3
$this->userRepository->delete(['email' => '[email protected]']); // delete user with email of [email protected]

$this->userRepository->forceDelete(1);

$this->userRepository->forceDelete($user); // delete the alredt retrived $user model instance
$this->userRepository->forceDelete(1); // delete user id of 1
$this->userRepository->forceDelete([1,2,3]); // delete user id of 1,2 and 3
$this->userRepository->forceDelete(['email' => '[email protected]']); // delete user with email of [email protected]

$this->userRepository->restore(1);

$this->userRepository->restore($user); // restore the already retrived $user model instance
$this->userRepository->restore(1); // restore user id of 1
$this->userRepository->restore([1,2,3]); // restore user id of 1,2 and 3test

$this->userRepository->getModel();

$this->userRepository->setModel(new User);
$this->userRepository->setModel($user);

/**
 * Sanitize data list to model fillables
 *
 * @param  array   $data
 * @return array
 */
public function sanitizeToModelFillable(array $data) {

    $classModel   = $this->model->getModel();
    $fillable     = $classModel->getFillable();

    $fillables = ! empty($fillable) 
                    ? $fillable 
                    : array_diff(
                        array_diff(
                            Schema::getColumnListing($classModel->getTable()), 
                            $classModel->getGuarded()
                        ), 
                        $classModel->getHidden()
                    );

    return array_intersect_key($data, array_flip($fillables));
}

$user = $this->userRepository->create([
    'name' => 'User Name',
    'email' => '[email protected]',
    'password' => Hash::make('password'),
    'date_of_birth' => '1990-12-08' // This date_of_birth column not present in users table
]);

$user = $this->userRepository->create($request->validated());

$profile = $this->profileRepository->create($request->validated());
bash
php artisan vendor:publish --provider="Touhidurabir\ModelRepository\ModelRepositoryServiceProvider" --tag=config
bash
php artisan make:repository UserRepository --model=User
bash
php artisan make:repository App\\SomeOtherPath\\UserRepository --model=App\\OtherModelPath\\User
bash
php artisan make:repository UserRepository --model=User --replace