如何在查询中获取引用值?如果我有一个示例城市
表,其中引用了一个国家
表和一个国家
表的列。
移民
国家
public function up()
{
Schema::create('cities', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->integer('state_id')->unsigned();
$table->integer('country_id')->unsigned();
$table->index('state_id');
$table->index('country_id');
});
Schema::table('cities', function (Blueprint $table)
{
$table->foreign('state_id')
->references('id')
->on('states')
->onUpdate('cascade')
->onDelete('cascade');
$table->foreign('country_id')
->references('id')
->on('countries')
->onUpdate('cascade')
->onDelete('cascade');
});
}
国家
public function up()
{
Schema::create('states', function (Blueprint $table)
{
$table->id();
$table->text('name');
$table->integer('country_id')->unsigned()->nullable();
$table->index('country_id');
});
Schema::table('states', function (Blueprint $table)
{
$table->foreign('country_id')
->references('id')
->on('countries')
->onUpdate('cascade')
->onDelete('cascade');
});
}
城市
public function up()
{
Schema::create('cities', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->integer('state_id')->unsigned()->nullable();
$table->integer('country_id')->unsigned()->nullable();
$table->index('state_id');
$table->index('country_id');
});
Schema::table('cities', function (Blueprint $table)
{
$table->foreign('state_id')
->references('id')
->on('states')
->onUpdate('cascade')
->onDelete('cascade');
$table->foreign('country_id')
->references('id')
->on('countries')
->onUpdate('cascade')
->onDelete('cascade');
});
}
模型
国家
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
public function state()
{
return $this->hasMany(State::class);
}
public function city()
{
return $this->hasMany(City::class);
}
protected $table = 'countries';
}
状态
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class State extends Model
{
public function country()
{
return $this->belongsTo(Country::class);
}
public function city()
{
return $this->hasMany(City::class);
}
protected $table = 'states';
}
城市
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class City extends Model
{
public function country()
{
return $this->belongsTo(Country::class);
}
public function state()
{
return $this->belongsTo(State::class);
}
protected $table = 'cities';
}
输出
如果我查询所有的城市城市::all()
我可能会得到这样的东西:
all: [
App\City {#3157
id: 1,
name: "New York",
state_id: 1,
country_id: 1,
},
App\City {#3158
id: 2,
name: "Dallas",
state_id: 2,
country_id: 1,
},
App\City {#3159
id: 3,
name: "Miami",
state_id: 3,
country_id: 1,
},
],
}
我将如何返回:
all: [
App\City {#3157
id: 1,
name: "New York",
state_id: "New York",
country_id: "USA",
},
App\City {#3158
id: 2,
name: "Dallas",
state_id: "Texas",
country_id: "USA",
},
App\City {#3159
id: 3,
name: "Miami",
state_id: "Florida",
country_id: "USA",
},
],
}
如果您有正确的关系设置,您应该能够执行类似的操作。
City::with('country:id,name', 'state:id,name')->get();
在这里,我假设country
和state
表都有PKid
和name
列。
这种关系应该是这样的:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class City extends Model
{
public function country()
{
return $this->belongsTo('App\Country');
}
public function state()
{
return $this->belongsTo('App\State');
}
}