2014-08-30 212 views
0

我創建一個表單來更新我的項目,但我得到這個錯誤從空值Laravel形式:從空值創建默認對象

這裏創建默認的對象是我的路線:

Route::get('admin/projects/edit/{id}','[email protected]'); 
Route::put('admin/projects/update/{id}','[email protected]'); 

的編輯觀點:

@extends ('layout.admin') 

@section ('content') 

{{ Form::open(array('url'=>'admin/projects/update/'.$project->id.'','enctype'=>'multipart/form-data','method'=>'put','style'=>'margin:0!important;')) }} 
    {{ Form::token() }} 
<div class="form-group"> 
    {{ Form::label('name','Nom :') }} 
    {{ Form::text('name',$project->name,array('class'=>'form-control')) }} 
    @if($errors->first('name')) 
    <div class="alert alert-danger" role="alert">{{ $errors->first('name') }}</div> 
    @endif 
</div> 
<div class="form-group"> 
    {{ Form::label('slug','Slug :') }} 
    {{ Form::text('slug',$project->slug,array('class'=>'form-control')) }} 
    @if($errors->first('slug')) 
    <div class="alert alert-danger" role="alert">{{ $errors->first('slug') }}</div> 
    @endif 
</div> 

<div class="form-group"> 
    {{ Form::label('category_id','Category :') }} 
    {{ Form::select('category_id',$cats,$project->category_id) }} 
</div> 
<div class="form-group"> 
    {{ Form::label('thumbnail','Image :') }} 
    {{ Form::file('thumbnail',array('class'=>'form-control')) }} 
    @if($errors->first('thumbnail')) 
    <div class="alert alert-danger" role="alert">{{ $errors->first('thumbnail') }}</div> 
    @endif 
</div> 

<div class="form-group"> 
    {{ Form::label('description','Description :') }} 
    {{ Form::textarea('description',$project->description,array('class'=>'form-control')) }} 
    @if($errors->first('description')) 
    <div class="alert alert-danger" role="alert">{{ $errors->first('description') }}</div> 
    @endif 
</div> 

{{ Form::submit('Modifier', array('class'=>'btn btn-primary','style'=>'float:left')) }} 

{{ Form::close() }} 

<a href="{{ URL::to('admin/projects/delete/'.$project->id.'') }}"><button class="btn btn-danger" style="float:left;margin-left:20px;">Supprimer</button></a> 
@stop 

我的控制器

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('admin/projects/edit/'.Input::get('id').'')->withInput()->withError(); 
    } 
    $project=Project::find($id); 
    $project->name = Input::get('name'); 
    $project->slug = Slug::create(Input::get('slug')); 
    $project->category_id = Input::get('category_id'); 
    $project->description = Input::get('description'); 

    if(Input::hasFile('thumbnail')){ 
     $img = Str::slug(Input::get('name').'png'); 
     $post->thumbnail = 'img/'.$img.''; 
    } 
    if(Input::hasFile('thumbnail')){ 
     Input::file('thumbnail')->move('img/',$img); 
    } 

    return Redirect::to('admin/projects')->with('Alert_succes','Votre projet a bien été mis à jour'); 

} 


public function edit($id) 
{ 
    $project = Project::find($id); 
    $categories = Category::all(); 
    $cats = array(); 
    foreach ($categories as $category){ 
     $cats[$category->id] = $category->name; 
    } 
    return View::make('projects.edit')->with(array('project'=>$project,'cats'=>$cats)); 
} 

我真的不明白錯誤在哪裏,根據laravel debuger,這個問題來自這一行:$ post-> thumbnail ='img /'.$ img。'';

感謝你的幫助

回答

3

您正在使用$post->thumbnail = 'img/'.$img.'';但你沒有創建一個$post對象。相反,你必須$project對象從這個代碼:

$project = Project::find($id); 

哪裏是在你的代碼$post?它應該是$project,並且確保如果你有一個對象,使用這樣的事情:

$project = Project::find($id); 
if($project) { 
    //... 
} 

它看起來是,$post->thumbnail應該在你update方法$project->thumbnail