我是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();更多推荐
发布评论