@edmond_brakus
In Laravel, you can use Eloquent Relationships to join two different tables. Here is an example of how you can join two tables in Laravel:
User model:
1 2 3 |
public function posts() { return $this->hasMany('AppPost'); } |
Post model:
1 2 3 |
public function user() { return $this->belongsTo('AppUser'); } |
1
|
$users = User::with('posts')->get(); |
This will fetch all users along with their posts in a single query.
1 2 3 |
$posts = Post::join('users', 'users.id', '=', 'posts.user_id') ->select('users.name', 'posts.title') ->get(); |
This will return all posts with the user's name in a single query.
By using Eloquent Relationships or join queries, you can easily join two different tables in Laravel.