Laravel eloquent 一对多反转 returns 空
Laravel eloquent inverse one to many returns empty
我有以下 MySQL table 结构:
posts
table:
posts: {id(PK), title, content, slug, date, writer_id, created_at, updated_at}
writers
table:
writers: {id(PK), name, type, created_at, updated_at}
迁移 classes 在 database/migrations
目录中:
个帖子 table:
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();
});
}
}
作家table:
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
目录中创建了一个存储库 class。
PostRepository.php:
class PostRepository
{
public function forSingle($slug)
{
return Post::whereSlug($slug)->get();
}
}
我调试了上面的查询:
return Post::whereSlug($slug)->toSql();
它 return 是以下查询:
select * from `posts` where `slug` = ?
我的路线在 routes/web.php
文件中。
web.php:
Route::get('/post/{slug}', 'PostController@single');
我终于在 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
这部分给我的错误是:
正在尝试获取非对象的 属性 (视图:\resources\views\single.blade.php)
我已经打印了 {{$blog}}
查看的 $blog。它没有 return 任何作者属性。你能帮我解决这个问题吗?
PS: 我没有在MySQL数据库中定义主键外键关系tables.
您必须定义外键或索引
在我的项目中,我使用索引关系
所以你要做的就是像这样在writer_id中添加索引关系
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')->unsigned()->index(); // add this
$table->timestamps();
});
}
请尝试前面的
当它是一对多eloquent逆时,我们需要明确的告诉我们需要另一个table数据。更改 PostRepository.php 中的以下内容修复了问题。
class PostRepository
{
public function forSingle($slug)
{
return Post::whereSlug($slug)->with('writer')->get();
}
}
我有以下 MySQL table 结构:
posts
table:
posts: {id(PK), title, content, slug, date, writer_id, created_at, updated_at}
writers
table:
writers: {id(PK), name, type, created_at, updated_at}
迁移 classes 在 database/migrations
目录中:
个帖子 table:
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();
});
}
}
作家table:
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
目录中创建了一个存储库 class。
PostRepository.php:
class PostRepository
{
public function forSingle($slug)
{
return Post::whereSlug($slug)->get();
}
}
我调试了上面的查询:
return Post::whereSlug($slug)->toSql();
它 return 是以下查询:
select * from `posts` where `slug` = ?
我的路线在 routes/web.php
文件中。
web.php:
Route::get('/post/{slug}', 'PostController@single');
我终于在 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
这部分给我的错误是:
正在尝试获取非对象的 属性 (视图:\resources\views\single.blade.php)
我已经打印了 {{$blog}}
查看的 $blog。它没有 return 任何作者属性。你能帮我解决这个问题吗?
PS: 我没有在MySQL数据库中定义主键外键关系tables.
您必须定义外键或索引
在我的项目中,我使用索引关系
所以你要做的就是像这样在writer_id中添加索引关系
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')->unsigned()->index(); // add this
$table->timestamps();
});
}
请尝试前面的
当它是一对多eloquent逆时,我们需要明确的告诉我们需要另一个table数据。更改 PostRepository.php 中的以下内容修复了问题。
class PostRepository
{
public function forSingle($slug)
{
return Post::whereSlug($slug)->with('writer')->get();
}
}