One-to-One Relationships

Posted 6 months ago by Tetravalence

Hello! I'm confused by the whole thing. According to the Eloquent documentation to define the relationship, we place a link method on the Eloquent model but I have troubles getting the formula to get any data. My Post Model is associated with one PostDetail. The relationship one-to-one is defined by the details method in Post

public function details()
{
    return $this->hasOne('Tetravalence\MyPackage\PostDetail', 'post_id');
}

This is the Post migration schema

        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('post_uid', false, true)->default(0);
            $table->text('post_title');
            $table->char('post_type', 20)->default('article');
            $table->char('post_status', 20)->default('draft');
            $table->foreign('post_uid')->references('id')->on('users');
        });

This is the PostDetail migration schema

        Schema::create('post_details', function (Blueprint $table) {
            $table->bigIncrements('post_details_id');
            $table->bigInteger('post_id', false, true);
            $table->longText('post_body');
            $table->char('post_discuss_status', 20);
            $table->timestamp('post_date')->nullable();
            $table->timestamp('post_last_update')->nullable();
            $table->text('post_excerpt');
            $table->char('post_protected', 255);
            $table->bigInteger('post_parent');
            $table->char('post_slug', 255);
            $table->char('post_metakeys', 255);
            $table->foreign('post_id')->references('id')->on('posts');
        });

My query is not working bacause I get an array(0)

        $collection = $posts->details()->where('post_protected', 'yes')->get();

        dd($collection);

What is the correct query to use?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.