[英]How to link data from one table to another using Laravel 4?
I have a customers migration table 我有一个客户迁移表
Schema::create('customers', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('address');
$table->string('phone');
$table->string('email');
});
and a tripsheet migration table which goes like this 和这样的Tripsheet迁移表
Schema::create('tripsheets', function(Blueprint $table) {
$table->increments('id');
$table->integer('tripsheet_num');
$table->integer('customer_id');
$table->string('date');
$table->string('customer_name');
$table->string('customer_address');
$table->string('customer_phone');
$table->string('rep_address');
$table->string('rep_phone');
});
I have also created a customer eloquent, 我也创造了一个有口才的客户,
class Customer extends Eloquent {
public $timestamps = true;
public $table = 'customers';
protected $fillable = ['name', 'address', 'phone', 'email'];
public function tripsheets(){
return $this->belongsTo('Tripsheet', 'name', 'address', 'phone');
}
}
and a tripsheet model, 和一个tripsheet模型,
class Tripsheet extends Eloquent {
public $timestamps = true;
public $table = 'tripsheets';
protected $fillable = [];
public function customer(){
return $this->hasMany('Customer', 'name', 'address', 'phone');
}
}
and my routes.php goes like this, 我的routes.php就这样,
Route::get('/', function()
{
return View::make('hello');
});
Route::controller('/customers', 'CustomerController');
Route::controller('/tripsheets', 'TripsheetController');
I would like to link the customer_name, customer_address, customer_phone from the tripsheet table to the name, address, phone of the customer table. 我想将Tripsheet表中的customer_name,customer_address,customer_phone链接到customer表的名称,地址,电话。 I also want to know how to route them and fetch them as a json data to be used by angular JS to display the result.
我还想知道如何路由它们并将其作为json数据获取,以供Angular JS用于显示结果。
now should i create a third table to link these two? 现在我应该创建第三个表来链接这两个表吗? Or should i call it with
Customer::with('tripsheets')->all();
还是我应该用
Customer::with('tripsheets')->all();
调用它Customer::with('tripsheets')->all();
in the routes/controller? 在路线/控制器中?
I also want to know how to route them and fetch them as a json data to be used by angular JS to display the result.? 我还想知道如何路由它们并将其作为json数据获取,以供有角JS使用以显示结果。
I overlooked your code and after user315.. answer I see the problem. 我忽略了您的代码,在user315之后。回答我看到了问题。 The
belongsTo
and hasMany
has wrong arguments. belongsTo
和hasMany
参数错误。
You need to change your code to the following to make it work: 您需要将代码更改为以下代码才能起作用:
class Customer extends Eloquent {
public function tripsheets(){
return $this->belongsTo('Tripsheet', 'tripsheet_num');
}
}
class Tripsheet extends Eloquent {
public function customer(){
return $this->hasMany('Customer', 'tripsheet_num');
}
}
The problem is that you have the field tripsheet_num
in the table tripsheets
. 问题是,你有场
tripsheet_num
在表tripsheets
。 Laravel tries to find a field called tripsheet_id
inside the tripsheets table when you use the belongsTo(Tripsheet)
on Customer
. 当您在
Customer
上使用belongsTo(Tripsheet)
时,Laravel会尝试在tripsheets表中找到一个名为tripsheet_id
的字段。 In your case this key field is named different and is not found, and so the relation is not set. 在您的情况下,此关键字段被命名为different且未找到,因此未设置该关系。
Same goes for hasMany()
only then it looks in the other table for the key field. hasMany()
相同,然后在另一个表中查找键字段。
See the relation documentation for more info: One-to-one relation & hasMany 有关更多信息,请参见关系文档: 一对一关系和hasMany
You only need the first argument in your belongsTo
and hasMany
method. 您只需要您的
belongsTo
和hasMany
方法中的第一个参数。 The others are likely the reason why it's not working. 其他人可能是它不起作用的原因。 They are meant for telling Eloquent what the foreign key is, and if you are specifying the foreign key as
name
, then it's obviously not going to work right. 它们的目的是告诉Eloquent外键是什么,如果您将外键指定为
name
,则显然无法正常工作。
Since you have proper naming conventions, Eloquent can accurately guess what they should be and you shouldn't need them. 由于您具有正确的命名约定,因此Eloquent可以准确地猜测它们应该是什么,并且您不需要它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.