我有以下MySQL表结构:Laravel雄辩逆一对多返回空
posts
表:
posts: {id(PK), title, content, slug, date, writer_id, created_at, updated_at}
writers
表:
writers: {id(PK), name, type, created_at, updated_at}
迁移班database/migrations
目录:
posts表:
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('content');
$table->string('slug');
$table->date('date');
$table->date('modified_date');
$table->integer('publish');
$table->integer('trash');
$table->integer('wid');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
改变柱的类型:
class RenamePostColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function ($table) {
$table->longText('content')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function ($table) {
$table->longText('content')->change();
});
}
}
更名柱:
class RenamePostColumnWid extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function ($table) {
$table->renameColumn('wid', 'writer_id')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function ($table) {
$table->renameColumn('writer_id', 'wid')->change();
});
}
}
位作家表:
class CreateWritersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('writers', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name');
$table->string('type');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('writers');
}
}
以下是我在app
目录情态动词:
post.php中:
class Post extends Model
{
public function writer()
{
return $this->belongsTo(Writer::class);
}
}
Writer.php:
class Writer extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
现在我已经在app/Repositories
目录中创建了一个存储库类。
PostRepository.php:
class PostRepository
{
public function forSingle($slug)
{
return Post::whereSlug($slug)->get();
}
}
我上面的查询与调试:
return Post::whereSlug($slug)->toSql();
它返回下面的查询:
select * from `posts` where `slug` = ?
我的路线是routes/web.php
文件。
web.php:
Route::get('/post/{slug}', '[email protected]');
最后,我有我的app/Http/Controllers
目录控制器。
PostController。PHP:
use App\Repositories\PostRepository;
class PostController extends Controller
{
protected $post;
function __construct(PostRepository $post)
{
$this->post = $post;
}
public function single($slug)
{
return view('single', [
'post' => $this->post->forSingle($slug)
]);
}
}
我做了一件视图文件,如下所示:
single.blade.php
@if (count($post) > 0)
@foreach ($post as $blog)
<h3><a href="#">{{$blog->title}}</a></h3>
<p>{!!$blog->content!!}</p>
@foreach($blog->writer as $writer)
<span>{{$writer->name}}</span>
@endforeach
@endforeach
@endif
这里是我的问题。一切工作正常,直到我添加
@foreach($blog->writer as $writer)
<span>{{$writer->name}}</span>
@endforeach
本节给了我错误说:
试图让非对象的属性(查看:\资源\意见\ single.blade.php)
我在{{$blog}}
列印了$博客。它不返回任何作家属性。你能帮助我吗?
PS:我没有在MySQL数据库表中定义主键外键关系。
您可以添加邮政表的迁移类吗? –
@ Allloush补充说。 –