1. Go to this page and download the library: Download duardaum/laravel-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/ */
duardaum / laravel-repository example snippets
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
//
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Message extends Model
{
use SoftDeletes;
protected $table = 'messages';
protected $fillable = [
'content',
'created_at',
'updated_at',
];
}
namespace App\Contracts\Repositories;
use Duardaum\LaravelRepository\Contracts\Repositories\BaseRepositoryInterface;
interface MessageRepositoryInterface extends BaseRepositoryInterface
{
}
namespace App\Repositories;
use Duardaum\LaravelRepository\Repositories\BaseRepository;
use App\Contracts\Repositories\MessageRepositoryInterface;
use App\Models\Message;
class MessageRepository extends BaseRepository implements MessageRepositoryInterface
{
protected string|\Illuminate\Database\Eloquent\Model $_model = Message::class;
}
// app/Providers/RepositoryServiceProvider.php
...
public function register()
{
$this->app->bind(\App\Contracts\Repositories\MessageRepositoryInterface::class, \App\Repositories\MessageRepository::class);
}
...
// 1. On constructor
use App\Contracts\Repositories\MessageRepositoryInterface;
class SomeClass {
public function __construct
(
private MessageRepositoryInterface $_repo_message
){}
}
// 2. With Service Container
$repo_message = app(\App\Contracts\Repositories\MessageRepositoryInterface::class);
//From trash (deactivated)
$data = $repo_message->onlyTrashed()->findWhere(...);
//The next line will keep on trashed context, so you don't need to put 'onlyTrashed' again:
$data2 = $repo_message->findWhere(...);
//If you want to change query context on next line, just call 'newQuery' or 'withTrashed' and the context will change:
$data3 = $repo_message->newQuery()->findWhere(...);
$data4 = $repo_message->findWhere(...); // now running on 'newQuery' context
// app/Repositories/MessageRepository.php
...
public function getAllDeactived(): \Illuminate\Database\Eloquent\Collection
{
return self::onlyTrashed()->all();
}
..
// app/Contracts/Repositories\MessageRepositoryInterface.php
...
public function getAllDeactived() : \Illuminate\Database\Eloquent\Collection;
...
// app/Repositories/MessageRepository.php
...
public function someGreatMethod(): \Illuminate\Database\Eloquent\Collection
{
return self::getCurrentQuery()->findWhere(...);
}
...
// app/Contracts/Repositories/MessageRepositoryInterface.php
...
public function someGreatMethod(): \Illuminate\Database\Eloquent\Collection;
...