控制者和雄辩的困境(Controller and Eloquent woes)

编程入门 行业动态 更新时间:2024-10-12 03:27:36
控制者和雄辩的困境(Controller and Eloquent woes)

我是Laravel的新手,已经在这个问题上喋喋不休了一段时间。 我只是想从表中获取一行并将其移交给视图。

该表有两个应与此查询匹配的字段,第一个当然是id,第二个是与登录用户匹配的user_id。 当它完成时,我计划用失败消息进行响应(没有找到匹配id:X的记录)。

public function show($id) { $data = Partners::where('id', $id);->where('user_id', Auth::user()->id)->get(); return View::make('partners.showone') ->with('data', $data) ->with('title', 'View Record') ->with('breadcrumb', 'View Partner'); }

在我看来:

{{ $data->firstName }}

这个配置给我一个错误:

未定义属性:Illuminate \ Database \ Eloquent \ Builder :: $ firstName

I am new to Laravel and have been banging my head on this issue for a while. I am merely trying to get a single row from a table and hand it off to a view.

The table has two fields that should match up with this query, the first of course is the id and the second is user_id which matches the logged in user. When its done I plan on responding with a failure message (no record found that matches id: X).

public function show($id) { $data = Partners::where('id', $id);->where('user_id', Auth::user()->id)->get(); return View::make('partners.showone') ->with('data', $data) ->with('title', 'View Record') ->with('breadcrumb', 'View Partner'); }

In my view:

{{ $data->firstName }}

This configuration gives me an error of:

Undefined property: Illuminate\Database\Eloquent\Builder::$firstName

最满意答案

错字......你有一个; 在那里......应该......

$data = Partners::where('id', $id)->where('user_id', Auth::user()->id)->get();

但你的查询真的应该是这个..

$data = Partners::find($id)->where('user_id', Auth::user()->id)->first();

如果你只想要一排

编辑(根据您的评论)

对于您的合作伙伴模型中的手机(我正在使用Laravel 3 btw)

function phone(){ return $this->hasMany('Phone'); }

然后使用上面的$ data查询,你可以通过...获取手机

$data->phone();

Typo...you have a ; in the middle there....it should be....

$data = Partners::where('id', $id)->where('user_id', Auth::user()->id)->get();

but your query really should be this..

$data = Partners::find($id)->where('user_id', Auth::user()->id)->first();

if you just want one row

EDIT (based on your comments below)

For your phones in your Partner model (Im working with Laravel 3 btw)

function phone(){ return $this->hasMany('Phone'); }

Then with the above $data query, you can get the phone by...

$data->phone();

更多推荐

本文发布于:2023-07-15 16:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1115697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制者   雄辩   困境   woes   Eloquent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!