Добрый день, подскажите пожалуйста - почему не получается получить доступ к связи hasMany?
Миграции, Сидеры и модели расположил тут:
https://paste.laravel.io/1188864d-3bbe-400b-8e9a-2...
Ввиду комментария ниже - продублирую тут.
Миграции:
class CreateBrandsTable extends Migration
{
public function up()
{
Schema::create('brands', function (Blueprint $t) {
$t->increments('id');
$t->string('name')->index();
$t->timestamps();
$t->softDeletes();
});
}
public function down()
{
Schema::dropIfExists('brands');
}
}
class CreateEditionsTable extends Migration
{
public function up()
{
Schema::create('editions', function (Blueprint $t) {
$t->increments('id');
$t->integer('brand_id')->index();
$t->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');
$t->string('name')->index();
$t->string('slug')->nullable()->index();
$t->text('body')->nullable();
$t->timestamps();
$t->softDeletes();
});
}
public function down()
{
Schema::dropIfExists('editions');
}
}
Модели:
<?php
namespace App\Entity\Vehicle;
use App\Entity\Behaviors\ImagePublicBehavior;
use App\Entity\brand\BrandComment;
use App\Entity\User;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
class Brand extends Model
{
use Notifiable;
use SoftDeletes;
use Sluggable;
use ImagePublicBehavior;
protected $dates = ['created_at','updated_at', 'deleted_at'];
protected $fillable = ['name', 'slug',];
public function editions()
{
return $this->hasMany(Edition::class, 'id', 'brand_id');
}
}
namespace App\Entity\Vehicle;
use App\Entity\Behaviors\ImagePublicBehavior;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
class Edition extends Model
{
use Notifiable;
use SoftDeletes;
use Sluggable;
use ImagePublicBehavior;
protected $table = 'editions';
protected $dates = ['created_at','updated_at', 'deleted_at'];
protected $visible = ['id', 'name'];
protected $fillable = ['name', 'brand_id',];
public function sluggable()
{
return ['slug' => ['source' => 'name']];
}
public function brand()
{
return $this->belongsTo(Brand::class, 'brand_id', 'id');
}
}
Контроллер (!!! проблема):
namespace App\Http\Controllers\Admin\Vehicles;
use App\Entity\Vehicle\Brand;
use App\Entity\Vehicle\Edition;
use App\Entity\Vehicle\Vehicle;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Vehicle\BrandRequest;
class BrandsController extends Controller
{
public function show(Brand $brand)
{
// пробленая зона ахтунг
$brand->editions()->create(['name' => 'Example-xx']);
// Всегда 0, зотя записи в таблицах есть
echo $brand->editions()->count();
// так работает
// Edition::create(['name' => 'xxx', 'brand_id'=>$brand->id]);
return view('brands.admin.show', compact('brand','editions'));
}
}
?>
При сохранении ловлю:
Illuminate \ Database \ QueryException (23502)
SQLSTATE[23502]: Not null violation: 7 ОШИБКА: нулевое значение в столбце "id" нарушает ограничение NOT NULL DETAIL:
Ошибочная строка содержит (null, null, yyy, yyy, null, null, null, null, 1, null, null, 1, 2018-08-05 14:34:02, 2018-08-05 14:34:02, null). (SQL: insert into "editions" ("name", "id", "slug", "updated_at", "created_at") values (yyy, , yyy, 2018-08-05 14:34:02, 2018-08-05 14:34:02) returning "id")
На всякий случай еще
Seeder:
<?php
use Illuminate\Database\Seeder;
class BrandEditionTableSeeder extends Seeder
{
public function run()
{
DB::insert("INSERT INTO brands (id, name) VALUES (1,'Acura'),(2,'Alfa Romeo'),(3,'Alpina'),(4,'Aro'),(5,'Asia'),(6,'Aston Martin').........");
DB::insert("INSERT INTO editions (id,brand_id,name) VALUES (1,1,'CL'),(2,1,'EL'),(3,1,'Integra'),(4,1,'MDX'),(5,1,'NSX'),(6,1,'RDX'),(7,1,'RL'),(8,1,'RSX'),(9,1,'TL'),(10,1,'TSX'),(11,2,'33'),(12,2,'75'),(13,2,'145'),(14,2,'146'),(15,2,'147'),(16,2,'155'),(17,2,'156'),(18,2,'159'),(19,2,'164'),(20,2,'166'),(21,2,'Alfetta'),(22,2,'Brera'),(23,2,'GT'),(24,2,'GTV'),(25,2,'Giulietta'),(26,2,'Spider'),(27,3,'B11'),(28,3,'B12'),(29,3,'B3'),(30,3,'B6'),(31,3,'B7'),(32,3,'Roadster S'),(33,4,'10'),(34,4,'24'),(35,5,'Rocsta'),(36,6,'DB7'),(37,6,'DB9'),(38,6,'DBS'),(39,6,'V12 Vanquish'),.......");
$statement = "ALTER SEQUENCE brands_id_seq RESTART WITH 115";
DB::unprepared($statement);
$statement = "ALTER SEQUENCE editions_id_seq RESTART WITH 1403";
DB::unprepared($statement);
}
}
Laravel - 5.6
Postgres - 9.6.9
Буду благодарен за помощь.