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.
我有一个Users表,一个Images表和一个Bookmarks表。我已经在我的数据库中指定了每个关系,我能够访问用户上传的所有图像。但是,我无法访问他们已添加书签的图像。
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
1 个解决方案
#1
11
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 to $ bookmark-> image-> id。
#1
11
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 to $ bookmark-> image-> id。