2014-07-21 1 views
0

나는 PHP의 새로운 문서입니다. 나는 laravel로 웹 사이트를 만들고있다. 나는 게시물 작성시 업로드 이미지에 포스트 번호를 추가 할 laravel의 이미지 이름에 게시물 ID 번호를 추가하는 방법 4

내 스토어 컨트롤러 : 가능

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(); 
    } 

인가? 도와주세요.

감사 사이 풀

+0

$ 파일 이름 변수에 – saiful408

답변

0

당신은 먼저 현재 저장 한 후 ID를 얻을 만하면 이미지 파일 이름에 포스트 ID를 추가 할 수 있습니다, DB에 게시물을 저장해야합니다.

작업 할 수있는 코드를 다음과 같이

뭔가 :

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(); 
} 
+0

가 대단히 감사합니다 @ SUB0DH – saiful408

관련 문제