Php 如何在laravel 4中向图像名称添加帖子id号
我是php新手。我正在与拉威尔建立一个网站。我想把帖子号添加到创建帖子时上传的图片中 我的店铺管理员:Php 如何在laravel 4中向图像名称添加帖子id号,php,laravel-4,eloquent,Php,Laravel 4,Eloquent,我是php新手。我正在与拉威尔建立一个网站。我想把帖子号添加到创建帖子时上传的图片中 我的店铺管理员: public function store() { $validator = Validator::make(Input::all(), Post::$rules); if ($validator->passes()) { $post = new Post; $post->title = In
public function store()
{
$validator = Validator::make(Input::all(), Post::$rules);
if ($validator->passes()) {
$post = new Post;
$post->title = Input::get('title');
$post->body = Input::get('body');
$post->reporter = Input::get('reporter');
$post->meta = Input::get('meta');
$post->slug = Input::get('title');
$post->top = Input::get('top');
$post->pubdate = Input::get('pubdate');
$image = Input::file('image');
if ($image) {
$filename = "image274".$image->getClientOriginalExtension();
Image::make($image->getRealPath())->resize(250, 145)->save('public/images/postimages/'.$filename);
$post->image = 'images/postimages/'.$filename;
}
$categories = Input::get('categories');
$post->save();
$post->categories()->sync($categories);
return Redirect::route('admin.posts.index')
->with('message', 'Product Created');
}
return Redirect::back()
->with('message', 'Something went wrong')
->withErrors($validator)
->withInput();
}
可能吗?请帮帮我
谢谢
Saiful您必须首先将帖子保存到DB,然后获取当前保存的帖子id,然后只有您可以将帖子id添加到图像文件名中 类似于以下代码的代码可能会起作用:
public function store()
{
$validator = Validator::make(Input::all(), Post::$rules);
if ($validator->passes()) {
$post = new Post;
$post->title = Input::get('title');
$post->body = Input::get('body');
$post->reporter = Input::get('reporter');
$post->meta = Input::get('meta');
$post->slug = Input::get('title');
$post->top = Input::get('top');
$post->pubdate = Input::get('pubdate');
$post->save();
$image = Input::file('image');
if ($image) {
$filename = "image274".$post->id.$image->getClientOriginalExtension();
Image::make($image->getRealPath())->resize(250, 145)->save('public/images/postimages/'.$filename);
$post->image = 'images/postimages/'.$filename;
$post->save();
}
$categories = Input::get('categories');
$post->categories()->sync($categories);
return Redirect::route('admin.posts.index')
->with('message', 'Product Created');
}
return Redirect::back()
->with('message', 'Something went wrong')
->withErrors($validator)
->withInput();
}
$filename variable非常感谢@SUB0DH