简体   繁体   English

如何使用Laravel 4将数据从一个表链接到另一个表?

[英]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. belongsTohasMany参数错误。

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. 您只需要您的belongsTohasMany方法中的第一个参数。 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.

相关问题 在laravel中,如何从一个表中检索链接到另一表上已删除行的数据时排除行 - In laravel, how to exclude rows while retrieving data from one table which have link to a deleted row on another table 如何使用Database:Seeding将数据从一个表导入另一个表? (laravel 5.3)? - How to import data from one table to another table using Database: Seeding? (laravel 5.3)? 如何使用 laravel 迁移将数据从一个表传输到另一个数据库表 - How can i transfer data from one table to another database table using laravel migration 如何在Laravel中将数据从一个表发送到另一个表? - How to send data from one table to another in Laravel? 如何将表的数据从一个视图发送到 laravel 中的另一个视图 - how to send data of table from one view to another view in laravel 如何在 laravel 中将数据从一个表插入到另一个表? - How to insert data from one table to another in laravel? 如何使用laravel从一个表复制到另一个表? - How to copy from one table to another using laravel? 如何将一个表中的Id与另一个表相关联 - How to link Id from one table with another 如何使用Laravel从一个视图向另一个视图提交表单数据 - How to submit form data from one view to another using Laravel Laravel 如何使用按钮将数据从表移动到另一个 - Laravel how to move data from table to another using button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM