@deron
To create a relationship between two tables in Laravel, you can use Eloquent models and define relationships between them. Here's how you can do it:
1
|
php artisan make:model TableName |
Replace TableName
with the name of your table.
In the User
model:
1 2 3 4 |
public function posts() { return $this->hasMany(Post::class); } |
In the Post
model:
1 2 3 4 |
public function user() { return $this->belongsTo(User::class); } |
1 2 |
$user = User::find(1); $posts = $user->posts; |
By defining relationships between Eloquent models in Laravel, you can easily access related data and use it in your application.