Laravel one to many eloquent relationship tutorial example
In this tutorial, today we discuss laravel one to many Eloquent relationships. Eloquent ORM means Object-relational Mapping and laravel provides a beautiful ActiveRecord structure. so we can easy to interact with the application database. let’s start about one to many eloquent relationships.
Laravel one to many relationships is based work on the one table row connected with multiple tables rows. For example, the category has multiple products.
Setting Database Configuration
After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
1 2 3 4 5 6 | DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(one_to_many_relation) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root) |
Create Table using migration
Now, We need to create a migration. so we will below command using create the categories and products table migration.
1 2 | php artisan make:migration create_categories_table --create=categories php artisan make:migration create_products_table --create=products |
After complete migration. we need below changes in the database/migrations/create_categories_table and database/migrations/create_products_table file.
.
create_categories_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCategoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categories'); } } ?> |
create_products_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('category_id')->unsigned(); $table->string('title'); $table->timestamps(); $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } } ?> |
Run the below command. after the changes above file.
1 | php artisan migrate |
Create Model
Here below command help through we will create the Category and Product model. we will also use “hasMany()” for a category model and “belongsTo()” for a product model.
1 2 | php artisan make:model Category php artisan make:model Product |
Category.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php namespace App; use Illuminate\Database\Eloquent\Model; class Category extends Model { protected $fillable = [ 'name' ]; /** * Get the products for the category. */ public function products() { return $this->hasMany('App\Product'); } } ?> |
Product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { /** * Get the category that owns the product. */ public function category() { return $this->belongsTo('App\Category'); } } ?> |
Route and Controller
We have to need put below code route in routes/web.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('product','productsController@index'); ?> |
Here below command help to create the product controller.
1 | php artisan make:controller ProductsController |
ProductsController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php namespace App\Http\Controllers; use App\Product; use App\Category; use Illuminate\Http\Request; class ProductsController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { //get Products data $product = Category::find(1)->products; dd($product); //get categories data $category = Product::find(1)->category; dd($category); } } ?> |