Laravel表单:从空值创建默认对象

时间:2014-08-30 20:56:10

标签: object laravel

我正在创建一个表单来更新我的项目,但是我收到此错误从空值创建默认对象

这是我的路线:

Route::get('admin/projects/edit/{id}','ProjectsController@edit');
Route::put('admin/projects/update/{id}','ProjectsController@update');

编辑视图:

    @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-&gt; thumbnail ='img /'.$ img。'';

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

您使用的是$post->thumbnail = 'img/'.$img.'';,但您没有创建$post个对象。相反,您可以使用此代码中的$project对象:

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

代码中的$post在哪里?它应该是$project并且还要确保你是否有一个对象,使用这样的东西:

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

看来,$post->thumbnail方法$project->thumbnail应为update