TransWikia.com

how to we can get records from two table using Relationships in laravel?

Stack Overflow Asked by anurag singh on December 7, 2021

I want that news should also come with all given reviews on it.Help will be highly appriciated.
This is News Model

<?php
namespace ModulesNewsletterEntities;
use BrexisLaravelWorkflowTraitsWorkflowTrait;
use IlluminateDatabaseEloquentModel;
class News extends Model {
use WorkflowTrait;

protected $table = 'news_info';
protected $fillable = [
'title', 'header', 'description', 'status', 'created_by', 'media_url', 'media_thumbnail',
];

    public function reviews() {
        return $this->hasMany(NewsReview::class,'reviewable_id');
    }

}

This is NewsReview Model

<?php

namespace ModulesNewsletterEntities;

use AppUser;
//use HynTenancyAbstractsTenantModel as TenancyModel;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;

class NewsReview extends Model {
    use SoftDeletes;
    
    protected $fillable = [
        'review_text',
        'review_reaction',
        'is_visible',
        'reviewed_by',
        'reviewable_id',
        'reviewable_type'
    ];

    public function reviewable() {
        return $this->morphTo();
    }

    public function reviewer() {
        return $this->hasOne(User::class, 'id', 'reviewed_by');
    }
}

This is my controller function

public function newsReview($news){
        $review = News::find($news);
    $newsReview=$review->reviews;

    return $newsReview;

This is the output I am getting by this

{
        "id": 1,
        "review_text": null,
        "review_reaction": "hiie this is revieew",
        "is_visible": 1,
        "reviewed_by": 1,
        "reviewable_id": 1,
        "reviewable_type": "Modules\Newsletter\Entities\News",
        "created_at": "2020-07-22 14:50:39",
        "updated_at": "2020-07-22 14:50:39",
        "deleted_at": null
    },
    {
        "id": 2,
        "review_text": null,
        "review_reaction": "hiie this is revieew 2",
        "is_visible": 1,
        "reviewed_by": 1,
        "reviewable_id": 1,
        "reviewable_type": "Modules\Newsletter\Entities\News",
        "created_at": "2020-07-22 15:04:25",
        "updated_at": "2020-07-22 15:04:25",
        "deleted_at": null
    },
    {
        "id": 3,
        "review_text": null,
        "review_reaction": "hiie this is revieew 3",
        "is_visible": 1,
        "reviewed_by": 1,
        "reviewable_id": 1,
        "reviewable_type": "Modules\Newsletter\Entities\News",
        "created_at": "2020-07-22 15:04:35",
        "updated_at": "2020-07-22 15:04:35",
        "deleted_at": null
    },

I want that news should also come with all given reviews on it.Help will be highly appriciated.

3 Answers

You can eager load the reviews relationship when retrieving the news model from the database:

public function newsReview($news)
{
    return News::with('reviews')->find($news);
}

this will return the news model and all of its reviews nested in the relations property.

From the docs:

When accessing Eloquent relationships as properties, the relationship data is "lazy loaded". This means the relationship data is not actually loaded until you first access the property. However, Eloquent can "eager load" relationships at the time you query the parent model.

class Book extends Model
{
    /**
     * Get the author that wrote the book.
     */
    public function author()
    {
        return $this->belongsTo('AppAuthor');
    }
}

$books = AppBook::with('author')->get();

Answered by Remul on December 7, 2021

you can use laravel eager loading and inside the News model use the proerty $with, which will tell laravel to fetch all reviews associated with a news instance, like so

class News extends Model {
use WorkflowTrait;

protected $table = 'news_info';
protected $fillable = [
'title', 'header', 'description', 'status', 'created_by', 'media_url', 'media_thumbnail',
];

   protected $with= ['review']; <-----------

    public function reviews() {
        return $this->hasMany(NewsReview::class,'reviewable_id');
    }

}

this way, whenever you fetch a News model, its reviews will be fetched too!, however if you dont want every query to have the reviews, only specific queries, then you can do this

News::with(['review'])->get()

Answered by Ahmed Khattab on December 7, 2021

Can you change the NewsReview table's reviewer relation function with

public function reviewer(){
  return $this->belongsTo(User::class);
}

thanks

Answered by user3291589 on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP