Answers for "get eloquent model"

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
1

laravel model::query

// Eloquent's Model::query() returns the query builder

Model::where()->get();
// Is the same as 
Model::query()->where()->get();

Model::query();
// Can be useful to add query conditions based on certain requirements

// See https://stackoverflow.com/questions/51517203/what-is-the-meaning-of-eloquents-modelquery
Posted by: Guest on October-06-2020

Browse Popular Code Answers by Language