Вызов неопределенного метода Illuminate\Database\Query\Builder в laravel

У меня настроены 2 модели, и им обеим нужна настройка отношений, чтобы они могли взаимодействовать друг с другом и т. Д., Как и любой другой метод отношений. У меня файлы настроены так:

Модель PostImage.php:

<?php

class PostImage extends Eloquent {
    protected $guarded = array();

    public static $rules = array();

    public function post(){
        return $this->belongsTo('Post');
    }

    public function __construct(array $attributes = array()) {
    // Profile pictures have an attached file (we'll call it photo).
    $this->hasAttachedFile('image', [
        'styles' => [
            'thumbnail' => '100x100#'
        ]
    ]);

    parent::__construct($attributes);
    }

}

Модель Post.php:

<?php

class Post extends Eloquent {
    use Codesleeve\Stapler\Stapler;
    protected $guarded = array();

    public static $rules = array(
        'title' => 'required',
        'body' => 'required'
    );

    public function postImages()
    {
        return $this->hasMany('PostImage');
    }

    public function __construct(array $attributes = array()) {
    $this->hasAttachedFile('picture', [
        'styles' => [
            'thumbnail' => '100x100',
            'large' => '300x300'
        ],
        // 'url' => '/system/:attachment/:id_partition/:style/:filename',
        'default_url' => '/:attachment/:style/missing.jpg'
    ]);

    parent::__construct($attributes);
    }

}

Функция хранилища PostsController.php:

/**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $post = new Post(Input::get());
        $post = Post::create(['picture' => Input::file('picture')]);
        $post->save();

        foreach(Input::file('images') as $image)
        {
            $postImage = new PostImage();             // (1)
            $postImage->image = $image;                    // (2)
            $post->postImages()->save($postImage);    // (3)
        }

        return Redirect::route('posts.create')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

На мой взгляд, для создания у меня есть форма, которая в основном запрашивает изображения следующим образом:

{{ Form::open(array('route' => 'posts.store', 'files' => true)) }}
    <ul>
        <li>
            {{ Form::label('title', 'Title:') }}
            {{ Form::text('title') }}
        </li>

        <li>
            {{ Form::label('body', 'Body:') }}
            {{ Form::textarea('body') }}
        </li>

        <li>
            {{ Form::file('picture') }}
        </li>
        <li>
            {{ Form::file( 'images[]', ['multiple' => true] ) }}
        </li>

        <li>
            {{ Form::submit('Submit', array('class' => 'btn btn-info')) }}

    </ul>
{{ Form::close() }}

Когда дело доходит до отправки формы для создания сообщения, я получаю следующую ошибку:

Call to undefined method Illuminate\Database\Query\Builder::hasAttachedFile()

Может ли кто-нибудь сказать мне, почему это может привести к этой ошибке и что я вообще делаю, чтобы создать эту ошибку?

Спасибо,


person M dunbavan    schedule 02.02.2014    source источник


Ответы (1)


Похоже, вы должны поставить свой

use Codesleeve\Stapler\Stapler;

в модель PostImage тоже. Отношение Eloquent просто для базы данных и не расширяет дополнительный класс Stapler, к которому вы пытаетесь получить доступ.

person Chris G    schedule 02.02.2014
comment
да, это странно, когда я поместил свой код в переполнение стека, я сразу заметил ошибку... я слишком долго смотрел на это - person M dunbavan; 02.02.2014