Laravel雄辩反向一对多返回空

时间:2016-09-19 06:11:00

标签: php eloquent laravel-5.2 one-to-many blade

我有以下MySQL表结构:

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目录中的迁移类:

帖子表:

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}', '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。它不返回任何writer属性。你能帮我解决这个问题吗?

PS:我没有在MySQL数据库表中定义主键外键关系。

2 个答案:

答案 0 :(得分:1)

当它与许多雄辩的反向时,我们需要明确告诉我们需要其他表数据。在 PostRepository.php 中更改以下内容解决了问题。

class PostRepository
{
    public function forSingle($slug)
    {
        return Post::whereSlug($slug)->with('writer')->get();
    }
}

答案 1 :(得分:0)

您必须定义外键或索引

在我的项目中,我使用索引关系

所以你需要做的是在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();
    });
}

请尝试上一页