Pierre Ftn
Pierre Ftn

Reputation: 321

Laravel Form : Creating default object from empty value

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.'';

Thank You for your help

Upvotes: 0

Views: 6268

Answers (1)

The Alpha
The Alpha

Reputation: 146191

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

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

Where is $post in your code ? It should be $project and also make sure if you got an object, using something like this:

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

It looks that, $post->thumbnail should be $project->thumbnail in your update method.

Upvotes: 3

Related Questions