A
A
adam_carraway2018-09-03 19:57:56
Laravel
adam_carraway, 2018-09-03 19:57:56

What is the best way to display username by id in laravel?

The question is how to do it right:
I have an index.blade file where the news is displayed:
id | title | body | author
1 | www | www | 1
2 | avy | fivw| 1
How to display the name of the author instead of 1 if I have a function in the model:

public static function author($id){
        return $author=DB::table('users')->where('id',$id)->select('name');
    }

I tried to do something like this:
@forelse($news as $item)
            <tr>
                <td>{{ $item->id }}</td>
                <td>{{ $item->title }}</td>
                <td>{{ $item->body }}</td>
                <td>{{ \App\News::author($item->author) }}</td>
            </tr>
    @endforelse

But it didn't work out. How to do it right?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question