you are calling the create method instead of the update method like this
$user->client->update([
......
]);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello community !!! I need to edit and update a record in my database. For this 3 tables are related and I want to use eloqunent. The relationship is as follows: a customer belongs to a user and a user is related to a customer, a customer belongs to a company and a company is related to a customer. At the moment I only have the index and store method working. Would you help me implement eloquent with the editing and updating method? (I'm starting with eloquent) I leave my code below: model client
class Client extends Model
{
protected $fillable = [
'user_id', 'company_id', 'direccion', 'ciudad', 'provincia', 'codigo_postal', 'pais', 'telefono_contacto'
];
public $timestamps = false;
public function user()
{
return $this->belongsTo(User::class);
}
public function company()
{
return $this->belongsTo(Company::class);
}
}
model user:
public function client()
{
return $this->hasOne(Client::class);
}
model company:
public function client()
{
return $this->hasOne(Client::class);
}
clientcontroller method store:
public function store(ClientStoreRequest $request)
{
DB::transaction(function () use ($request) {
$user = User::create([
'name' => $request['nombre_cliente'],
'surname' => $request['apellido'],
'email' => $request['email'],
'password' => bcrypt($request['password']),
]);
$company = Company::create([
'nombre' => $request['nombre_empresa'],
'registro_fiscal' => $request['registro_fiscal'],
'sitio_web' => $request['sitio_web'],
'telefono' => $request['telefono'],
]);
$client = $user->client()->create([
'telefono_contacto' => $request['telefono_contacto'],
'direccion' => $request['calle'],
'ciudad' => $request['ciudad'],
'provincia' => $request['provincia'],
'codigo_postal' => $request['codigo_postal'],
'pais' => $request['pais'],
'company_id' => $company->id
]);
return response()->json(['success' => 'Cliente agregado con exito.']);
}
I hope your prompt help. Thanks!
In addition to the official documentation of Laravel, could you provide me with a tutorial, a crud that relates tables and models so I apply my questions to be able to list create edit save update
Please or to participate in this conversation.