Php 不将id传递到Laravel中的模型
我正在使用Eloquent的Php 不将id传递到Laravel中的模型,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我正在使用Eloquent的firstOrCreate()方法插入数据,或者检索数据(如果存在) 我需要获取$trip->id。如果我echo在FlightController中输入它,那么它echo就是正确的id,但它似乎没有将其传递到UserFlights模型中以插入到用户飞行表中 我在页面上看到此错误: Connection.php行729中的QueryException:SQLSTATE[23000]:完整性 约束冲突:1048列“用户id”不能为空(SQL: 插入用户航班(airpor
firstOrCreate()
方法插入数据,或者检索数据(如果存在)
我需要获取$trip->id
。如果我echo
在FlightController中输入它,那么它echo
就是正确的id
,但它似乎没有将其传递到UserFlights模型中以插入到用户飞行
表中
我在页面上看到此错误:
Connection.php行729中的QueryException:SQLSTATE[23000]:完整性
约束冲突:1048列“用户id”不能为空(SQL:
插入用户航班(airport\u from
,airport\u to
,
user\u trips\u id
)值(1,4,)
流程:用户从下拉框(
)中选择两个机场(从和飞到),并将它们添加到“行程”中。如果他们没有旅行,它会创建一个,然后将两个机场添加到他们的旅行中
模式
# `user_trips` table
Schema::create('user_trips', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->text('name');
});
# `user_flights ` table
Schema::create('user_flights', function (Blueprint $table) {
$table->increments('id');
$table->integer('trip_id')->unsigned();
$table->integer('airport_from')->unsigned();
$table->integer('airport_to')->unsigned();
$table->foreign('trip_id')->references('id')->on('user_trips')->onDelete('cascade');
});
FlightController
<?php
namespace App\Http\Controllers;
use App\UserFlights;
use App\UserTrips;
use Illuminate\Http\Request;
/**
* Class FlightController
*
* @package App\Http\Controllers
*/
class FlightController extends Controller
{
/**
* @param Request $request
* @param UserTrips $user_trips_obj
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request, UserTrips $user_trips_obj)
{
// Retrieve the trip by the attributes, or create it if it doesn't exist...
$trip=$user_trips_obj->addTrip();
# Returns the ID correctly.
//echo $trip->id;exit;
$user_trips_obj->addFlight(
new UserFlights([
'airport_from'=>$request->flight_from,
'airport_to'=>$request->flight_to,
# Does not pass the `$trip->id` into the UserFlights model.
'user_trips_id'=>$trip->id
])
);
return back();
}
}
您的addflights()
方法调用$this->userflights()->save($user\u flights\u obj)代码>
在关系对象上调用save()
时,它会将传入对象($user\u flights\u obj
)上的外键设置为拥有关系的对象的id($this
)。然后保存$user\u flights\u obj
对象
由于您的控制器调用$user\u trips\u obj->addFlight(new UserFlights…
),因此$this
方法中的$this
引用来自控制器的$user\u trips\u obj
实例。此实例只是一个空壳,id为空。因此,在addFlight()
方法中,当您对关系调用save()
时,它将把新的UserFlights
实例上的外键设置为调用addflights()
的实例的id(空白)
要解决此问题,只需在控制器中创建的$trip
实例上调用addFlight()
。这就是要将新的UserFlights
实例与之关联的实例。此外,您不需要手动设置外键;这就是对关系调用save()
的全部原因。您是否查看了错误消息列“user\u trips\u id”不能为null
这似乎就是问题所在,不是id您在数据中没有值,而是DB字段设置为允许NULL-更可能为false,因此需要在用户\u trips\u id数据中设置值?@SimonDavies我理解错误,但用户\u trips\u id
不应为NULL。正如我所说,我可以在FlightController中看到正确的ID,但它不会传递到UserFlights模型中。也许我不理解你?对。非常感谢。作品now@Draven当您创建新的UserFlights()
时,您正在手动分配user\u trips\u id
字段。如果要将此对象传递到关系的save()
方法中,则不需要这样做,因为save方法就是这样做的。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* Class UserTrips
*
*/
class UserTrips extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps=FALSE;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable=[
'name',
'user_id'
];
/**
* @param UserFlights $user_flights_obj
* @return Model
*/
public function addFlight(UserFlights $user_flights_obj)
{
return $this->userflights()->save($user_flights_obj);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function userflights()
{
return $this->hasMany('App\UserFlights');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
* @return mixed
*/
public function addTrip()
{
// Retrieve the trip by the attributes, or create it if it doesn't exist...
$trip=$this->firstOrCreate([
'user_id'=>1,
'name'=>'My Trip'
]);
return $trip;
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* Class UserFlights
*
*/
class UserFlights extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps=FALSE;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable=[
'airport_from',
'airport_to',
'user_trips_id'
];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function usertrips()
{
return $this->belongsTo('App\UserTrips');
}
}