Open sidebar
Миграции: 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', 'parent_id', ]; ... public function editions() { return $this->hasMany(Edition::class, 'id', 'brand_id'); } ... } ?> class EditionSerial 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\EditionSerial; 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(); // так работает // EditionSerial::create(['name' => 'xxx', 'brand_id'=>$brand->id]); return view('brands.admin.show', compact('brand','editions')); } }
Close sidebar
Back
Please note that all pasted data is publicly available.
X (fomerly Twitter)
GitHub
Use setting
Back
Please note that all pasted data is publicly available.
X (fomerly Twitter)
GitHub
Use setting