【发布时间】:2017-06-28 00:08:52
【问题描述】:
你好,这是我的亲戚:
地点:
public function _geoloc()
{
return $this->belongsTo('App\GeoLoc', 'id');
}
地质:
public function locations()
{
return $this->hasOne('App\Location', 'geoloc_id');
}
控制器:
$geoloc = new Geoloc;
$geoloc->lat = $request->input('lat');
$geoloc->lng = $request->input('lng');
$geoloc->save();
$geoloc_id = $geoloc->id;
$location = new Location;
$location->business_id = $business_id;
$location->latitude = $request->input('lat');
$location->longitude = $request->input('lng');
$location->slug = $request->input('name');
$location->geoloc_id = $geoloc_id;
$location->save();
完整的错误信息:
SQLSTATE[23000]:违反完整性约束:1452 无法添加或 更新子行:外键约束失败 (
liveoldham.locations, 约束locations_geoloc_id_foreign外键 (geoloc_id) 引用geoloc(id) 删除级联 ON UPDATE CASCADE) (SQL: 插入locations(business_id,latitude,longitude,slug,geoloc_id,updated_at,created_at) 值 (1, 53.4867546, -2.054066599999942, Live & Now, 94、2017-06-27 15:22:51、2017-06-27 15:22:51))
所有值都是正确的,行被插入到 geoloc 但它没有被插入到位置,这是我遇到的错误,如何解决?
//编辑:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateLocationsTable extends Migration {
public function up()
{
Schema::create('locations', function(Blueprint $table) {
$table->increments('id');
$table->integer('business_id')->unsigned();
$table->float('latitude', 10,6);
$table->float('longitude', 10,6);
$table->integer('geoloc_id')->unsigned();
$table->string('slug', 29)->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::drop('locations');
}
}
第二个:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGeolocTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('_geoloc', function(Blueprint $table) {
$table->increments('id');
$table->float('lat', 10,6);
$table->float('lng', 10,6);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('_geoloc');
}
}
【问题讨论】:
标签: laravel laravel-5 eloquent