Laravel eloquent 关系用户 ID 问题
Laravel eloquent relationship user id issue
我目前有很多表,但为简单起见,我将展示两个受影响的表。它们包括:
- 用户
- 宣布
一个用户可以有多个公告,一个公告只能属于一个用户。这似乎是一个多对一的关系(或者我认为如此)。
数据库模式创建:
Schema::create('X_USER', function($table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
Schema::create('X_ANNOUNCE', function (Blueprint $table)
{
$table->increments('id');
$table->integer('created_by')->unsigned();
$table->foreign('created_by')->references('id')->on('X_USER')->onDelete('cascade');
$table->timestamps();
});
现在模型也很简单我在 User 模型上有这个:
public function announces()
{
return $this->hasMany('App\Announce', 'created_by');
}
而这个在公告型号:
public function user()
{
return $this->belongsTo('App\User', 'created_by');
}
当我插入一个公告时,我在 laravel AnnounceController.php(存储功能)上执行以下操作:
$announce = Announce::create($request->all());
return Response::json([
'message' => 'Announce Created Succesfully',
'data' => $this->transform($announce)
]);
前一行实际上正确地将所有内容插入了数据库。到目前为止没有错,直到我以我想传递给前端的格式执行 "transform" returns json:
private function transform($announce)
{
//Get User
$user = Announce::find($announce['created_by'])->user;
return [
'announce_id' => $announce['id'],
'announce_title' => $announce['title'],
'announce_user_id' => $user->id,
'announce_created_by' => $user->name,
];
}
当我执行 $user = Announce::find($announce['created_by'])->user
时,我从来没有得到正确的 ID ($user->id)。但是,$user->id 和 $user->name 是连贯的,只是不正确。我已经检查过 id ($announce['created_by']) 是否正确。
非常感谢任何帮助,我知道这个问题不是问得最好的问题,但在尝试了太多时间、检查了堆栈和教程之后,我有点发疯了。
当您使用 find
方法时,您是按 ID 搜索的,我认为正确的方法是:
$user = Announce::where('created_by', $announce['created_by'])->first()->user;
但实际上我认为这里没有多大意义,因为对于transform
方法来说,使用这样的代码就足够了:
此外,您应该能够将这样的代码用于 tranform
方法:
private function transform($announce)
{
return [
'announce_id' => $announce->id,
'announce_title' => $announce->title,
'announce_user_id' => $announce->user->id,
'announce_created_by' => $announce->user->name,
];
}
我目前有很多表,但为简单起见,我将展示两个受影响的表。它们包括:
- 用户
- 宣布
一个用户可以有多个公告,一个公告只能属于一个用户。这似乎是一个多对一的关系(或者我认为如此)。
数据库模式创建:
Schema::create('X_USER', function($table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
Schema::create('X_ANNOUNCE', function (Blueprint $table)
{
$table->increments('id');
$table->integer('created_by')->unsigned();
$table->foreign('created_by')->references('id')->on('X_USER')->onDelete('cascade');
$table->timestamps();
});
现在模型也很简单我在 User 模型上有这个:
public function announces()
{
return $this->hasMany('App\Announce', 'created_by');
}
而这个在公告型号:
public function user()
{
return $this->belongsTo('App\User', 'created_by');
}
当我插入一个公告时,我在 laravel AnnounceController.php(存储功能)上执行以下操作:
$announce = Announce::create($request->all());
return Response::json([
'message' => 'Announce Created Succesfully',
'data' => $this->transform($announce)
]);
前一行实际上正确地将所有内容插入了数据库。到目前为止没有错,直到我以我想传递给前端的格式执行 "transform" returns json:
private function transform($announce)
{
//Get User
$user = Announce::find($announce['created_by'])->user;
return [
'announce_id' => $announce['id'],
'announce_title' => $announce['title'],
'announce_user_id' => $user->id,
'announce_created_by' => $user->name,
];
}
当我执行 $user = Announce::find($announce['created_by'])->user
时,我从来没有得到正确的 ID ($user->id)。但是,$user->id 和 $user->name 是连贯的,只是不正确。我已经检查过 id ($announce['created_by']) 是否正确。
非常感谢任何帮助,我知道这个问题不是问得最好的问题,但在尝试了太多时间、检查了堆栈和教程之后,我有点发疯了。
当您使用 find
方法时,您是按 ID 搜索的,我认为正确的方法是:
$user = Announce::where('created_by', $announce['created_by'])->first()->user;
但实际上我认为这里没有多大意义,因为对于transform
方法来说,使用这样的代码就足够了:
此外,您应该能够将这样的代码用于 tranform
方法:
private function transform($announce)
{
return [
'announce_id' => $announce->id,
'announce_title' => $announce->title,
'announce_user_id' => $announce->user->id,
'announce_created_by' => $announce->user->name,
];
}