dongliao2011 2014-08-22 16:13
浏览 33
已采纳

使用雄辩的模型来解决视图和控制器问题

I am using two eloquent models alpha and beta. I am having trouble with the controller and the view. Alpha is designed to have multiple betas and their is a hasMany relationship established. When redirected to the show page they should see the alpha and the beta data by alpha_id. The code I am currently using is pulling an error and after looking in the book and online I can not figure out how to fix it.

Here is the controller code:

public function display($alpha_id)
{
    return View::make('alpha.show')->with([
        'alpha' => $this->alphaRepository->findBy($alpha_id),
        $alpha->alphaRecord->getBeta(),
    ]);
}

Here is the view:

<p>{{ $alpha }}</p>

@foreach($alpha->Betas() as $beta)
<p>Beta: <br />{{ $beta }}</p>
@endforeach
  • 写回答

1条回答 默认 最新

  • dongyangben6144 2014-08-22 16:49
    关注

    The problem is how you call the with method, try to set the $alpha variable first:

    public function display($alpha_id)
    {
        $alpha = $this->alphaRepository->findBy($alpha_id);
        return View::make('alpha.show')->with('alpha',$alpha);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?