Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent HasOne::$id Undefined Property

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: Database

I am able to access the data I want using the SQL input on PHPMyAdmin however I cannot when using Laravel: SQL

Using Laravel yields this error: Error

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 &copy; Egami {{ date('Y') }}
        </p>
    </div>
</footer>
@stop

@section('scripts')

@stop
like image 332
Liam Potter Avatar asked Nov 25 '14 13:11

Liam Potter


1 Answers

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.

like image 99
Robbie Avatar answered Oct 25 '22 03:10

Robbie