there is two tables, albums and tracks and in my albums table there is this
albums = ‘id’, ‘title’,’artist_id’, ‘genre_id’, ‘track_id’
While in my tracks table there is this
tracks = ‘id’, ‘title’, ‘album_id’, ‘artist_id’
in my Album.php Model i have
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Album extends Model
{
public function artist()
{
return $this->belongsTo(‘App\Artist’);
}
public function genre()
{
return $this->belongsTo(‘App\Genre’);
}
public function track()
{
return $this->hasMany(‘App\Track’);
}
}
and in my Track.php Model i have
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Track extends Model
{
public function artist()
{
return $this->belongsTo(‘App\Artist’);
}
public function albums()
{
return $this->belongsTo(‘App\Album’);
}
}
In My Track Controller I have
<?php
namespace App\Http\Controllers;
use App\Track;
use App\Http\Resources\Track as TrackResource;
use Illuminate\Http\Request;
class TrackController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// Get tracks
$tracks = Track::with(‘albums’)->get();
// Return collection of albums as a resource
return TrackResource::collection($tracks);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$track = new Track();
$track->title = $request->input(‘title’);
$track->album_id = $request->input(‘album_id’);
$track->artist_id = $request->input(‘artist_id’);
$track->duration = $request->input(‘duration’);
$track->release_date = $request->input(‘release_date’);
$track->track_position = $request->input(‘track_position’);
$track->track_link = $request->input(‘track_link’);
if($track->save()) {
return new TrackResource($track);
}else{
return “Error Saving”;
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
// Get single track
$track = Track::findOrFail($id);
// Return single track as a resource
return new TrackResource($track);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
// Get single track
$track = Track::findOrFail($id);
if($track->delete()) {
return “deleted”;
}else{
return “Error Deleting”;
}
}
}
Album Controller I have
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Album;
use App\Http\Resources\Album as AlbumResource;
class AlbumController extends Controller
{
public function index()
{
// Get albums
$albums = Album::with(‘artist’)->orderBy(‘artist_id’, ‘desc’)->limit(8)->get();
// Return collection of albums as a resource
return AlbumResource::collection($albums);
}
public function store(Request $request)
{
$album = new Album();
$album->title = $request->input(‘title’);
$album->artist_id = $request->input(‘artist_id’);
$album->genre_id = $request->input(‘genre_id’);
$album->artwork_path = $request->input(‘artwork_path’);
$album->album_date = $request->input(‘album_date’);
$album->upc = $request->input(‘upc’);
$album->record_label = $request->input(‘record_label’);
if($album->save()) {
return new AlbumResource($album);
}else{
return “Error Saving”;
}
}
public function show($id)
{
// Get single album
$album = Album::findOrFail($id);
// Return single album as a resource
return new AlbumResource($album);
}
public function destroy($id)
{
// Get single album
$album = Album::findOrFail($id);
if($album->delete()) {
return “deleted”;
}else{
return “Error Deleting”;
}
}
}
I would like to display all the (tracks) related to an (album) under that album.
Solution :
You can do it by using relationships: Laravel eloquent relationships
Just add this function to the Album model:
public function tracks () {
return $this->hasMany(‘App\Track’);
}
And this to the Track model:
public function album () {
return $this->belongsTo(‘App\Album’);
}
That way you can get the tracks of a single album like this:
$tracks = App\Album::find($id)->tracks();
Solution 2:
<?php
class Album extends Entity
{
protected $table = ‘albums’;
public function tracks()
{
return $this->hasMany(Track::class);
}
}
class Track extends Entity
{
protected $table = ‘tracks’;
public function album()
{
return $this->belongsTo(Album::class);
}
public function artist()
{
return $this->belongsTo(Artist::class);
}
}
class Artist extends Entity
{
protected $table = ‘artists’;
public function tracks()
{
return $this->hasMany(Track::class);
}
}
// Get all traks of album :
$tracks = Album::find($id)->tracks();
// or all albums and their respective tracks :
$albums = Album::with(‘tracks’)->get();
// Get all traks of artist :
$tracks = Artist::find($id)->tracks();
// or all artists and their respective tracks :
$artists = Artist::with(‘tracks’)->get();
Know more about relationships here
Solution 3:
You can put the keys in the relationship’s function if you do not defined in the DB. Try to put another related key in relationship’s function like this
return $this->hasMany(‘App\Comment’, ‘foreign_key’, ‘local_key’);
For more information Eloquent Relationships
Hope it wiil be helpful to you :)