我在获取外键时遇到了一些麻烦。
我的迁移看起来像这样(缩短了它们):
<?php
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('title');
$table->string('filename');
$table->integer('number_of_chapters');
$table->text('input_mpg');
$table->timestamps();
});
}
public function down()
{
Schema::drop('products');
}
}
<?php
class CreateChaptersTable extends Migration
{
public function up()
{
Schema::create('chapters', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->time('input-chapter-start1');
$table->time('input-chapter-end1');
$table->timestamps();
});
Schema::table('chapters', function($table) {
$table->foreign('product_id')->references('id')->on('products');
});
}
public function down()
{
Schema::drop('chapters');
}
}
我的2模型是这样的:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Chapters extends Model
{
protected $table = 'chapters';
protected $fillable = ['input-chapter-start1', 'input-chapter-end1'];
public function product()
{
return $this->belongsTo('App\Product');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table = 'products';
protected $fillable = ['email', 'title', 'filename', 'inputMpg', 'number_of_chapters'];
public static $rules = [
'email' => 'required|email|max:50',
'title' => 'required|max:50',
'filename' => 'required|max:50',
'input_mpg' => 'required'
];
public function Chapters()
{
return $this->hasMany('App\Chapters');
}
}
只需将它保存在我的控制器中
$product->save();
$Chapters->save();
并收到以下错误:
SQLSTATE [23000]:完整性约束违规:1452无法添加或 更新子行:外键约束失败 (
generator
。chapters
,CONSTRAINTchapters_product_id_foreign
FOREIGN KEY(product_id
)REFERENCESproducts
(id
))(SQL:insert 进入chapters
(input-chapter-start1
,input-chapter-end1
,updated_at
,created_at
)值(12:12:12,12:12:12,2016-04-25 11:41:31,2016-04-25 11:41:31))
修改 控制器看起来像这样:
namespace App\Http\Controllers;
class ProductController extends Controller
{
protected $request;
public function request(Request $request)
{
$this->request = $request;
}
public function createProduct(Request $request)
{
$product = new Product;
$Chapters = new Chapters($request->all());
$data = $request->all();
$projectEmail = $request->input('email');
$projectTitle = $request->input('title');
$projectFile = $request->input('filename');
$projectChapters = $request->input('number_of_chapters');
$validator = Validator::make($request->all(), Product::$rules);
if($validator->fails())
{
return Redirect::back()->withInput()->withErrors($validator);
}
$product->fill($data);
if($product->save())
{
$Chapters->product()->associate($product);
$Chapters->save();
return redirect()->route('root')->with('message', 'success')->withInput();
}
else
{
return redirect()->route('newProduct')->with('message', 'Error')->withInput();
}
}
}
编辑我试过Samsquanch建议: 并在我的控制器中添加了这个:
$product->save();
$Chapters->product()->associate($product);
$Chapters->save();
但仍然收到此错误消息:
Builder.php第2093行中的BadMethodCallException:调用undefined 方法Illuminate \ Database \ Query \ Builder :: products()
答案 0 :(得分:0)
问题在于你没有告诉Laravel或MySQL应该是什么外键。
这里有两个选项(均来自文档:https://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models)
第一个选项是将chapters
保存到product
:
$chapters = $product->chapters()->saveMany($Chapters); // or just->save() if it's only one
第二个(以及我通常如何做)将使用依赖于章节模型中associate()
关系的belongsTo
:
$product->save();
$Chapters->product()->associate($product);
$Chapters->save();
还有第三个但不推荐的选项,只需手动设置外键。
编辑:
$product->chapters()->associate($Chapters);
$product->save();