1 year ago

One-to-One Relationships

Posted 1 year 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->bigInteger('post_uid', false, true)->default(0);
            $table->char('post_type', 20)->default('article');
            $table->char('post_status', 20)->default('draft');

This is the PostDetail migration schema

        Schema::create('post_details', function (Blueprint $table) {
            $table->bigInteger('post_id', false, true);
            $table->char('post_discuss_status', 20);
            $table->char('post_protected', 255);
            $table->char('post_slug', 255);
            $table->char('post_metakeys', 255);

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

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


What is the correct query to use?

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