我有急切的加載/ n + 1問題,研究和閱讀和觀看關於這個問題的教程整天排除故障,但還沒有解決它。我已經建立了模型的關係,但是當我用輔助函數傳遞數據時,我得到了這個n + 1問題。 我想從url site.com/Artist/songs獲取藝術家名稱,並獲取所有歌曲並顯示這樣的網址。Laravel 4 Eager Loading n + 1期
site.com/$Artist/songs/$id
我的藝術家/ index.blade.php視圖看起來像這樣http://i61.tinypic.com/2nqzatk.jpg 我不知道我在這裏失蹤。
在此先感謝!
我的表
歌曲 ID,標題,正文,蛞蝓,命中,artist_id,created_at,的updated_at
藝術家 ID,姓名,身體,created_at,的updated_at
routes.php文件
Event::listen('illuminate.query', function($query)
{
var_dump($query);
});
...
Route::get('{artist}/songs', '[email protected]');
Route::get('{artist}/songs/{id}', ['as' => 'artist.songs.show', 'uses' => '[email protected]']);
型號:Song.php
class Song extends Eloquent {
protected $guarded = ['id'];
/**
* Setting up relationship for the artist model for easier usage
*
*/
public function artist()
{
return $this->belongsTo('Artist');
}
// Override find method
public static function find($id, $name = null)
{
$song = static::with('artist')->find($id);
// If the song is found
if ($song)
{
// If the song doesn't belong to that artist, throw an exception which will redirect to home, defined in global.php
if ($name and $song->artist->name !== $name)
{
throw new Illuminate\Database\Eloquent\ModelNotFoundException;
}
return $song;
}
// If the song is not found, throw an exception which will redirect to home, defined in global.php
else
{
throw new Illuminate\Database\Eloquent\ModelNotFoundException;
}
}
// Get songs from artist
public static function byArtist($name)
{
return Artist::byName($name)->songs;
}
}
模型Artist.php
class Artist extends Eloquent {
protected $fillable = [];
/**
* Setting up relationship with the song model for easier usage
* $artist->songs;
*/
public function songs()
{
return $this->hasMany('Song');
}
// Get artist by name
public static function byName($name)
{
return static::whereName($name)->first();
}
}
控制器:ArtistsController.php
class ArtistsController extends BaseController {
// Set default layout for this controller
protected $layout = 'layouts.master';
/**
* Display a listing of the resource.
* GET /artists
*
* @return Response
*/
public function index($name)
{
$this->data['songs'] = Song::byArtist($name);
$this->layout->content = View::make('artists.index', $this->data);
}
helpers.php
function link_to_artist_song(Song $song)
{
return link_to_route('artist.songs.show', $song->title, [$song->artist->name, $song->id]);
}
爲藝術家 藝術家/ index.blade索引圖。 php http://i61.tinypic.com/2nqzatk.jpg
@extends('layouts.master')
@section('content')
@if(isset($songs))
<h1>All Songs</h1>
<ul class="list-group">
@foreach($songs as $song)
<li class="list-group-item">{{ link_to_artist_song($song) }}</li>
@endforeach
</ul>
@endif
@stop
是的,這是正確的我想從一個給定的藝術家的所有歌曲與$名稱從URL。 我用您的代碼行取代了 '$ this-> data ['songs'] = Artist :: with('songs') - > where'($ name) - > first()這個錯誤與我的幫手。 「傳遞給link_to_artist_song()的參數1必須是Song的一個實例,布爾給定,在...中調用」 因此,這些方法根本無法用於提取歌曲? 歌模型 \t'公共靜態函數byArtist($名) \t { \t \t回報藝術家::綽號($名) - >歌曲; \t}' 藝術家模型 \t'公共靜態函數綽號($名) \t { \t \t回靜態:: whereName($名) - >第一(); \t}' – Vartox
我希望能夠抓住藝術家的名字並顯示這樣的鏈接 'domain.com/ArtistName/songs/1' 這就是爲什麼我想要加載的渴望。 – Vartox
您想要列出名稱爲$ name的給定藝術家的所有歌曲,所以您只需要一位藝術家和所有相關歌曲。除非你想要別的東西。檢查我的編輯在一秒鐘內 –