EloquentのfirstOrCreate()
メソッドを使用してデータを挿入するか、存在する場合は取得します。LaravelのモデルにIDを渡しません
私は$trip->id
を取得する必要があります。 FlightControllerのecho
FlightControllerの場合、は正しいid
ですが、user_flights
テーブルに挿入するためにUserFlightsモデルに渡していないようです。
私は、ページ上でこのエラーを取得する:Connection.phpライン729で
QueryException:SQLSTATE [23000]:整合性 制約違反:1048列 'user_trips_id' はNULLにすることはできません(SQL: を挿入
user_flights
(airport_from
、airport_to
、user_trips_id
)の値(1、4、))
フロー:ユーザー選択の2つの空港(flyiドロップダウンボックス(<select>
)から「飛行機」に飛行機を追加して飛行機に追加します。彼らは旅行をしていない場合は、それを作成し、2つの空港を旅行に追加します。
スキーマ
# `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();
}
}
UserTripsモデル
<?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;
}
}
UserFlightsモデル
<?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');
}
}
あなたはエラーメッセージ 'Column 'user_trips_id'をnullにすることはできません。これは問題ではないようですが、データに値はありませんが、DBファイルはALLOW NULLに設定されていません。 user_trips_idデータにsa値が必要ですか? –
@SimonDavies私はエラーを理解していますが、 'user_trips_id'はNULLであってはなりません。私が言ったように、私はFlightControllerで正しいIDを見ることができますが、UserFlightsモデルには渡されません。多分私はあなたを理解していませんか? – Draven