在laravel 5.4中从空值创建默认对象

I try to let my users update their ads and after click on update button i will get this error:

ErrorException in AdController.php line 108:
Creating default object from empty value

my AdController line 108 is:

$ad->company_id = Input::get('company_id');

And here is the refer line to that code in my form: as you can see company_id field is not empty and will pass company id number to that field which is the same as store method and works perfectly in there but for update method returns this error, also when I check my page source codes there is correct company id and isn't empty.

{!! Form::model($ads, ['route' => ['company.update', $ads->id], 'method' => "PUT", 'files' => true]) !!}
<input type="hidden" name="company_id" value ="@foreach ($companies as $company){{ $company->id }}@endforeach">

//the rest of the codes

{!! Form::close() !!}

any idea why is that?

Fixed it!

The issue was

$ad = Ad::find($id);

line before line 108 which was

$ads = Ad::find($id);

before

I think you should try this:

$ad = Ad::where('id',$id)->first();
$ad->company_id = Input::get('company_id');

Hope this work for you!