“Laravel Hasmany” Code-Antworten

wie man wo Beziehung Laravel verwendet

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

Laravel eloquent ohne Beziehung

$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

wobei 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, wie man die Beziehung zwischen hingehören

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

Modell 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

Ähnliche Antworten wie “Laravel Hasmany”

Fragen ähnlich wie “Laravel Hasmany”

Weitere verwandte Antworten zu “Laravel Hasmany” auf PHP

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen