【问题标题】:Laravel eloquent relationship not giving me what i wantLaravel 雄辩的关系没有给我我想要的
【发布时间】:2015-03-13 00:33:12
【问题描述】:

我试图弄清楚它的雄辩并很难理解它,即使我试图阅读它。

我有两张桌子:fs_festivals 和 fs_bands。

fs_festivals:id、名字

fs_bands:id、festival_id、名称

所以,一个音乐节可以有多个乐队,一个乐队属于一个音乐节。

波段模型(Band.php)

class Band extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

protected $fillable = array(
    'festival_id','name','note','bandak', 'bandakinfo','created_by','updated_by'
);

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'fs_bands';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password', 'remember_token');

public function festival() {
    return $this->belongsTo('Festival');
}

}

节日模型(Festival.php):

class Festival extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

protected $fillable = array(
    'name','year','info','slug', 'image','created_by','updated_by'
);

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'fs_festivals';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password', 'remember_token');

public function bands() {
    return $this->hasMany('Band');
}

}

在我的控制器中:

    public function festivalHome() {
   $bands = Band::all();
    return View::make('fis.festivalhome')->with('bands',$bands);
}

在我看来:

Bands: 
@foreach($bands as $band)
{{ $band->name }}
@endforeach

这会列出 fs_bands 表中的所有波段。我只想列出那些设置了我正在处理的当前节日的节日 ID 的人(比如节日 ID='2')。我该怎么办?

我试过这个(看看别人做了什么),

@foreach($festival->$bands as $band)

但它给了我一个错误

未定义变量:节日

我做错了什么?我也想知道,我应该做点别的而不是 $bands = Band:all();按festival_id 列出它们?这将是一个选项,但有些东西告诉我这应该用 eloquent 自动完成。

【问题讨论】:

    标签: php laravel eloquent relationship


    【解决方案1】:

    控制器:

    public function festivalHome($id) {
          //$bands = Band::all();
          $festival = Festival::with('bands')->whereId($id)->first(); //it will load festival with all his bands 
          return View::make('fis.festivalhome')->with('festival',$festival);
    
    
          // or you can filter bands
          $bands = Band::whereHas('festival', function($query) use ($id){ 
                         $query->whereId($id);
                   })->get(); //there will be only bands which will be on the festival
    
       }
    

    在你看来:

    @foreach($festival->bands as $band)
       {{ $band->name }}
    @endforeach
    
     //or 
     @foreach($bands as $band)
       {{ $band->name }}
     @endforeach
    

    【讨论】:

    • 这给了我:未定义的属性:Illuminate\Database\Eloquent\Builder::$bands(查看:/var/www/html/fis/app/views/fis/festivalhome.blade.php)
    • 忘记 first() 方法 :) 修复
    • 那行得通。我使用了: $festival = Festival::with('bands')->whereId($festivalid)->first();然后 @foreach($festival->bands as $band) {{ $band->name }} @endforeach 非常感谢 Dmytro!
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2018-04-13
    • 2019-04-03
    • 2021-01-16
    • 2015-03-14
    • 2018-11-28
    • 2015-01-11
    • 2021-06-03
    相关资源
    最近更新 更多