在laravel中调用未定义的方法Illuminate \ Database \ Query \ Builder

时间:2014-02-02 13:22:15

标签: php laravel laravel-4 eloquent query-builder

我设置了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()

有谁能告诉我为什么会出现这个错误以及我在做什么来创建这个错误?

谢谢,

1 个答案:

答案 0 :(得分:3)

看起来你必须把你的

use Codesleeve\Stapler\Stapler;

也进入了PostImage模型。 Eloquent关系仅适用于数据库,不会扩展您尝试访问的其他Stapler类。