Laravel 5.2 laravel如何对待这种归属关系? [英] Laravel 5.2 How laravel treat this belongsto relationship?
问题描述
我有3张桌子,下面列出了这些字段:
I have 3 tables, with the fields listed below:
价格
- id
- route_id
路线
- id
- from_city_id
- to_city_id
城市
- id
- 名称
到目前为止,字段之间的关系是:pricings belong to a route
和routes belongs to city
.
So far the relationships of fields are: pricings belong to a route
, and routes belongs to city
.
但是我不确定这些关系,因为from_city_id
和to_city_id
是与cities
上的id
相关的外键.
But I'm not sure about these relationships, since from_city_id
and to_city_id
are foreign keys relating to id
on cities
.
也许我在设计桌子或其他东西时错了.
Maybe I'm wrong designing my table or something else.
route_id
是Routes
表上id
的外键.
from_city_id
和to_city_id
是Cities
表上id
的外键.
route_id
is a foreign key to id
on the Routes
table.
from_city_id
and to_city_id
are foreign keys to id
on the Cities
table.
如何定义这3个表的关系,以便可以从定价模型(如$pricing->from_city->name
和$pricing->to_city->name
)获得from city name
和to city name
?
How I can define relationships of these 3 tables so I can get from city name
and to city name
from the pricings model, like $pricing->from_city->name
and $pricing->to_city->name
?
任何帮助表示赞赏.
更新:
我的定价模型:
public function route()
{
return $this->belongsTo(Route::class);
}
我的路线模型:
public function pricing(){
return $this->hasOne(Pricing::class);
}
public function zone(){
return $this->belongsTo(Zone::class);
}
public function city(){
return $this->belongsTo(City::class);
}
public function from_city(){
return $this->belongsTo(City::class);
}
public function to_city(){
return $this->belongsTo(City::class);
}
现在我可以使用$pricing->route->from_city->name
和$pricing->route->to_city->name
它显示了正确的结果,但是如何使用Laravel来实现呢?
It shows the correct result, but how can this be achieved using Laravel?
这是否意味着Laravel将假定route
表具有字段to_city_id
和from_city_id
,因为路由模型中的方法是to_city()
和from_city()
?
Does this mean Laravel will assume that the route
table has fields to_city_id
and from_city_id
, since the method in the route model is to_city()
and from_city()
?
谢谢
推荐答案
一种解决方案可能是进行迁移(新表或更改为现有表). https://laravel.com/docs/5.3/migrations
One solution may be to make a migration (new table or to change to existing table). https://laravel.com/docs/5.3/migrations
Laravel的模式构建非常方便: https://laravel.com/docs/5.0/schema
Laravel's schema build is super handy: https://laravel.com/docs/5.0/schema
routes
迁移的示例为:
-
进行迁移:
Make the migration:
php artisan make:迁移路线
php artisan make:migration routes
迁移看起来像:
```
使用Illuminate \ Database \ Schema \ Blueprint; 使用Illuminate \ Database \ Migrations \ Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
Class CreateUserRole扩展了迁移 { /** *运行迁移. * * @返回无效 */ 公共功能up() { Schema :: create('routes',function(Blueprint $ table){
class CreateUserRole extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('routes', function (Blueprint $table) {
$table->increments('id');
$table->foreign('id')->references('route_id')->on('pricings')->onDelete('cascade');
$table->integer('from_city_id')->unsigned()->index();
$table->foreign('from_city_id')->references('id')->on('cities')->onDelete('no action');
$table->integer('to_city_id')->unsigned()->index();
$table->foreign('to_city_id')->references('id')->on('cities')->onDelete('no action');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('routes');
}
}
```
以上内容由于某种原因无法在此处正确显示,因此这里是一个清理后的视图链接: http://viper-7.com/kfgUjt
The above for some reason will not show correctly on here, so here is a cleaned up view link: http://viper-7.com/kfgUjt
这篇关于Laravel 5.2 laravel如何对待这种归属关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!