php - How to determine columns that will be returned in the result when using `paginate()` method in Laravel?

778

I have 2 tables:users andarticles. To fetch all columns from thearticles table and onlyuser_name column from theusers table, I use this code:

$articles = Article::join('users', 'articles.user_id', '=', 'users.user_id')
    ->get(array('articles.*', 'users.user_name'));

and it works fine, but when I usepaginate() method like this:

$articles = Article::join('users', 'articles.user_id', '=', 'users.user_id')
    ->paginate(10);

it fetches all columns from both tables, which I don't want. My question is: How can I select columns that will be returned in the result if I usepaginate() method in Laravel framework?

50

Answer

Solution:

The select function does this.

$articles = Article::join('users', 'articles.user_id', '=', users.user_id')
    ->select('articles.*', 'users.user_name')
    ->paginate(10);

People are also looking for solutions to the problem: javascript - json encode multidimensional array get value

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.