It looks like you have to put your
use Codesleeve\Stapler\Stapler;
into the PostImage model too. The Eloquent relationship is just simply for the database and does not extend the additional Stapler class that you are trying to access.
質問
I have 2 models set up and they both need a relationship setup so they can interact with each other etc like any other relationship method. I have the files set up like so:
PostImage.php model:
<?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 model:
<?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 function:
/**
* 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.');
}
In my view for the create I have a form that basically asks for the images like so:
{{ 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() }}
When it comes to submitting the form to create the post I get this error below:
Call to undefined method Illuminate\Database\Query\Builder::hasAttachedFile()
Can anyone tell me why this would give that error and what I am doing to create this error at all?
Thanks,
解決
It looks like you have to put your
use Codesleeve\Stapler\Stapler;
into the PostImage model too. The Eloquent relationship is just simply for the database and does not extend the additional Stapler class that you are trying to access.