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:
- Simple, fast routing engine.
- Powerful dependency injection container.
- Multiple back-ends for session and cache storage.
- Expressive, intuitive database ORM.
- Database agnostic schema migrations.
- Robust background job processing.
- Real-time event broadcasting.
Laravel is accessible, powerful, and provides tools required for large, robust applications.
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.
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.
- Vehikl
- Tighten Co.
- Kirschbaum Development Group
- 64 Robots
- Cubet Techno Labs
- Cyber-Duck
- Many
- Webdock, Fast VPS Hosting
- DevSquad
- Curotec
- OP.GG
- WebReinvent
- Lendio
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
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.
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.
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
php artisan make:model Category -m
public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('slug'); $table->timestamps(); }); }
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',
];
}
Route::resource('categories', CategoryController::class);
-
First you need to create Repositories Folder inside app
-
Then you need to create Interfaces Folder and add CategoryRepositoryInterface.php
-
Now Create CategoryRepository.php inside Repositories
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 Name | Slug | Edit | Delete |
---|---|---|---|---|
{{$category->id}} | {{$category->name}} | {{$category->slug}} | Edit |