How to get only relations from eloquent query

laravel relationship with condition
hasmanythrough laravel example
laravel many to many relationship
savemany laravel
laravel pluck
laravel sync with pivot
laravel relationship tutorial
wherebetween laravel

I got the following code:

$popularProducts = PopularProduct::with('product')->get();

This is how I get all of products from popular_products (contains only product_id) table with relations for products table. But I have this result:

Collection {#409 ▼
  #items: array:1 [▼
    0 => PopularProduct {#411 ▼
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [▶]
      #original: array:4 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "product" => Product {#462 ▶}
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]

I need to take only relations->product field. How can I take it?

You need to iterate over the collection:

@foreach ($popularProducts as $popularProduct)
    {{ $popularproduct->product->id }}

If you just want to get all products from the collection, use the pluck() method:

$products = $popularProducts->pluck('product');


In the comments, you've said you want to get links directly from DB:


This will work if you've defined the popularProduct relationship in the Product model.

Select only one column from belongsTo relationship, I have this relationship in my "Company" model public do it in a eloquent style but query builder joining is the only way because� use Illuminate\Database\Eloquent\Relations\Relation; Relation::morphMap([ 'posts' => 'App\Post', 'videos' => 'App\Video', ]); You may register the morphMap in the boot function of your AppServiceProvider or create a separate service provider if you wish.

You can do it like this

$products = PopularProduct::with('product')->get()->pluck('product');

if you like the result to be keyed you can do it like this

$products = PopularProduct::with('product')->get()->pluck('product','id');

// you can key it with **id** of the table (popularProduct) or by **id_product** of relationship (product)

if you like only to retrieve specific fields from relation you can try something like this

 $products = PopularProduct::with('product:id,product_name')->get()->pluck('product','id');

Eloquent: Relationships - Laravel, If you are unable to generate the query you need via the fluent interface, feel free to use you may wish to save not only a model, but also all of its relationships. Laravel ORM Eloquent is brilliant! That being said, let’s try to tackle very common questions that appear when you try to get related models loaded. Want to skip the preface and get Straight to The Point? Why don’t we create a popular category-post relation in not less popular MySQL db ? So here we go!

You can use whereHas method (if there is a relationship between these tables)

$popular_products = Product::whereHas('products', function ($query) {

Eloquent ORM - Laravel, I have a relationship with Users and Events. Its a one to one. But I am trying to return all users that don't have an event tied to them on a certain� Since Eloquent doesn't do a real join when using "with" this will add a simple join method without having to worry about the join columns since it's all stored with the models anyway. It will also select all the columns from the joined table with an added prefix of the table name.

Return only if relationship where query is true, Making custom relation classes in Laravel. We need to tell Laravel's query builder to only select the data from the contracts table, because� Learn how to optimize your related model queries in Laravel with eager loading. We will set up some example relationships and then walk through how queries change with and without eager loading. I like to get my hands directly on code and experiment with things, and I hope to illustrate how eager loading works with some examples will further help you understand how to …

Relationship issues, Eloquent relationships are defined as functions on your Eloquent model classes. Since, like Eloquent models themselves, relationships also serve as powerful query builders, The phone method should call the hasOne method and return its result: By default, only the model keys will be present on the pivot object. My Eloquent api call looks as below: return Response::eloquent(Theme::with('user')->get()); Which returns all columns from theme (that's fine), and all columns from user (not fine). I only need the 'username' column from the user model, how can I limit the query to that?

Eloquent relationships, Sometimes you have to query and use the matching results, the same ones, actually. @return \Illuminate\Database\Eloquent\Relations\HasMany */ public And sometimes you want to use $post->comments with only those� Laravel provides a fantastic database query builder and ORM system named Eloquent. It makes running queries extremely simple and allows you to get records with minimal effort. However, there may be times when you need to build an advanced query and would like to see the actual SQL being generated.

  • Try foreach($popularProducts->product as product)
  • This is good, but how can I use any filters (I need to use linkFilter) from Product model in this case? I need something like: PopularProduct::with('product')->get()->pluck('product')->link(), but we need to use filters only from query builder, not from collection
  • This is probably my bad. I have an scope in Product model:public function scopeLink($query) { //some $query return $query; }, and I need to use it like Product::get()->link(), not with pluck from collection.
  • @Alexxosipov scope is just a way to reuse code. You can't load scope into the collection with get() or something.
  • Yes. I find a solution with: PopularProduct::with('product')->link()->get(), it works fine.