Здравствуйте!. Есть следующие таблицы: Equipments (id, name, publisher_id, technical_attribute_id) Publishers (id, name) Technical_attributes(id, name) Я попробовал сделать сначала связь между двумя таблицами (Equipments и Publishers) как один ко многим (1 оборудование может иметь 1 производителя, а 1 производитель может делать много оборудования). И все вроде бы получилось. Выкладываю без git так уж исторически сложилось... Спойлер: Equipment.php PHP: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Equipment extends Model { protected $fillable = ['name', 'publisher_id']; protected $table = 'equipments'; public function publisher() { return $this->belongsTo('App\Publisher', 'publisher_id', 'id'); } } Спойлер: view/equipments/index.blade.php PHP: @extends('layouts.app') @section('content') <div class="container"> <a href="{{ route('equipment.create') }}" class="btn btn-primary">Создать</a> <hr> <table class="footable table table-stripped toggle-arrow-tiny"> <thead> <tr> <th data.toggle="true">Заголовок</th> <th data.toggle="true">Производитель</th> </tr> </thead> <tbody> @forelse($equipments as $equipment) <tr> <td>{{ $equipment->name }}</td> <td>{{ $equipment->publisher->name }}</td> <td class="text-right"> <form onsubmit="if(confirm('Удалить?')){return true}else{return false}" action="{{ route('equipment.destroy', $equipment) }}" method="post"> <input type="hidden" name="_method" value="DELETE"> {{ csrf_field() }} <div class="btn-group"> <a class="btn btn-secondary" href="{{ route('equipment.show', $equipment) }}">Просмотр</a> <a class="btn btn-primary" href="{{ route('equipment.edit', $equipment) }}">Редактировать</a> <button class="btn btn-danger" type="submit">Удалить</button> </div> </form> </td> </tr> @empty <p>Оборудование отсутствует</p> @endforelse </tbody> </table> </div> @endsection Спойлер: view/equipments/create.blade.php PHP: @extends('layouts.app') @section('content') <div class="container"> <form class="form-horizontal" action="{{ route('equipment.store') }}" method="post"> {{ csrf_field() }} <fieldset class="form-horizontal"> <div class="form-group"> <label class="col-sm-2 control-label">Наименование:</label> <div class="col-sm-10"> <input type="text" name="name" class="form-control" placeholder="" value="{{ $equipment->name or '' }}"> </div> <label class="col-sm-2 comtrol-label">Производитель:</label> <div class="col-sm-10"> <input type="text" name="publisher_id" class="form-control" placeholder="" value="{{ $equipment->publisher->name or '' }}"> </div> </div> <div class="form-group"> <div class="col-sm-4 sol-sm-offset-2"> <button class="btn btn-primary" type="submit">Сохранить</button> </div> </div> </fieldset> </form> </div> @endsection Спойлер: view/equipments/edit.blade.php PHP: @extends('layouts.app') @section('content') <div class="container"> <h3>Оборудование производителя: {{ $equipment->publisher->name or '' }}</h3> <form class="form-horizontal" action="{{ route('equipment.update', $equipment) }}" method="post"> {{ method_field('PUT') }} {{ csrf_field() }} <fieldset class="form-horizontal"> <div class="form-group"> <label class="col-sm-2 control-label">Наименование:</label> <div class="col-sm-10"> <textarea name="name" class="form-control">{{ $equipment->name or '' }}</textarea> </div> </div> <div class="form-group"> <div class="col-sm-4 col-sm-offset-2"> <button class="btn btn-primary" type="submit">Сохранить</button> </div> </div> </fieldset> </form> </div> @endsection Спойлер: migration table publishers PHP: <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePublishersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('publishers', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('publishers'); } } Спойлер: migration table equipments PHP: <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateEquipmentTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('equipments', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->integer('publisher_id'); $table->integer('technical_attribute_id'); $table->timestamps(); $table->foreign('publisher_id')->references('id')->on('publishers'); $table->foreign('technical_attribute_id')->references('id')->on('technical_attributes'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('equipments'); } } Спойлер: EquipmentController.php PHP: <?php namespace App\Http\Controllers; use App\Equipment; use Illuminate\Http\Request; class EquipmentController extends Controller { public function index() { return view('equipments.index', [ 'equipments' => Equipment::get(), ]); } public function create() { return view('equipments.create', [ 'equipment' =>[], ]); } public function store(Request $request) { Equipment::create($request->all()); return redirect()->back(); } public function edit(Equipment $equipment) { return view('equipments.edit', [ 'equipment' => $equipment, ]); } public function update(Request $request, Equipment $equipment) { $equipment->update($request->all()); return redirect()->route('publisher.index'); } } Спойлер: PublisherController PHP: <?php namespace App\Http\Controllers; use App\Publisher; use Illuminate\Http\Request; class PublisherController extends Controller { public function index() { return view('publishers.index', [ 'publishers' => Publisher::get(), ]); } public function create() { return view('publishers.create', [ 'publisher' => [], ]); } public function store(Request $request) { $publisher = Publisher::create($request->all()); return redirect()->route('publisher.show', $publisher); } public function show(Publisher $publisher) { return view('publishers.show', ['publisher' => $publisher]); } public function edit(Publisher $publisher) { return view('publishers.edit', [ 'publisher' => $publisher, ]); } public function update(Request $request, Publisher $publisher) { $publisher->update($request->all()); return redirect()->route('publisher.show', $publisher); } public function destroy(Publisher $publisher) { $publisher->delete(); return redirect()->route('publisher.index'); } } А как мне поступить если таблица Equipments у меня связывается не только с 1 таблицей Publisher а например еще с 5 таблицами. Как в описании 3 таблиц в начале вопроса? забыл еще Спойлер: Publisher.php PHP: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Publisher extends Model { protected $guarded = []; public function equipment() { return $this->hasMany('App\Equipment'); } }
вроде бы получилось путем добавления второй функции в основной модели, но правильно ли так? и получается, чтобы сейчас мне создать оборудование приходится указывать номер id