Edward Palen Edward Palen - 16 days ago 10
MySQL Question

ErrorException in ClientController.php line 120: Attempt to assign property of non-object Laravel 5.2

I am completing a form to edit clients, it must attach an image using intervention image and it is generating the following error:


ErrorException in ClientController.php line 120: Attempt to assign
property of non-object.


This is my model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Client extends Model
{
protected $table = 'clients';

protected $fillable = ['status_id', 'name', 'identity', 'address', 'location', 'l_number', 'province_id', 'email', 'email2', 'phone', 'movil', 'requirement_id', 'budget', 'user_id', 'comments'];

// Relation with Status client
public function statusclient()
{
return $this->belongsTo('App\Statusclient', 'status_id');
}

// Relation with Province
public function province()
{
return $this->belongsTo('App\Province');
}

// Relation with Province
public function requirement()
{
return $this->belongsTo('App\Requirement');
}

// Relation with User
public function user()
{
return $this->belongsTo('App\User');
}
}


This is my update method:

public function update(Request $request, $id)
{
$updated = Client::findorFail($id);

$this->validate($request, [
'name' => 'required'
]);

$clients = $request->all();

$updated->fill($clients)->save();

if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 200)->save( public_path('uploads/clients/' . $filename ) );

$clients->avatar = $filename;
$clients->save();
}

return redirect()->route('clients.index')
->with('success','Cliente actualizado satisfactoriamente!!!');
}


This is the form:

{!! Form::model($clients, ['method' => 'PATCH','route' => ['clients.update', $clients->id], 'enctype' => 'multipart/form-data']) !!}

<div class="row">

<div class="col-md-4 col-xs-4">

<div class="form-group">
<strong>Estado cliente:</strong>
{!! Form::select('status_id', $statusclients, null, ['class' => 'form-control']) !!}
</div>

<div class="form-group">
<strong>Nombre:</strong>
{!! Form::text('name', null, array('placeholder' => 'Nombres o razón social','class' => 'form-control')) !!}
</div>

<div class="form-group">
<strong>Documento de identidad:</strong>
{!! Form::text('identity', null, array('placeholder' => 'Cedula, pasaporte o RNC...','class' => 'form-control')) !!}
</div>

<!-- left column -->
<div class="form-group">
<div class="text-center">
<a href="#" class="thumbnail">
<img src="{{ URL::asset('uploads/clients/' . $clients->avatar) }}" class="avatar img-thumbnail" alt="avatar" style="height: 180px; width: 100%;">
<h6>Cambia la imagen del documento de identidad del cliente...</h6>
<input type="file" name="avatar" class="text-center center-block well well-sm" style="width: 100%;">
</a>
</div>
</div>

</div>

<div class="col-md-4 col-xs-4">

<div class="form-group">
<strong>Dirección:</strong>
{!! Form::text('address', null, array('placeholder' => 'Dirección','class' => 'form-control')) !!}
</div>

<div class="form-group">
<strong>Sector:</strong>
{!! Form::text('location', null, array('placeholder' => 'Localidad','class' => 'form-control')) !!}
</div>

<div class="form-group">
<strong>Número:</strong>
{!! Form::text('l_number', null, array('placeholder' => 'Número propiedad','class' => 'form-control')) !!}
</div>

<div class="form-group">
<strong>Provincia:</strong>
{!! Form::select('province_id', $provinces, null, ['class' => 'form-control']) !!}
</div>

<div class="form-group">
<strong>Email:</strong>
{!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
</div>

<div class="form-group">
<strong>Email secundario:</strong>
{!! Form::text('email2', null, array('placeholder' => 'Email secundario','class' => 'form-control')) !!}
</div>

<div class="row">

<div class="col-md-6">

<div class="form-group">
<strong>Teléfono:</strong>
{!! Form::text('phone', null, array('placeholder' => 'Número de teléfono','class' => 'form-control')) !!}
</div>

</div>

<div class="col-md-6">

<div class="form-group">
<strong>Movil:</strong>
{!! Form::text('movil', null, array('placeholder' => 'Número de movil','class' => 'form-control')) !!}
</div>

</div>

</div>

</div>

<div class="col-md-4 col-xs-4">

<div class="form-group">
<strong>Necesidad del cliente:</strong>
{!! Form::select('requirement_id', $requirements, null, ['class' => 'form-control']) !!}
</div>

<div class="form-group">
<strong>Presupuesto:</strong>
{!! Form::text('budget', null, array('placeholder' => 'Presupuesto del cliente','class' => 'form-control')) !!}
</div>
<div class="form-group">
<strong>Gestor:</strong>
{!! Form::select('user_id', $users, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
<strong>Comentarios:</strong>
{!! Form::textarea('comments', null, array('placeholder' => 'Comentar sobre algun detalle sobre el cliente','class' => 'form-control', 'size' => '30x5')) !!}
</div>
</div>

<div class="form-group col-md-4 col-xs-4">
<a class="btn btn-warning" href="{{ route('clients.index') }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Retornar">
<i class="material-icons">arrow_back</i>
</a>

<button type="submit" class="btn btn-success" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Actualizar">
<i class="material-icons">cached</i>
</button>
</div>
</div>
{!! Form::close() !!}


This is the stack of errors in the browser:

1. in ClientController.php line 120
2. at HandleExceptions->handleError('2', 'Attempt to assign property of non-object', 'C:\xampp\htdocs\zizaco_entrust\app\Http\Controllers\ClientController.php', '120', array('request' => object(Request), 'id' => '2', 'updated' => object(Client), 'clients' => array('_method' => 'PATCH', '_token' => 'Be325FU0P1utedgMUR1uR91QXQXoHsYFrlCCrXDa', 'status_id' => '2', 'name' => 'Jenny Lopez', 'identity' => '00010076966', 'address' => 'Calle 3ra, Esq. 9, Tropical del Este, Las Americas', 'location' => 'Tropical del Este, Las Americas', 'l_number' => '20', 'province_id' => '31', 'email' => 'jennylopez@gmail.com', 'email2' => 'jennylopez@outlook.com', 'phone' => '8095678910', 'movil' => '8298662101', 'requirement_id' => '1', 'budget' => '10000', 'user_id' => '2', 'comments' => 'Interesado en apartamento de 2 habitaciones, en la zona colonial. ', 'avatar' => object(UploadedFile)), 'avatar' => object(UploadedFile), 'filename' => '1479867405.jpg')) in ClientController.php line 120
3. at ClientController->update(object(Request), '2')
4. at call_user_func_array(array(object(ClientController), 'update'), array(object(Request), 'clients' => '2')) in Controller.php line 80
5. at Controller->callAction('update', array(object(Request), 'clients' => '2')) in ControllerDispatcher.php line 146
6. at ControllerDispatcher->call(object(ClientController), object(Route), 'update') in ControllerDispatcher.php line 94
7. at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
8. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
9. at Pipeline->Illuminate\Routing\{closure}(object(Request))
10. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
11. at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
12. at ControllerDispatcher->callWithinStack(object(ClientController), object(Route), object(Request), 'update') in ControllerDispatcher.php line 54
13. at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ClientController', 'update') in Route.php line 174
14. at Route->runController(object(Request)) in Route.php line 140
15. at Route->run(object(Request)) in Router.php line 724
16. at Router->Illuminate\Routing\{closure}(object(Request))
17. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
18. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 28
19. at Authenticate->handle(object(Request), object(Closure))
20. at call_user_func_array(array(object(Authenticate), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
21. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
22. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
23. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 29
24. at VerifyCsrfToken->handle(object(Request), object(Closure))
25. at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
26. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
27. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
28. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
29. at ShareErrorsFromSession->handle(object(Request), object(Closure))
30. at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
31. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
32. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
33. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
34. at StartSession->handle(object(Request), object(Closure))
35. at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
36. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
37. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
38. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
39. at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
40. at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
41. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
42. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
43. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
44. at EncryptCookies->handle(object(Request), object(Closure))
45. at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
46. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
47. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
48. at Pipeline->Illuminate\Routing\{closure}(object(Request))
49. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
50. at Pipeline->then(object(Closure)) in Router.php line 726
51. at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
52. at Router->dispatchToRoute(object(Request)) in Router.php line 675
53. at Router->dispatch(object(Request)) in Kernel.php line 246
54. at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
55. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
56. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
57. at Debugbar->handle(object(Request), object(Closure))
58. at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
59. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
60. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
61. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
62. at CheckForMaintenanceMode->handle(object(Request), object(Closure))
63. at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
64. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
65. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
66. at Pipeline->Illuminate\Routing\{closure}(object(Request))
67. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
68. at Pipeline->then(object(Closure)) in Kernel.php line 132
69. at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
70. at Kernel->handle(object(Request)) in index.php line 54

Answer

So the issue here is two fold:

  • First, you are attempting to set the property avatar, which doesn't exist on the request object.
  • Secondly, you are attempting to update $clients which is actually an array - meaning you cannot save() anything onto it as it is not a model.

From just looking at your code, it would appear that you would want to do:

$updated->avatar = $filename;
$updated->save();

Instead of:

$clients->avatar = $filename;
$clients->save();

Hope this helps!