/laravel-repository-app

Laravel 9 Repository Design Pattern CRUD Example

Primary LanguageJavaScript

Build Status Total Downloads Latest Stable Version License

About Laravel

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:

Laravel is accessible, powerful, and provides tools required for large, robust applications.

Learning Laravel

Laravel has the most extensive and thorough documentation and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.

If you don't feel like reading, Laracasts can help. Laracasts contains over 1500 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.

Laravel Sponsors

We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel Patreon page.

Premium Partners

Contributing

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

Security Vulnerabilities

If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via taylor@laravel.com. All security vulnerabilities will be promptly addressed.

License

reference

Laravel 9 Repository Design Pattern CRUD Example

by larainfo.com and mishuk

In this tutorial, we'll see how to use repository design pattern in laravel 9. We'll create a simple crud application using laravel 9 repository design pattern. If you're building a complex laravel application, then you should use repository design pattern and for small and low maintenance app, don't use repository design pattern.

follow for more info

https://larainfo.com/blogs/laravel-9-repository-design-pattern-crud-example

The Laravel framework is open-sourced software licensed under the MIT license.

##Step 1: Install Laravel & Connect Database

composer create-project laravel/laravel laravel-repository

Now, you need to set up the Laravel app to connect to the database, thus open up.env and add the database credentials as given below.

in .env

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database_name DB_USERNAME=database_user_name DB_PASSWORD=database_password

Step 2: Create Category Modal Migration and Controller

php artisan make:model Category -m

create_categories_table.php

public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('slug'); $table->timestamps(); }); }

Create Category Controller

php artisan make:controller CategoryController -r

app/Models/Category.php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;

class Category extends Model { use HasFactory;

protected $fillable = [
    'name',
    'slug',
];

}

Create routes web.PHP

Route::resource('categories', CategoryController::class);

Step 3: Create the Repository and Interface Folder Structure

  1. First you need to create Repositories Folder inside app

  2. Then you need to create Interfaces Folder and add CategoryRepositoryInterface.php

  3. Now Create CategoryRepository.php inside Repositories

Step 4: Add CRUD Functionality Repository and Interface

Add CRUD Functions in CategoryRepositoryInterface.php Interface

App/Repositories/Interfaces/CategoryRepositoryInterface.php

paginate(10); } public function storeCategory($data) { return Category::create($data); } public function findCategory($id) { return Category::find($id); } public function updateCategory($data, $id) { $category = Category::where('id', $id)->first(); $category->name = $data['name']; $category->slug = $data['slug']; $category->save(); } public function destroyCategory($id) { $category = Category::find($id); $category->delete(); } } ## Step 5: Bind Repository In App Service Provider Next, you need to add to bind CategoryRepositoryInterface and CategoryRepository in app/Providers/AppServiceProvider.php app/Providers/AppServiceProvider.php app->bind(CategoryRepositoryInterface::class, CategoryRepository::class); } /** * Bootstrap any application services. * * @return void */ public function boot() { // } } ## Step 6: Add Repository Design Pattern In CategoryController Add CategoryRepositoryInterface in CategoryController categoryRepository = $categoryRepository; } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $categories = $this->categoryRepository->allCategories(); return view('categories.index', compact('categories')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('categories.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $data = $request->validate([ 'name' => 'required|string|max:255', 'slug' => 'required|string|max:255', ]); $this->categoryRepository->storeCategory($data); return redirect()->route('categories.index')->with('message', 'Category Created Successfully'); } /** * Display the specified resource. * * @param \App\Models\Category $category * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param \App\Models\Category $category * @return \Illuminate\Http\Response */ public function edit($id) { $category = $this->categoryRepository->findCategory($id); return view('categories.edit', compact('category')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Models\Category $category * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $request->validate([ 'name' => 'required|string|max:255', 'slug' => 'required|string|max:255', ]); $this->categoryRepository->updateCategory($request->all(), $id); return redirect()->route('categories.index')->with('message', 'Category Updated Successfully'); } /** * Remove the specified resource from storage. * * @param \App\Models\Category $category * @return \Illuminate\Http\Response */ public function destroy($id) { $this->categoryRepository->destroyCategory($id); return redirect()->route('categories.index')->with('status', 'Category Delete Successfully'); } } ## Step 7: Create Blade View Files Now perform crud operations. resources/views/categories/create.blade.php

{{ __('Category Create') }}

@csrf
Category Name @error('name')
{{ $message }}
@enderror
Slug @error('slug')
{{ $message }}
@enderror
Submit
resources/views/categories/index.blade.php

{{ __('Category') }}

@foreach ($categories as $category) @endforeach
# Category Name Slug Edit Delete
{{$category->id}} {{$category->name}} {{$category->slug}} Edit
https://larainfo.com/storage/canvas/images/OctxYnEIcw7bIYVd1hqyszraz8To5GEFHqGg3GNr.png resources/views/categories/edit.blade.php

{{ __('Category Create') }}

@csrf @method('put')
Category Name @error('name')
{{ $message }}
@enderror
Slug @error('slug')
{{ $message }}
@enderror
Submit
## Step 8: Run Laravel and vite server php artisan serve //and next terminal npm run dev ## end ## tags #Laravel9 ## api repositories pattern for laravel server https://blog.devgenius.io/laravel-api-repository-pattern-make-your-code-more-structured-the-simple-guide-5b770da766d7 https://medium.com/@shadi.hariri68/resource-summary-rest-api-with-laravel-using-repository-b7ffcbc8ef1