laravel-repository
provides the basic repository
class for laravel
model The package was made to provide more
More external methods, and more friendly editor prompts; layering the code, repository
is
responsible for external business logic processing, model
is only responsible for the definition
of the fields, attributes, query conditions, and return values of the data table. It does not
participate in specific logical operations, and does not serve the control layer.
- Solve the problem that
model
does not automatically handle extra fields when adding or modifying - Optimize chained calls for
model
queries, query directly using arrays - Automatically process corresponding associated data queries through query conditions and query fields
- Provides a more friendly editor prompt
- PHP >= 7.0.0
- Laravel >= 5.5.0
composer require littlebug/laravel-repository
Suppose you have users in your database, or you replace users with the table names in your database.
php artisan core:model --table=users --name=User
The command will be at:
- Generate
User
file underapp/Models/
file - Generate
UserRepository
file underapp/Repositories/
file
use App\Repositories\UserRepository;
class UsersController extends Controller
{
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function index()
{
// Paging query
$list = $this->userRepository->paginate([
'name:like' => 'test123',
'status:in' => [1, 2],
]);
return view('users.index');
}
public function create()
{
list($ok, $msg, $user) = $this->userRepository->create(request()->all());
// You are right logic
}
public function update()
{
list($ok, $msg, $row) = $this->userRepository->update(request()->input('id'), request()->all());
// You are right logic
}
public function delete()
{
list($ok, $msg, $row) = $this->userRepository->delete(request()->input('id'));
// You are right logic
}
}
Please check more about repository
Commands support specifying database connections such as --table=dev.users
-
core:model
generatesmodel
class files andrepository
class files by querying database table information.php artisan core:model --table=users --name=User
-
core:repository
generates therepository
class filephp artisan core:repository --model=User --name=UserRepository
-
core:request
generatesrequest
verification class file by querying database table informationphp artisan core:request --table=users --path=Users