Laravel มีหลายความสัมพันธ์

รหัสตัวอย่าง

6
0

คนหลาย laravel

For example, a blog post may have an infinite number of comments. And a single
comment belongs to only a single post  

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }
}

class Comment extends Model
{
    public function post()
    {
        return $this->belongsTo('App\Models\Post');
    }
}
2
0

laravel นเป็นของ

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    /**
     * Get the user that owns the phone.
     */
    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }
}
0
0

laravel หลายจะมีความสัมพันธ์

/*
users
    id - integer
    name - string

roles
    id - integer
    name - string

role_user
    user_id - integer
    role_id - integer
*/

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function roles()
    {
      /*To determine the table name of the relationship's intermediate 
      table, Eloquent will join the two related model names in 
      alphabetical order. However, you are free to override this 
      convention. You may do so by passing a second argument to the 
      belongsToMany method*/
        return $this->belongsToMany(Role::class,'role_user');
    }
}
//Defining The Inverse Of The Relationship

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}
0
0

laravel ความสัมพันธ์ต้องมีหลาย

// Post model 
public function comments()
{
  return $this->hasMany(Comment::class);
}

// Post controller
$comments = Post::find(1)->comments;
0
0

laravel มีหลาย

<?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('App\Models\Comment');
    }
}
0
0

หลายที่มีความสัมพันธ์ laravel

use App\Models\User;

$user = User::find(1);

$user->roles()->attach($roleId);

หน้าคล้ายกัน

คล้ายกันหน้ากับตัวอย่าง

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................