Laravel 5.1 eloquent 关系模型-如何更新外键列?
Laravel 5.1 eloquent relations model- how to update foreign key column?
我是 laravel 的初学者。以前从未使用过框架。我创建了一个名为 'tabletest' 的数据库。它有两个 table。一个是用户 table,另一个是 phone table.user table 有两列(id 和 name)。 phone table 有 3 列(id phone 和 user_id)。我正在尝试的是,我将使用表单获取输入并将输入发送到数据库 tables。尽管名称和 phone 正确保存在不同的 table 中,但 user_id(外键列)未更新。一直是0。我现在该怎么办?
迁移文件是:
用户table:
public function up()
{
Schema::create('user', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
phone table :
public function up()
{
Schema::create('phone', function (Blueprint $table) {
$table->increments('id');
$table->string('phone');
$table->unsignedInteger('user_id');
$table->timestamps();
});
}
用户模型:
use App\Model\Phone;
class User extends Model
{
protected $table = "user";
protected $fillable = ['name'];
public function phone(){
return $this->hasOne(Phone::class);
}
}
Phone 型号:
use App\Model\User;
class Phone extends Model
{
protected $table = "phone";
protected $fillable = ['phone','user_id'];
public function user(){
return $this->belongsTo(User::class);
}
}
PhoneController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\model\User;
use App\model\Phone;
class PhoneController extends Controller
{
public function store(Request $request)
{
User::create([
'name' => $request->name
]);
Phone::create([
'phone' => $request->phone
]);
}
}
这里是 phone table 的截图:
您永远不会指定要为哪个用户创建 phone 号码。即使您有一个外键,它也只会索引和约束 user_id 列。 MySQL 无法 "read" 您的 php 代码并假设您正在为哪个用户创建 phone 号码。
有几个选项。
你可以这样做:
$user = User::create([
'name' => $request->input('name')
]);
Phone::create([
'phone' => $request->phone,
'user_id' => $user->id
]);
或者另一种方式是:
$user = User::create([
'name' => $request->input('name')
]);
$user->phone()->create([ // this uses the create method on the users phone relationship
'phone' => 999999999,
]);
不确定是否也可以改变它,但为了可读性,我不推荐它(即 User::create([])->phone()->create([]);
)
我是 laravel 的初学者。以前从未使用过框架。我创建了一个名为 'tabletest' 的数据库。它有两个 table。一个是用户 table,另一个是 phone table.user table 有两列(id 和 name)。 phone table 有 3 列(id phone 和 user_id)。我正在尝试的是,我将使用表单获取输入并将输入发送到数据库 tables。尽管名称和 phone 正确保存在不同的 table 中,但 user_id(外键列)未更新。一直是0。我现在该怎么办?
迁移文件是:
用户table:
public function up()
{
Schema::create('user', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
phone table :
public function up()
{
Schema::create('phone', function (Blueprint $table) {
$table->increments('id');
$table->string('phone');
$table->unsignedInteger('user_id');
$table->timestamps();
});
}
用户模型:
use App\Model\Phone;
class User extends Model
{
protected $table = "user";
protected $fillable = ['name'];
public function phone(){
return $this->hasOne(Phone::class);
}
}
Phone 型号:
use App\Model\User;
class Phone extends Model
{
protected $table = "phone";
protected $fillable = ['phone','user_id'];
public function user(){
return $this->belongsTo(User::class);
}
}
PhoneController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\model\User;
use App\model\Phone;
class PhoneController extends Controller
{
public function store(Request $request)
{
User::create([
'name' => $request->name
]);
Phone::create([
'phone' => $request->phone
]);
}
}
这里是 phone table 的截图:
您永远不会指定要为哪个用户创建 phone 号码。即使您有一个外键,它也只会索引和约束 user_id 列。 MySQL 无法 "read" 您的 php 代码并假设您正在为哪个用户创建 phone 号码。
有几个选项。
你可以这样做:
$user = User::create([
'name' => $request->input('name')
]);
Phone::create([
'phone' => $request->phone,
'user_id' => $user->id
]);
或者另一种方式是:
$user = User::create([
'name' => $request->input('name')
]);
$user->phone()->create([ // this uses the create method on the users phone relationship
'phone' => 999999999,
]);
不确定是否也可以改变它,但为了可读性,我不推荐它(即 User::create([])->phone()->create([]);
)