Вот и все, я пытаюсь создать отношение OneToOne (и даже OneToMany), а затем отобразить данные с помощью Blade.
Итак, у меня есть две сущности: Test и Test2.
И этоТест с ключом test2_id.
Но у меня есть ошибка ниже при отображении списков
Undefined property: stdClass::$test2
Вот коды:
Создание тестовой таблицы:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('test', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name', 100);
$table->integer('test2_id')->unsigned();
$table->foreign('test2_id')
->references('id')
->on('test2');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('test');
}
}
Создание таблицы test2:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTest2Table extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('test2', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('nameB', 100);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('test2');
}
}
Тестовая модель:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
protected $table = 'test';
public $timestamps = false;
protected $fillable = ['name', 'test2_id'];
public function test2()
{
return $this->belongsTo('App\Test2');
}
}
Модель Test2:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Test2 extends Model
{
protected $table = 'test2';
public $timestamps = false;
protected $fillable = ['nameB'];
public function test()
{
return $this->hasOne('App\Test');
}
}
Контроллер:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Test;
use App\Test2;
use App\Http\Requests\TestRequest;
class TestController extends Controller
{
public function getInfos()
{
return view('test_info');
}
public function postInfos(TestRequest $request)
{
$test2 = new Test2;
$input = ['nameB' => 'monNomB'];
$mtest2_id = $test2->create($input)->id;
$test = new Test;
$test->test2_id = $mtest2_id;
$test->name = $request->input('name');
$test->save();
return redirect('listtest');
}
public function list(){
$tests = \DB::table('test')->get();
return view('test_ok', compact('tests'));
}
}
И файл test_ok.blade:
<!DOCTYPE html>
<html>
<head>
<title>OK</title>
</head>
<body>
<h1>OK</h1>
@if(count($tests) > 0)
@foreach($tests as $test)
<p>Name: {{ $test->name }} - NameB: {{ $test->test2->nameB }}
@endforeach
@endif
</body>
</html>
Поэтому на дисплее (в файле test_ok.blade) появляется сообщение об ошибке:
Undefined property: stdClass::$test2
Уже несколько часовЯ борюсь, как дьявол, но я не могу найти никакого решения.
Может ли кто-нибудь мне помочь?