php - display data from database based on other column table laravel 5

341
153

Answer

Solution:

In your User Model add this

    public function province()
    {
        return $this->hasOne('App\Model', 'foreign_key', 'local_key');
    }

And in your controller do this

$data = User::with('province')->get();
795

Answer

Solution:

You can write a join query to fetch the specific column from table.

e.g.

$data = DB::table('users')
->join('provinsi','provinsi.id','=','users.uiprv')
->select('users.name_of_column','provinsi.name_of_column')
->get();
728

Answer

Solution:

Add this in your user model,

     /**
     * Returns the province for the user
     *
     * @return user all details
     */
     public function province()
     {
         return $this->belongsTo('App\Province', 'uiprv', 'id');
     }        

or

     /**
     * Returns the province for the user
     *
     * @return user select details
     */
     public function province()
     {
         return $this->belongsTo('App\Province', 'uiprv', 'id')->select('id', 'nama_provinsi');
     }        

you can add some other fields also in the select.

Try these and reply for this answer.

People are also looking for solutions to the problem: php - Undefined variable that has been defined

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.