Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

As the error states, associate accepts an eloquent model as first parameter, not a string. You need to pass it a colaborador model, not id.

Last updated 2 years ago.
0

Ok, nice information.

But what should I use to better accomplish my need? Is really instantiating an colaborador object? Or there is a simplest way?

Last updated 2 years ago.
0

Of course not, but since there is no disassociate method you need to set the FK value to null:

$veiculo->colaborador()->associate('NULL')->save();   

// change to:
$veiculo->colaborador_id = null;
$veiculo->save();

Obviously this will work only if you have set that column as nullable.

Last updated 2 years ago.
0

Hello, I tried that, the field is nullable. But its not setting the field to NULL.

Like this: $veiculo = Veiculo::find($id); $colid = $veiculo->colaborador_id;

                		$colaborador = Colaborador::find($colid);
			$colaborador->veiculo_id = null;
			$colaborador->save();
			$veiculo->colaborador_id = null;

And there is no change at that field.

Last updated 2 years ago.
0

You are setting a value on colaborador and trying to output a value on veiculo...

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sytker sytker Joined 29 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.