在 Eloquents "One to many" 关系中使用自定义方法名称
Using custom method names in Eloquents "One to many" relationship
我有 2 个 table 具有简单的 one to many
关系。
用户table:
id (primary key)
-- other columns ...
帖子 table:
id (primary key)
user_id
-- other columns ...
在用户模型中我有:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function postsbla() {
return $this->hasMany('App\Post');
}
}
在 Post 模型中我有:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function userbla() {
return $this->belongsTo( "App\User",'user_id', 'id' );
}
}
然后,我的控制器之一:
use App\User;
use App\Post;
public function index()
{
$a = true;
if($a) { // case 1
$user_id = 1;
$user_posts = User::find( $user_id )->postsbla;
var_dump($user_posts); exit;
}
else { // case 2
$post_id = 5;
$post_owner_user_data = Post::find( $post_id )->usersbla;
var_dump($post_owner_user_data); exit;
}
}
if $a === true
(case 1) then custom method name postsbla()
works and var_dump returns valid object, but if $a === false
, (case 2 ) 然后自定义方法名称 usersbla()
不起作用并且 returns NULL.
P.S。
如果我不使用 usersbla
名称,而是使用 user
,那么情况 2 也适用并提供有效对象。
请告诉我,为什么我可以在案例 1 中使用自定义方法名称,而不能在案例 2 中使用?
在您的情况下,您需要通过添加 foreignKey
来指定 belongsTo 方法的第二个参数,因为您的方法名称是 userbla
并且 laravel 表示:
Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id
.
return $this->belongsTo('App\User', 'user_id');
在 hasMany 的情况下
Eloquent will take the "snake case" name of the owning model and suffix it with _id.
我有 2 个 table 具有简单的 one to many
关系。
用户table:
id (primary key)
-- other columns ...
帖子 table:
id (primary key)
user_id
-- other columns ...
在用户模型中我有:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function postsbla() {
return $this->hasMany('App\Post');
}
}
在 Post 模型中我有:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function userbla() {
return $this->belongsTo( "App\User",'user_id', 'id' );
}
}
然后,我的控制器之一:
use App\User;
use App\Post;
public function index()
{
$a = true;
if($a) { // case 1
$user_id = 1;
$user_posts = User::find( $user_id )->postsbla;
var_dump($user_posts); exit;
}
else { // case 2
$post_id = 5;
$post_owner_user_data = Post::find( $post_id )->usersbla;
var_dump($post_owner_user_data); exit;
}
}
if $a === true
(case 1) then custom method name postsbla()
works and var_dump returns valid object, but if $a === false
, (case 2 ) 然后自定义方法名称 usersbla()
不起作用并且 returns NULL.
P.S。
如果我不使用 usersbla
名称,而是使用 user
,那么情况 2 也适用并提供有效对象。
请告诉我,为什么我可以在案例 1 中使用自定义方法名称,而不能在案例 2 中使用?
在您的情况下,您需要通过添加 foreignKey
来指定 belongsTo 方法的第二个参数,因为您的方法名称是 userbla
并且 laravel 表示:
Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with
_id
.
return $this->belongsTo('App\User', 'user_id');
在 hasMany 的情况下
Eloquent will take the "snake case" name of the owning model and suffix it with _id.