“Laravel Hasmany” Réponses codées

Comment utiliser où la relation Laravel

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

Laravel éloquent sans relation

$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

WHAME SITE: 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 comment interroger la relation appartenance

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

Modèle 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

Réponses similaires à “Laravel Hasmany”

Questions similaires à “Laravel Hasmany”

Plus de réponses similaires à “Laravel Hasmany” dans PHP

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code