0
我有一個Users表,一個Images表和一個Bookmarks表。我已經爲我的數據庫中的每個人指定了關係,並且我可以訪問用戶上傳的所有圖片。不過,我無法訪問他們已添加書籤的圖片。
這是我的數據庫中的關係是這樣的:
我能訪問我想使用上的phpMyAdmin的SQL輸入數據,但是我不能使用Laravel時:
使用Laravel產量這個錯誤:
這是我的用戶模型:
<?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');
}
}
這是我的圖片型號:
<?php
class Image extends Eloquent {
public function poster() {
return $this->belongsTo('user', 'poster_id', 'id');
}
}
這是我的書籤模型:
<?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');
}
}
這是在其中拋出的錯誤觀點:
@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('[email protected]', $user->id) }}">Uploaded Images</a>
</li>
<li class="active">
<a href="{{ URL::action('[email protected]', $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('[email protected]', $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
是的。該函數本身獲取關係。該屬性獲取關係的結果。 +1。 – ceejayoz 2014-11-25 14:49:02