Resource

Published 4 days ago by thiagousa

I have two table, and i need to take just the name of all user for post.

Sergiu17
Sergiu17
4 days ago (192,060 XP)
Post::where('user_id', $userId)->get('name');
Cronix
Cronix
4 days ago (786,650 XP)

or if using relationships: https://laravel.com/docs/5.7/eloquent-relationships

// get the post, and the user who created the post, for post id = 4
$post = Post::with('user')->find(4);

echo $post->user->name;

You might want to explain a bit more.

thiagousa

$posts = Post::with('user')->get();

return new PostCollection($posts);
Garry_Oldman

Yep. This variant is pretty good.

return new PostCollection($posts);

thiagousa

It's works.

public function postsDataTables()
{
    $datatable = DataTables::of(PostResources::collection(Post::all()))->toJson();
    
    return $datatable;
}

Exemplo in this link - https://github.com/thiagousa/datatable

Please sign in or create an account to participate in this conversation.