[英]How to update hasMany relationship in Laravel 5.1
我在Laravel上是全新的,我已经实现了Laravel Auth
提供的User
表,并且我还为用户元数据创建了一个表,这是一个Key Value pare table
。
用户元表由以下代码创建:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UserMeta extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_meta', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->char('meta_key', 255);
$table->longText('meta_value')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('user_meta');
}
}
在我的User model
我有以下方法:
public function meta() {
return $this->hasMany('App\Models\UserMeta');
}
在我的UserMeta model
我有以下方法:
public function user() {
return $this->belongsTo('App\User');
}
到现在为止一切都很好。 因此,当我注册新用户时,我执行以下操作:
$user = User::create(
[
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt( $data['password'] ),
]
);
if ( $user ) {
$telephone_number = new UserMeta;
$telephone_number->user()->associate($user);
$telephone_number->meta_key = 'telephone_number';
$telephone_number->meta_value = $data['telephone_number'];
$telephone_number->save();
$company = new UserMeta;
$company->user()->associate($user);
$company->meta_key = 'company';
$company->meta_value = $data['company'];
$company->save();
$web_site = new UserMeta;
$web_site->user()->associate($user);
$web_site->meta_key = 'web_site';
$web_site->meta_value = $data['web_site'];
$web_site->save();
}
return $user;
我想这应该是一个更好的方式来执行相同的操作,但我不知道是什么是另一种方式:( :)
所以,上面的代码对我来说非常好,但现在的问题是值更新。 在这种情况下,如何更新用户配置文件时更新Meta Data
?
在我的UserControler
update
方法中,我执行以下操作:
$ user = User :: where('id','=',$ id) - > first();
$user->name = $request->input( 'name' );
$user->email = $request->input( 'email' );
$user->password = bcrypt( $request->input( 'password' ) );
$user->save();
我的$request->input();
具有以下额外字段,对应于元值telephone_number
, web_site
, company
。
那么,如何更新user_meta表中的元值?
首先,你是对的,你可以循环创建方法中的三个键:
// Loop through all the meta keys we're looking for
foreach(['telephone_number', 'web_site', 'company'] as $metaKey) {
$meta = new UserMeta;
$meta->meta_key = $metaKey;
$meta->meta_value = array_get($data, $metaKey);
$meta->save();
}
然后,在您的更新方法中
// Loop through all the meta keys we're looking for
foreach(['telephone_number', 'web_site', 'company'] as $metaKey) {
// Query for the meta model for the user and key
$meta = $user->meta()->where('meta_key', $metaKey)->firstOrFail();
$meta->meta_value = array_get($data, $metaKey);
$meta->save();
}
请注意firstOrFail()
以结束查询。 这只是我的严格要求。 如果你想添加一个元值,如果它不存在,那么你可以用它替换该行
// Query for the meta model for the user and key, or
// create a new one with that key
$meta = $user->meta()->where('meta_key', $metaKey)
->first() ?: new UserMeta(['meta_key' => $metaKey]);
这种方法效率更高,但更复杂(但也可能教授Eloquent的一个很酷的功能!)。
您可以先加载所有元键(请参阅懒惰的预先加载 )。
// load the meta relationship
$user->load('meta');
// Loop through all the meta keys we're looking for
foreach(['telephone_number', 'web_site', 'company'] as $metaKey) {
// Get the first item with a matching key from the loaded relationship
// Or, create a new meta for this key
$meta = $user->meta
->first(function($item) use ($metaKey) {
return $item->meta_key === $metaKey;
}) ?: new UserMeta(['meta_key' => $metaKey]);
$meta->meta_value = array_get($data, $metaKey);
$meta->save();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.