如何使用Laravel Eloquent从两个表中获取(专辑和曲目)

时间:2019-02-16 16:40:11

标签: php laravel vue.js

我有两个表,专辑和曲目,在我的专辑表中有这个 专辑='id','title','artist_id','genre_id','track_id'  在我的足迹表中,我有这个 tracks ='id','title','album_id','artist_id'

在我拥有的Album.php模型中

<?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');
    }
}

在我的Track.php模型中

<?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');
    }
}

在我的跟踪控制器中,

<?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";
        }
    }
}

我有专辑控制器

<?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";
        }
    }
}

我想在该专辑下显示与(专辑)相关的所有(曲目)。

3 个答案:

答案 0 :(得分:1)

您可以使用以下关系来做到这一点:Laravel eloquent relationships

只需将此功能添加到相册模型中即可:

public function tracks () {
     return $this->hasMany('App\Track');
}

这是您的Track模型:

public function album () {
     return $this->belongsTo('App\Album');
}

这样,您可以像这样获取单个专辑的曲目:

$tracks = App\Album::find($id)->tracks();

答案 1 :(得分:1)

<?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();

进一步了解关系here

答案 2 :(得分:0)

如果未在数据库中定义键,则可以将键放在关系的函数中。尝试像这样在关系功能中放置另一个相关键

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

有关更多信息,Eloquent Relationships

希望对您有帮助:)