[英]How to update hasMany relationship in Laravel 5.1
I am totally new on Laravel, and I have implement the User
table provided by Laravel Auth
, and also I have create a table for the user meta data that is a Key Value pare table
. 我在Laravel上是全新的,我已经实现了
Laravel Auth
提供的User
表,并且我还为用户元数据创建了一个表,这是一个Key Value pare table
。
The user meta table is created by the following code : 用户元表由以下代码创建:
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');
}
}
In my User model
I have the following method: 在我的
User model
我有以下方法:
public function meta() {
return $this->hasMany('App\Models\UserMeta');
}
and inside my UserMeta model
I have the following method: 在我的
UserMeta model
我有以下方法:
public function user() {
return $this->belongsTo('App\User');
}
Until now anything is fine. 到现在为止一切都很好。 So, when I register a new user I perform the following actions:
因此,当我注册新用户时,我执行以下操作:
$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;
I suppose that should be a better way to perform that same actions, but I don't know what is the other way :( :) 我想这应该是一个更好的方式来执行相同的操作,但我不知道是什么是另一种方式:( :)
So, the above code works very nice for me, but now the problem is with the value update. 所以,上面的代码对我来说非常好,但现在的问题是值更新。 In this case, how can I update the
Meta Data
when I update the user profile ? 在这种情况下,如何更新用户配置文件时更新
Meta Data
?
In my update
method of my UserControler
, I perform the following actions: 在我的
UserControler
update
方法中,我执行以下操作:
$user = User::where( 'id', '=', $id )->first(); $ user = User :: where('id','=',$ id) - > first();
$user->name = $request->input( 'name' );
$user->email = $request->input( 'email' );
$user->password = bcrypt( $request->input( 'password' ) );
$user->save();
My $request->input();
我的
$request->input();
has the following extra fields that corresponding to meta values telephone_number
, web_site
, company
. 具有以下额外字段,对应于元值
telephone_number
, web_site
, company
。
So, how can I update the meta values in the user_meta table ? 那么,如何更新user_meta表中的元值?
Firstly, you are right that you could loop through the three keys in your create method to: 首先,你是对的,你可以循环创建方法中的三个键:
// 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();
}
Then, in your update method 然后,在您的更新方法中
// 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();
}
Note the firstOrFail()
to end the query. 请注意
firstOrFail()
以结束查询。 This is just me being strict. 这只是我的严格要求。 If you wanted to add a meta value if it didn't exist, then you could replace that line with
如果你想添加一个元值,如果它不存在,那么你可以用它替换该行
// 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]);
This approach is a little more efficient, but a more complex (but also potentially teaches about a cool feature of Eloquent!). 这种方法效率更高,但更复杂(但也可能教授Eloquent的一个很酷的功能!)。
You could load in all of the meta keys first (see lazy eager loading ). 您可以先加载所有元键(请参阅懒惰的预先加载 )。
// 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.