Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I join with Eloquent: Relationships?

My query is like this :

<?php
public function getListReviews()
{
    $reviews = Review::where('user_id', auth()->user()->id)
                     ->get();
    return $reviews;
}

From the query, it can get all review data by id

I want get user photo, store photo and product photo

I want get it use Eloquent: Relationships

How can I get it with Eloquent: Relationships?

My review model is like this :

<?php
namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class Review extends Eloquent
{
    use HybridRelations;
    protected $connection = 'mongodb';
    protected $fillable = ['user_id', 'user_name', 'product_id', 'product_name', 'store_id', 'store_name', 'invoice_number', 'rating', 'comments', 'created_at', 'updated_at'];
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

My user model is like this :

<?php
namespace App;
...
class User extends Authenticatable
{
    ...
    protected $fillable = ['name', 'email', 'password', 'birth_date', 'mobile_number', 'photo'];
    public function store()
    {
        return $this->hasOne(Store::class);
    }
}

My store model is like this :

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Store extends Model
{   
    protected $fillable = ['user_id', 'name', 'address', 'phones', 'total_product', 'photo'];
    public function products() 
    {
        return $this->hasMany(Product::class);
    }
}

My product model is like this :

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
    protected  $fillable = ['store_id','category_id','name', 'photo','description'];
    public function store()
    {
        return $this->belongsTo(Store::class);
    }
}
like image 545
moses toh Avatar asked Mar 07 '17 06:03

moses toh


People also ask

How do you join in eloquent?

laravel eloquent Join method by default use inner join. For example if you have 10 rows and you want the join from other table then it will only return the rows which satisfy both the tables. Laravel eloquent or query builder join method do the same as MySQL inner join function.

Can we use join in eloquent Laravel?

Eloquent power joins Add some Laravel magic to your Eloquent joins. If you have some experience using databases, it is very likely you have used joins at least once in your career. Joins can be used for a bunch of different reasons, from selecting data from other tables to limiting the matches of your query.

What is relationship in eloquent?

Defining Relationships. Eloquent relationships are defined as methods on your Eloquent model classes. Since relationships also serve as powerful query builders, defining relationships as methods provides powerful method chaining and querying capabilities.

What are eloquent relationships and how do they work?

In addition, all types of Eloquent relationships also serve as query builders, allowing you to continue to chain constraints onto the relationship query before finally executing the SQL query against your database. For example, imagine a blog application in which a User model has many associated Post models: * Get all of the posts for the user.

How to use eloquent join in Laravel?

Laravel Eloquent Join. Laravel JOIN eloquent returns all rows from the both table, if there are matches in the both table. Otherwise, the result is NULL. Now, demonstrates laravel eloquent join with the following examples. You can see the following example of laravel eloquent join() method: Example 1: Laravel Eloquent Join() with 2 Tables

How do I use eloquent to retrieve specific columns of relationships?

For this reason, Eloquent allows you to specify which columns of the relationship you would like to retrieve: When using this feature, you should always include the id column and any relevant foreign key columns in the list of columns you wish to retrieve. Sometimes you might want to always load some relationships when retrieving a model.

How does eloquent handle n + 1 query problems?

However, Eloquent can "eager load" relationships at the time you query the parent model. Eager loading alleviates the "N + 1" query problem. To illustrate the N + 1 query problem, consider a Book model that "belongs to" to an Author model: * Get the author that wrote the book.


1 Answers

If you want to find out product and store from review model then add two more methods to Review model as below.

Edit App\Review.php

//You already have key to to find store i.e. store_id
public function store()
{
    return $this->belongsTo(Store::class);
}


//You already have key to to find product i.e. product_id    
public function product()
{
    return $this->belongsTo(Product::class);
}

then execute your query as below

$reviews = Review::where('user_id', auth()->user()->id)
           ->with('store')
           ->with('product')
           ->with('user')
           ->get();

and you can access them as below,

Iterate through $reviews object as $review

$review->store->photo;
$review->product->photo;
$review->user->photo;
like image 98
Vaibhavraj Roham Avatar answered Oct 19 '22 02:10

Vaibhavraj Roham