Hy all,
我需要更新文章的图片,所有这些都可以更新帖子,但我的问题是如何使用我的多态关系更新此帖子的图片。
我有一张带有imageable_id imageable_type路径的图片。
在我的Post控制器更新功能中
public function update($id)
{
$inputs = Input::all();
$rules =array(
'name'=>'required|min:5',
'description'=>'required|min:10',
);
$validation = Validator::make($inputs,$rules);
if($validation->fails()){
return Redirect::to('posts/'.Input::get('id').'/edit')->withInput()
->withErrors($validation)
->with('alert_error','Veuillez corriger les érreurs SVP');
}
if(Input::hasFile('mediafile')){
$file = Input::file('mediafile');
$name = date('Ymd') . '-' . $file->getClientOriginalName();
$file = $file->move('img/uploads/', $name);
$path = $file->getRealPath();
//take the path only at the img not totally
$pos = strpos($path,'/img/');
if ($pos !== false) {
$path = substr($path, $pos + 1);
}
$input['file'] = $path;
$post=Post::find(Input::get('id'));
$post->name = e(Input::get('name'));
$post->description = e(Input::get('description'));
$post->category_id = e(Input::get('categories'));
$post->tag_id = e(Input::get('tag'));
$post->rating = e(Input::get('rating'));
$post->content = Input::get('content');
$post->is_online = e(Input::get('online'));
$post->images->path = e(Input::get('mediafile'));
$post->save();
}
}
我的模特帖子
public function images(){
return $this->morphMany('Image','imageable');
}
因此,当我提交表单以显示帖子的新图片时,我希望仅更新我的表格中图像的路径。
感谢帮助
答案 0 :(得分:0)
morphMany将返回一组图像,因此当您运行$ post-> images->路径时,您需要在图像模型上设置集合上的路径属性。如果Post仅用于拥有一个图像,请将其更改为morphOne,或者您必须从该集合中识别要更新的图像模型。