PHP code example of goodmanluphondo / laravel-service-repository-pattern

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

    

goodmanluphondo / laravel-service-repository-pattern example snippets


// config/app.php (Laravel 10 and older)
'providers' => [
    // Other providers...
    GoodmanLuphondo\LaravelServiceRepositoryPattern\Providers\ServiceRepositoryPatternServiceProvider::class,
],

// bootstrap/providers.php (Laravel 11+)


return [
    App\Providers\AppServiceProvider::class,
    GoodmanLuphondo\LaravelServiceRepositoryPattern\Providers\ServiceRepositoryPatternServiceProvider::class,
];

// config/app.php (Laravel 10 and older)
'providers' => [
    // Other providers...
    App\Providers\RepositoryServiceProvider::class,
],

// bootstrap/providers.php (Laravel 11+)


return [
    App\Providers\AppServiceProvider::class,
    App\Providers\RepositoryServiceProvider::class,
];



namespace App\Interfaces;

use App\Interfaces\BaseInterface;

interface PostRepositoryInterface extends BaseInterface
{
    //
}



namespace App\Repositories;

use App\Interfaces\PostRepositoryInterface;
use App\Models\Post;
use App\Repositories\Repository;

class PostRepository extends Repository implements PostRepositoryInterface
{
    public function __construct(Post $post)
    {
        parent::__construct($post);
    }
}



namespace App\Services;

use App\Interfaces\PostRepositoryInterface;

class PostService
{
    public function __construct(
        protected PostRepositoryInterface $postRepository,
    ) {}
}



namespace App\Http\Controllers;

use App\Services\PostService;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function __construct(
        protected PostService $postService
    ) {}

    public function index()
    {
        $posts = $this->postService->getAllPosts();
        return view('posts.index', compact('posts'));
    }

    public function store(Request $request)
    {
        $post = $this->postService->createPost($request->validated());
        return redirect()->route('posts.show', $post);
    }
}
bash
php artisan vendor:publish --tag=service-repository-pattern
bash
php artisan make:service User
bash
php artisan make:service Integrations\\Payment