Laravel Eloquent HasOne::$id 未定义属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27128252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent HasOne::$id Undefined Property
提问by Liam Potter
I have a Users table, a Images table and a Bookmarks table. I've specified the relationships for each one in my database and I am capable of accessing all the images that a user has uploaded. However I cannot access the images they have bookmarked.
我有一个用户表、一个图像表和一个书签表。我已经为我的数据库中的每一个指定了关系,并且我能够访问用户上传的所有图像。但是我无法访问他们已添加书签的图像。
This is what my databases relationships look like:
这是我的数据库关系的样子:
I am able to access the data I want using the SQL input on PHPMyAdmin however I cannot when using Laravel:
我可以使用 PHPMyAdmin 上的 SQL 输入访问我想要的数据,但是在使用 Laravel 时我不能:
Using Laravel yields this error:
使用 Laravel 会产生此错误:
This is my User Model:
这是我的用户模型:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
class User extends SentryUserModel implements UserInterface, RemindableInterface {
public $activated = true;
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
public function getRememberToken() {
return $this->remember_token;
}
public function setRememberToken($value) {
$this->remember_token = $value;
}
public function getRememberTokenName() {
return 'remember_token';
}
public function getReminderEmail() {
return $this->email;
}
public function images() {
return $this->hasMany('image', 'poster_id', 'id');
}
public function bookmarks() {
return $this->hasMany('bookmark', 'bookmarker_id', 'id');
}
}
This is my Image Model:
这是我的图像模型:
<?php
class Image extends Eloquent {
public function poster() {
return $this->belongsTo('user', 'poster_id', 'id');
}
}
This is my Bookmark Model:
这是我的书签模型:
<?php
class Bookmark extends Eloquent {
public $timestamps = false;
public function bookmarker() {
return $this->belongsTo('user', 'bookmarker_id', 'id');
}
public function image() {
return $this->hasOne('image', 'image_id', 'id');
}
}
This is the view in which the error is being thrown:
这是引发错误的视图:
@extends('master')
@section('content')
<!-- Page Content -->
<header class="navbar navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/" class="navbar-brand">Egami</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
@if(!Sentry::check())
<li>
<a href="#" data-toggle="modal" data-target="#login-modal">Login</a>
</li>
<li>
<a href="#" data-toggle="modal" data-target="#signup-modal">Signup</a>
</li>
@endif
@if(Sentry::check())
@if(Sentry::getUser()->id == $user->id)
<li class="active">
@else
<li>
@endif
<a href="/profile/{{ Sentry::getUser()->id }}">Profile</a>
</li>
<li>
<a href="/logout">Logout</a>
</li>
@endif
</ul>
</div>
</div>
</header>
<div class="container" id="profile">
<div class="row">
<div class="tac col-md-3">
<img src="/img/{{ $user->profile_image }}" alt="{{ $user->username }}'s Profile Image">
</div>
<div class="tal col-md-8">
<h2>{{ $user->username }}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="navbar navbar-inverse" role="navigation">
<ul class="nav navbar-nav navbar-center">
<li>
<a href="{{ URL::action('ProfileImageController@show', $user->id) }}">Uploaded Images</a>
</li>
<li class="active">
<a href="{{ URL::action('ProfileBookmarkController@show', $user->id) }}">Bookmarked Images</a>
</li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="row">
<?php
$bookmarks = $user->bookmarks()->orderBy('id', 'DESC')->simplePaginate(15);
?>
@foreach($bookmarks as $bookmark)
<div class="col-md-4 user-image">
<a href="{{ URL::action('ImageController@show', $bookmark->image()->id) }}">
<img src="/uimg/{{ $bookmark->image()->id }}.png" alt="{{ $bookmark->image()->title }}">
</a>
</div>
@endforeach
</div>
<div class="row">
<div class="container">
{{ $bookmarks->links() }}
</div>
</div>
</div>
</div>
</div>
<footer class="navbar-fixed-bottom">
<div class="container">
<p>
Copyright © Egami {{ date('Y') }}
</p>
</div>
</footer>
@stop
@section('scripts')
@stop
回答by Robbie
Try this:
尝试这个:
<a href="{{ URL::action('ImageController@show', $bookmark->image->id) }}">
<img src="/uimg/{{ $bookmark->image->id }}.png" alt="{{ $bookmark->image->title }}">
</a>
Removed parentheses $bookmark->image()->id to $bookmark->image->id.
删除括号 $bookmark->image()->id 到 $bookmark->image->id。