Answers for "laravel eloquent get model with relations"

PHP
0

laravel eloquent get model with relations

<?php
//For example, imagine a blog application in which a User model has many associated Post models:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    /**
     * Get all of the posts for the user.
     */
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

//You may query the posts relationship and add additional constraints to the relationship like so:
use App\Models\User;
$user = User::find(1);
$user->posts()->where('active', 1)->get();
Posted by: Guest on September-11-2021

Code answers related to "laravel eloquent get model with relations"

Browse Popular Code Answers by Language