Guide to Controller Repository Service Model in Laravel

The basic implementation guide for the Controller - Repository - Service model in Laravel helps you organize your source code in a way that is easy to manage and maintain. Here is a concrete example of how you can implement this structure:

Model

This is where you define the attributes and methods for interacting with the database. Laravel provides the Eloquent ORM mechanism to work with models. For example, let's create a model for the Posts table:

// app/Models/Post.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = ['title', 'content'];
}

Repository

The repository acts as an intermediary between the Controller and the Model. It contains methods to perform database operations through the model. This helps to separate the database logic from the controller and makes it easy to change or test the database logic.

// app/Repositories/PostRepository.php
namespace App\Repositories;

use App\Models\Post;

class PostRepository
{
    public function create($data)
    {
        return Post::create($data);
    }
    
    public function getAll()
    {
        return Post::all();
    }
    
    // Other similar methods
}

Service

The service contains business logic and communicates with the Repository. The Controller will call methods from the Service to handle requests and return corresponding data. This helps to separate the business logic from the controller and makes testing and maintenance easier.

// app/Services/PostService.php
namespace App\Services;

use App\Repositories\PostRepository;

class PostService
{
    protected $postRepository;
    
    public function __construct(PostRepository $postRepository)
    {
        $this->postRepository = $postRepository;
    }
    
    public function createPost($data)
    {
        return $this->postRepository->create($data);
    }
    
    public function getAllPosts()
    {
        return $this->postRepository->getAll();
    }
    
    // Other similar methods
}

Controller

The controller is where you handle user requests, call methods from the Service to retrieve or send data, and return results to the user.

// app/Http/Controllers/PostController.php
namespace App\Http\Controllers;

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

class PostController extends Controller
{
    protected $postService;
    
    public function __construct(PostService $postService)
    {
        $this->postService = $postService;
    }
    
    public function create(Request $request)
    {
        $data = $request->only(['title', 'content']);
        $post = $this->postService->createPost($data);
        // Handle the response
    }
    
    public function index()
    {
        $posts = $this->postService->getAllPosts();
        // Handle the response
    }
    
    // Other similar methods
}

By applying this structure, you can efficiently manage different parts of your Laravel application. Additionally, separating the business logic, storage logic, and communication between classes makes your codebase flexible, maintainable, and testable.