0

I have 3 models like this-

User

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $fillable =   [
                                'profile_picture',
                                'first_name',
                                'last_name',
                                'email',
                                'cell_no',
                                'website',
                                'date_of_birth',
                                'social_security_number_p1',
                                'social_security_number_p2',
                                'social_security_number_p3',
                                'address',
                                'location_latitude',
                                'location_longitude',
                                'password',
                                'user_type',
                                'is_enabled'
                            ];

    protected $hidden = [
                            'password',
                            'remember_token',
                            'id',
                            'user_type',
                            'is_enabled',
                            'created_at',
                            'updated_at'
                        ];

    public function advertisement()
    {
        return $this->hasMany('App\Advertisement', 'user_id');
    }
}

Advertisement

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Auth;

class Advertisement extends Model
{
    protected $fillable     =   [
                                    'user_id',
                                    'category_id',
                                    'sub_category_id',
                                    'title',
                                    'price',
                                    'description',
                                    'address',
                                    'location_lat',
                                    'location_lon',
                                    'is_active',
                                    'deleted_at'
                                ];

    public function User()
    {
        return $this->belongsTo('App\User','user_id', 'id');
    }

    public function Offer()
    {
        return $this->hasMany('App\Offer', 'add_id', 'id');
    }
}

Offer

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Offer extends Model
{
    protected $fillable =   [
                                'add_id',
                                'user_id',
                                'price',
                                'message'
                            ];

    public function User()
    {
        return $this->hasOne('App\User','user_id','id');
    }

    public function Advertisement()
    {
        return $this->hasOne('App\advertisement','add_id','id');
    }
}

I want to get advertisements with offer and offers with offer sender (user).

So, I am doing something like this-

return Advertisement::with('AdvertisementImages')
    ->with('Offer')
    ->with('User')
    ->where('user_id', Auth::user()->id)
    ->get();

And getting this-

[
    {
        "id": 1,
        "user_id": 16,
        "title": "123asd",
        "price": 123,
        "description": "asd asd asd ",
        "address": "Dhaka University Campus, Dhaka, Dhaka Division, Bangladesh",
        "location_lat": 23.73,
        "location_lon": 90.39,
        "total_views": "110",
        "avg_rating": "4.0000",
        "is_reviewed": 0,
        "is_offer_sent": 1,
        "advertisement_images": [
            {
                "image_name": "16-68261.jpg"
            },
            {
                "image_name": "16-34746.JPG"
            },
            {
                "image_name": "16-79570.jpg"
            }
        ],
        "offer": [
            {
                "id": 1,
                "add_id": 1,
                "user_id": 9,
                "price": 123,
                "message": "asd ad asdasd ",
                "created_at": "2016-08-11 02:22:43",
                "updated_at": "2016-08-11 02:22:43"
            },
            {
                "id": 2,
                "add_id": 1,
                "user_id": 15,
                "price": 43,
                "message": "as dfasd ",
                "created_at": "2016-08-11 02:24:04",
                "updated_at": "2016-08-11 02:24:04"
            },
            {
                "id": 3,
                "add_id": 1,
                "user_id": 16,
                "price": 23,
                "message": "hgf",
                "created_at": "2016-08-11 02:25:31",
                "updated_at": "2016-08-12 01:37:42"
            }
        ],
        "user": {
            "profile_picture": "",
            "first_name": "Ne",
            "last_name": "Ajgor",
            "cell_no": null,
            "email": "[email protected]",
            "website": "",
            "date_of_birth": "0000-00-00",
            "social_security_number_p1": "",
            "social_security_number_p2": "",
            "social_security_number_p3": "",
            "address": "",
            "location_latitude": 0,
            "location_longitude": 0
        }
    }
]

So, it is wrong. I am getting user from Advertisement.

But I like to have user from Offer.

So, I need to have something like this-

[
    {
        "id": 1,
        "user_id": 16,
        "title": "123asd",
        "price": 123,
        "description": "asd asd asd ",
        "address": "Dhaka University Campus, Dhaka, Dhaka Division, Bangladesh",
        "location_lat": 23.73,
        "location_lon": 90.39,
        "total_views": "110",
        "avg_rating": "4.0000",
        "is_reviewed": 0,
        "is_offer_sent": 1,
        "advertisement_images": [
            {
                "image_name": "16-68261.jpg"
            },
            {
                "image_name": "16-34746.JPG"
            },
            {
                "image_name": "16-79570.jpg"
            }
        ],
        "offer": [
            {
                "id": 1,
                "add_id": 1,
                "user_id": 9,
                "price": 123,
                "message": "asd ad asdasd ",
                "created_at": "2016-08-11 02:22:43",
                "updated_at": "2016-08-11 02:22:43",
                "user": {
                    "profile_picture": "",
                    "first_name": "Me",
                    "last_name": "Ajgor",
                    "cell_no": null,
                    "email": "[email protected]",
                    "website": "",
                    "date_of_birth": "0000-00-00",
                    "social_security_number_p1": "",
                    "social_security_number_p2": "",
                    "social_security_number_p3": "",
                    "address": "",
                    "location_latitude": 0,
                    "location_longitude": 0
                }
            },
            {
                "id": 2,
                "add_id": 1,
                "user_id": 15,
                "price": 43,
                "message": "as dfasd ",
                "created_at": "2016-08-11 02:24:04",
                "updated_at": "2016-08-11 02:24:04",
                "user": {
                    "profile_picture": "",
                    "first_name": "Ne",
                    "last_name": "Ajgor",
                    "cell_no": null,
                    "email": "[email protected]",
                    "website": "",
                    "date_of_birth": "0000-00-00",
                    "social_security_number_p1": "",
                    "social_security_number_p2": "",
                    "social_security_number_p3": "",
                    "address": "",
                    "location_latitude": 0,
                    "location_longitude": 0
                }
            },
            {
                "id": 3,
                "add_id": 1,
                "user_id": 16,
                "price": 23,
                "message": "hgf",
                "created_at": "2016-08-11 02:25:31",
                "updated_at": "2016-08-12 01:37:42",
                "user": {
                    "profile_picture": "",
                    "first_name": "Je",
                    "last_name": "Ajgor",
                    "cell_no": null,
                    "email": "[email protected]",
                    "website": "",
                    "date_of_birth": "0000-00-00",
                    "social_security_number_p1": "",
                    "social_security_number_p2": "",
                    "social_security_number_p3": "",
                    "address": "",
                    "location_latitude": 0,
                    "location_longitude": 0
                }
            }
        ]
    }
]

Can anyone please help me, what I need to call?

Thanks in advance for helping.

1
  • Have you tried using the "dot" syntax? See the "Nested Eager Loading" section. Commented Aug 12, 2016 at 2:23

1 Answer 1

1

I have solved it like this-

Advertisement::with('AdvertisementImages')
    ->with('Offer','Offer.User')
    ->where('user_id', Auth::user()->id)
    ->has('Offer')
    ->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.