连接控制器中的两个表



绵羊迁徙

Schema::create('sheeps', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->integer('corral_id')->unsigned()->nullable();
$table->foreign('corral_id')->references('id')->on('corrals');
});

Corral只有name字段。我想展示有羊的畜栏。我可以检查每只羊的前端和相关畜栏的corral_id。但我想在后台做,所以我应该在那里养羊。

public function index():CorralResourceCollection
{
return new CorralResourceCollection(Corral::paginate());
}

您是想进行SQL连接,还是只连接两个集合就可以了?

在下面的例子中,我有两张桌子,分别命名为狗和猫。我可以通过以下操作轻松创建他们所有名字的集合(在Tinker中尝试(:

use MyCoolAppModelsDog;
use MyCoolAppModelsCat;
$all_my_pets = Dog::get()->pluck('name');
$all_my_pets->concat(Cat::get()->pluck('name'));

最新更新