简体   繁体   中英

How to get select field in model using laravel?

I want to fetch only the specific column seller_db from seller table with the help of model, but when i used this below code my relationship shows null value, please help me to find my mistake, and how can i fetch selected one field with model using laravel?

here is my User.php file.

User.php


    public function sellerDB()
        {
            $instance = $this->hasOne('\App\Seller', 'user_id', 'id')->select('seller_db');
            return $instance;
        }

Edit: I made a mistake, I misunderstood your database design, it's necessary add an aditional method to resolve this:

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

    public function sellerDB()
    {
        return $this->seller->seller_db;
    }

In this case you can call $user->sellerDB() and receive the column seller_db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM