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);
$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