Laravel hasMany relationship is used to create the relation between two tables. hasMany means create the relation one to Many. For example if a article have comments and we wanted to get all comments of the article then we can use hasMany relationship .
Database Relations are used to create the relations between the tables so that we can manage the transactions in a better way. Similar to database relations laravel itself provides relationships out of the box to create the relation between tables in the application layer.
Laravel relations do not create relations in database it creates relation in application layer and produces the output.
Laravel eloquent hasMany
accepts 3 parameters first as model name, second is optional foreign_key and third is optional local_key .
Here is the syntax
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Parent extends Model
{
public function child()
{
return $this->hasmany(child::class,'foreign_key','local_key');
}
}
In the above syntax we have used belongsTo
method with 3 parameters so here Child::class
is the name of Model which we want to relate, foreign_key
means column name of child table and local_key
means column name of own table.
Laravel do not create join query internally but it creates a separate query and then attach the data.
So we can use it as below:
Parent::with('child')->get();
This will return all the articles along with comments.
Let’s understand laravel hasMany in eloquent query with example
Use of hasMany to method in article comments table
Suppose we have two tables one is article and another one is comments so comments table contains comments of articles table so here is the schema of both tables and you can also read How to make database connection in Laravel 8 ?
Step 1 : Create article and comment model
Create two model in app\Models
folder, one is article and other is comment
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasFactory;
}
Comment Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use HasFactory;
}
Step 2 : Create hasMany relation in Article model
Now create hasMany relation in Article class so that we can fetch comment detail using article_id
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasFactory;
public function comments(){
return $this->hasMany(Comment::class);
}
}
In the above code we have created a method comments and used hasMany method to create the relation with Comment Model.
Laravel assumes here table name is comment
and foreign key is comments
table article_id
and local key is articles
table id
. If you have different columns name to map then you can change.
Step 3 : Use the relationship in controller
We have created the relation now we need to access the model and its relations so lets create a controller and import the necessary classes
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Article;
class ArticleController extends Controller
{
public function index(Request $request)
{
$article=Article::with("comments")->first();
dump($article);
dump($article->comments);
}
}
Step 4 : Create Route
Lastly create the route in web.php
Route::get('/articles',[ArticleController::class, 'index']);