Now I will create one to many relationships with each other by using the Laravel Eloquent Model. We can use the hasMany() method to define one to many relationships and the first argument passed to the hasMany method is the name of the related model class. In this example, I will create a posts table and a comments table. Suppose, A post may have many comments and a comment may be related to one post. Let's see how we can define Laravel's one to many relationship. In this Laravel 9 one to many relationship example tutorial, I am going to use the Post and Comments concept. Laravel provides many relationships and in this tutorial, I will show you how we handle one to many or we can call this hasMany eloquent relationship. Laravel eloquent makes managing and working with these relationships very convenient. For example, a post may have many comments or an order could be related to the user who placed it. resources/views/ a relational database management system, we know database tables are related to one another. This is also and important part while your displaying recursive categories list, we are going to need two views here first is the one which display the first level parent categories and second view is the one which is going to list out all the child level categories.Ĭreate new file called under views folder Return view('categories', compact('categories')) ĭefine route to get categories under web.php file: Route::get('/get-categories', Create Parent View and Child View: Many-to-many relations are slightly more complicated than hasOne and hasMany relationships. app/Http/Controllers/CategoriesController.php with('childCategories') Php artisan make:controller CategoriesControllerĭefine getCategories method in CategoriesController, this is the method where we are going to fetch all the categories with parent child relationship. We will this control to fetch categories from the database using our Categories eloquent model and pass it to the laravel blade file Next’s lets see how we can show this relationship using Laravel Blade View Create new Controller and Define Route It is going to give us all items with recursive relationship. It will returns us only one level of child items and here is the magic if you call Categories::with('childCategories')->get() In Laravel, Eloquent ORM’s has-many relationship is a game-changer for managing one-to-many associations in your database. If we try to get records like this Categories::with('categories')->get() Return $this->hasMany(Categories::class, 'category_id')->with('categories') This is method where we implement recursive relationship Open Categories model from app folder and define following methods: hasMany(Categories::class, 'category_id') This is important part of this tutorial and this is the main reason of writing this tutorial because it is the actual solution to have Laravel HasMany Recursive Relationship in your project. Migrated: 2019_09_09_164026_create_categories_table (0.07 seconds)Ĭategories table with parent child items Define Eloquent model Relationship Laravel6 git:(master) ✗ php artisan migrate $table->unsignedBigInteger('category_id')->nullable() Create new database model and a migration file which we use to define our categories table php artisan make:model Categories -mĪdd following columns for categories table bigIncrements('id')
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |