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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25586689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:01:38  来源:igfitidea点击:

Laravel Form : Creating default object from empty value

objectlaravel

提问by Pierre Ftn

i'm creating a form to update my project but i get this error Creating default object from empty value

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

Here is my route :

这是我的路线:

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

The edit view :

编辑视图:

    @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

And my controller

还有我的控制器

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

I Dont really understand where the errors is , the problem come from this line according to laravel debuger : $post->thumbnail = 'img/'.$img.'';

我真的不明白错误在哪里,根据laravel调试器,问题来自这一行:$post->thumbnail = 'img/'.$img.'';

Thank You for your help

感谢您的帮助

回答by The Alpha

You are using $post->thumbnail = 'img/'.$img.'';but you didn't create a $postobject. Instead you have $projectobject from this code:

您正在使用$post->thumbnail = 'img/'.$img.'';但没有创建$post对象。相反,您有$project来自此代码的对象:

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

Where is $postin your code ? It should be $projectand also make sure if you got an object, using something like this:

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

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

It looks that, $post->thumbnailshould be $project->thumbnailin your updatemethod.

看起来,$post->thumbnail应该$project->thumbnail在你的update方法中。