В моей логике у меня есть эти таблицы:
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)
{
}
}
И аналогичным образом модель Grid:
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 и height из Grid, чтобы проверить, меньше ли grid_pos_x и grid_pos_y по сравнению с width и height, используя методы setGridPosXValue и setGridPosYValue соответственно.
Вы знаете, как это сделать?
Я пробую только этот. В случае, если все вместе, мне пришлось бы перебирать их все, а затем выполнять проверку вручную.

Похоже, у вас здесь настроена связь один ко многим: https://laravel.com/docs/5.8/eloquent-relationships#один ко многим
Чтобы получить сетку для вездехода, вы можете получить к ней доступ несколькими способами:
Напрямую:
$width = $Rover->grid->width;
$height = $Rover->grid->height;
С помощью методов строителя:
$Grid = $Rover->grid()->first();
$width = $Grid->width;
$height = $Grid->height;
Вы пытаетесь пройти через все марсоходы, чтобы сделать это, или только по одному?