Updated 30 August 2022
In Bagisto we use a Repository pattern for interacting with the Database and before going forward to create the files we have to understand some concepts e.g – Contracts, Repository, and Model Proxies
Contracts are a set of interfaces that define the core services provided by the Laravel framework. For example, the Illuminate\Contracts\Mail\Mailer contract defines the methods needed for sending an e-mail.
Repositories create a bridge between models and controllers. Decouple the hard dependencies of models from the controllers. The model should not be responsible for communicating with or extracting data from the database
The concept of Model proxies has been introduced to override the functionality of the existing Model. It is a type of model inheritance without creating a new table in the database.
First, we have to create a Model in packages/Webkul/HelloWorld/src/Models as illustrated below
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace Webkul\HelloWorld\Models; use Illuminate\Database\Eloquent\Model; use Webkul\HelloWorld\Contracts\HelloWorld as HelloWorldContract; class HelloWorld extends Model implements HelloWorldContract { protected $fillable = []; } |
Now create an interface in a folder named Contract path packages/Webkul/HelloWorld/src/Contract and also implement the interface class in the above Model class
1 2 3 4 5 6 7 8 |
<?php namespace Webkul\HelloWorld\Contracts; interface HelloWorld { } |
In Bagisto we create a layer above the Model which we called Model Proxy. For this, we have to create a class where the Model exists which extends a class Konekt\Concord\Proxies\ModelProxy as illustrated below
1 2 3 4 5 6 7 8 9 10 |
<?php namespace Webkul\HelloWorld\Models; use Konekt\Concord\Proxies\ModelProxy; class HelloWorldProxy extends ModelProxy { } |
Now create a folder Repositories in which create a class HelloWorldRepository.php that extends Webkul\Core\Eloquent\Repository and create a method model() in the class that returns the path of the contract
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace Webkul\HelloWorld\Repositories; use Webkul\Core\Eloquent\Repository; class HelloWorldRepository extends Repository { /** * Specify Model class name * * @return mixed */ function model() { return 'Webkul/HelloWorld/Contracts/HelloWorld'; } } |
After creating the above files, Create a Service Provider as ModuleServiceProvider.php which extends the Konekt\Concord\BaseModuleServiceProvider as illustrated below
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace Webkul\HelloWorld\Providers; use Konekt\Concord\BaseModuleServiceProvider; class ModuleServiceProvider extends BaseModuleServiceProvider { protected $models = [ Webkul\HelloWorld\Models\HelloWorld::class, ]; } |
Now Register the Service Provider in the config/concord.php file
1 2 3 4 5 6 7 |
<?php return [ 'modules' => [ ACME\HelloWorld\Providers\ModuleServiceProvider::class ] ]; |
Now Run command
1 |
php artisan optimize |
We will consistently update blog posts, so don’t forget to keep a check on it regularly. Also for any queries or doubts reach out to us at [email protected]. You can also raise a ticket at our HelpDesk System.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.