“Laravel Hasmany” Respostas de código

Como usar onde o relacionamento Laravel

Event::with(["owner", "participants" => function($q) use($someId){
    $q->where('participants.IdUser', '=', 1);
    //$q->where('some other field', $someId);
}])
Alive Angelfish

Laravel eloquente sem relação

$books = Book::without('author')->get();
Real Raccoon

Laravel Hasmany

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
      return $this->hasMany(Comment::class);
      //return $this->hasMany(Comment::class, 'foreign_key');
      //return $this->hasMany(Comment::class, 'foreign_key', 'local_key');
    }
}

// Other
use App\Models\Post;
 
$comments = Post::find(1)->comments;
 
foreach ($comments as $comment) {
    //
}
// Other
$comment = Post::find(1)->comments()
                    ->where('title', 'foo')
                    ->first();

Shadow

Site de onde: https: //laravel.com/docs/

use Illuminate\Database\Eloquent\Builder;

// Retrieve posts with at least one comment containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
})->get();

// Retrieve posts with at least ten comments containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
}, '>=', 10)->get();
Tiago F2

Laravel como fazer uma consulta pertencente ao relacionamento

$movies = Movie::whereHas('director', function($q) {
    $q->where('name', 'great');
})->get();
Fragile Flatworm

Modelo Hasmany Laravel

class Mechanic extends Model
{
    /**
     * Get the car's owner.
     */
    public function carOwner()
    {
        return $this->hasOneThrough(
            Owner::class,
            Car::class,
            'mechanic_id', // Foreign key on the cars table...
            'car_id', // Foreign key on the owners table...
            'id', // Local key on the mechanics table...
            'id' // Local key on the cars table...
        );
    }
}
Sore Seal

Respostas semelhantes a “Laravel Hasmany”

Perguntas semelhantes a “Laravel Hasmany”

Mais respostas relacionadas para “Laravel Hasmany” em PHP

Procure respostas de código populares por idioma

Procurar outros idiomas de código