访问对象及其在laravel中的关系4.1

时间:2013-12-23 14:42:47

标签: php laravel laravel-4

我希望我能清楚地解释清楚,如果它令人困惑,请提前道歉。我有一个目标表,其中包含bodyGoalDescs,strengthGoalDescs和distanceGoalDescs中的每一个,如下所示

goals.php     

class Goal extends BaseModel
{
    protected $guarded = array();

    public static $rules = array();

    //define relationships

    public function user()
    {
        return $this->belongsTo('User', 'id', 'userId');
    }

    public function goalStatus()
    {
        return $this->hasOne('GoalStatus', 'id', 'goalStatus');
    }

    public function bodyGoalDesc()
    {
        return $this->hasOne('BodyGoalDesc', 'id', 'bodyGoalId');
    }

    public function distanceGoalDesc()
    {
        return $this->hasOne('DistanceGoalDesc', 'id', 'distanceGoalId');
    }

    public function strengthGoalDesc()
    {
        return $this->hasOne('StrengthGoalDesc', 'id', 'strengthGoalId');
    }

    //goal specific functions

    public static function yourGoals()
    {
        return static::where('userId', '=', Auth::user()->id)->paginate();
    }
}

这三个表中的每一个都看起来像这样,功能细节已经改变

class BodyGoalDesc extends BaseModel
{
    protected $guarded = array();

    public static $rules = array();

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

    //define relationships
    public function goal()
    {
        return $this->belongsTo('Goal', 'bodyGoalId', 'id');
    }

}

目标有身体目标,力量目标或距离目标。我在控制器函数

中遇到此方法的问题
<?php

class GoalsController extends BaseController
{
    protected $goal;

    public function __construct(Goal $goal)
    {
        $this->goal = $goal;
    }

    /**
     * Display the specified resource.
     *
     * @param  int      $id
     * @return Response
     */
    public function show($id)
    {
        $thisgoal = $this->goal->find($id);

        foreach ($this->goal->with('distanceGoalDesc')->get() as $distancegoaldesc) {
            dd($distancegoaldesc->DistanceGoalDesc);
        }

    }
}

当我通过具有距离目标的目标1时,上述方法终止,并使用目标1的细节及其关系数组(包括具有DistanceGoalDes的对象)转储Goal对象。

当我通过目标2时,它的传递方式与我通过目标1的情况完全相同

如果我dd()$ thisgoal我得到了通过

的目标

我最终想要的是一个方法,它将目标对象及其相关的目标描述对象返回到视图,但这甚至不会向我显示正确的目标细节,而不是正确的关系

1 个答案:

答案 0 :(得分:0)

这个功能现在正在做我想做的事情,我确信有更好的方法(除了它现在在控制器中发生的事实),我很乐意听到它。

public function show($id)
    {
        $thisgoal = $this->goal->find($id);

        if (!$thisgoal->bodyGoalDesc == null) {
            $goaldesc = $thisgoal->bodyGoalDesc;

            return View::make('goals.show')
                ->with('goal', $thisgoal)
                ->with('bodygoaldesc', $goaldesc);
        } elseif (!$thisgoal->strengthGoalDesc == null) {
            $goaldesc = $thisgoal->strengthGoalDesc;

            return View::make('goals.show')
                ->with('goal', $thisgoal)
                ->with('strengthgoaldesc', $goaldesc);
        } elseif (!$thisgoal->distanceGoalDesc == null) {
            $goaldesc = $thisgoal->distanceGoalDesc;

            return View::make('goals.show')
                ->with('goal', $thisgoal)
                ->with('distancegoaldesc', $goaldesc);
        }

    }