dongzj2015 2015-12-01 14:28
浏览 19
已采纳

Laravel关系2层

I have my database (=model) structure like that:

game:  
    lot (typeof Lot)
    places (array type of Place)
         place_id // just a number of a lot in some game
         user_id

What should I do to call in everywhere like this:

User::find(1)->games() // returns Game collection where user has places

?

Models are:

class Place extends Model
{
    protected $fillable = ['place_id', 'user_id', 'game_id'];

    public function user() {
        return $this->belongsTo(User::class);
    }

    public function game() {
        return $this->belongsTo(Game::class);
    }
}

User:

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'steam_id', 'avatar'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['remember_token'];

    /**
     * Get all of the tasks for the user.
     */
    public function items()
    {
        return $this->hasMany(SteamItem::class);
    }

    public function places() {
        return $this->hasMany(Place::class);
    }
}

The Game:

class Game extends Model
{
    protected $fillable = ['lot_id'];

    public function lot() {
        return $this->belongsTo(Lot::class);
    }

    public function places() {
        return $this->hasMany(Place::class);
    }
}

Now I use this code in my User class:

public function games() {
    return Game::with(['places' => function ($query) {
        $query->where('user_id', $this->id);
    }]);;
}

It doesn't work, because I need to make it as a relationship method, but with method returns a query builder.

In the finals I must call $user->games and it should return me all the games user linked to through place.

  • 写回答

1条回答 默认 最新

  • dpca4790 2015-12-01 16:46
    关注

    Okay. I think I understand now.

    User has many Place. Place belongs to User. Place belongs to Game. Game has many Place.

    You can try this:

    $user = User::with('places.game.lot')->find(1);
    

    This will fetch the User and eager load all the relationships. Because Place belongsTo a Game, which in turn belongs to Lot, you can then do this:

    @foreach ($user->places as $place)
        <img src="{{$place->game->lot->imageUrl}}" />
    @endforeach
    

    Also, place is actually a pivot table, and you can take advantage of Eloquent's many-to-many relationship, which I would recommend reading about.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大