How can i select from Database table For my API?

时间:2019-02-09 09:32:30

标签: rest eloquent laravel-5.2

I want to select from a database table e.g

 select artist.name as name from albums WHERE 
                albums.title != '' 
            AND 
                artists.id = albums.artist 
            ORDER BY 
                artist DESC LIMIT 8 ";

How can i use this to build my API in laravel?

public function index()
    {
        //Get albums



        $albums = Album::select('artist.name as name')->from('albums')->orderBy('artist', 'desc')->limit(8)->get();


        //Return collection of albums as a resource
        return AlbumResource::collection($albums);
    }

0 个答案:

没有答案