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

时间:2022-10-16 00:18:55

I hope I can explain this clearly, apologies in advance if it is confusing. I have a goals table which hasOne of each of bodyGoalDescs, strengthGoalDescs and distanceGoalDescs as shown below

我希望我能清楚地解释一下,如果它令人困惑,请提前道歉。我有一个目标表,其中包含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();
    }
}

each of the three tables looks like this with the function details changed

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

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

}

a goal has either a body goal, a strength goal, or a distance goal. I am having a problem with this method in the controller function

目标有身体目标,力量目标或距离目标。我在控制器功能中遇到此方法的问题

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

    }
}

when I pass through goal 1 which has a distance goal the above method dies and dumps the Goal object with the details of goal 1 and an array of its relations including an object with DistanceGoalDes.

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

when I pass through goal 2 it passes through exactly the same as if I had passed through goal 1

当我通过球门2时,它的传球方式与我通过球门1时完全相同

if I dd() $thisgoal i get the goal that was passed through

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

what I want ultimately is a method that returns the goal object with its relevant goal description object to the view but this wont even show me the correct goal details not too mind with the correct relations

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

1 个解决方案

#1


0  

this function is now doing what I want it to do, I am sure there is a better way (besides the fact that its happening in the controller right now) and I would love to hear it.

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

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

    }

#1


0  

this function is now doing what I want it to do, I am sure there is a better way (besides the fact that its happening in the controller right now) and I would love to hear it.

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

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

    }