Laravel Virgin:在模型的转换器中访问参与模型的逆模型关系

时间:2019-06-20 14:32:28

标签: database laravel eloquent

按照我的逻辑,我有这些表:

Table Grid:
id PK
width INT
height INT

Table Rover:
id pk
grid_pos_x UNSIGNED INT
grid_pos_y  UNSIGNED INT
grid_id UNSINGED BIGINT

注意:为了使您更容易理解,我尽量保持精简,有关完整表的规格,请参见下面的迁移脚本。

然后我使用以下迁移脚本来创建模式:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGridTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('grid', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('width');
            $table->unsignedInteger('height');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('grid');
    }
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRoverTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('rover', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('grid_id')->unsigned();
            $table->string('command');
            $table->foreign('grid_id')->references('id')->on('grid');
            $table->smallInteger('last_commandPos')->unsigned()->default(0);
            $table->smallInteger('grid_pos_x')->unsigned();
            $table->smallInteger('grid_pos_y')->unsigned();
            $table->enum('rotation', App\Constants\RoverConstants::ORIENTATIONS);
            $table->string('last_command');

            Schema::enableForeignKeyConstraints();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('rover');
    }
}

然后我尝试使用以下模型对表rover进行建模:

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Model\Grid;

class Rover extends Model
{

    /**
     * Table Name
     */
    protected $table='rover';

    public function grid()
    {
        return $this->belongsTo(Grid::class);
    }

    public function setGridPosXValue($value)
    {

    }

    public function setGridPosYValue($value)
    {

    }
}

以及类似的网格模型:

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Model\Rover;

class Grid extends Model
{
    /**
     * Table Name
     */
    protected $table='grid';

    public function rovers()
    {
        return $this->hasMany(Rover::class);
    }
}

我想要实现的是从width访问heightGrid属性,以检查grid_pos_xgrid_pos_y是否较小从widthheight分别使用方法setGridPosXValuesetGridPosYValue

你知道怎么做吗?

1 个答案:

答案 0 :(得分:1)

好像您在这里建立了一对多关系:https://laravel.com/docs/5.8/eloquent-relationships#one-to-many

要获取流动站的网格,可以通过多种方式访问​​它:

直接:

$width = $Rover->grid->width;
$height = $Rover->grid->height;

使用构建器方法:

$Grid = $Rover->grid()->first();
$width = $Grid->width;
$height = $Grid->height;