I'm developing a CRUD and in my controller I have the create function:
public function create()
{
$neighborhood = new Neighborhood();
$city = new City();
return view( view: 'admin.cities.create', compact( varname: 'city', _:'neighborhood'));
}
The goal is to create the City and Neighborhood at the same time if you do not have a City-related neighborhood. Below is the View:
@section('content')
<h3>Nova Cidade</h3>
<form method="post" action="{{ route('cities.store') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Nome da Cidade</label>
<input class="form-control" id="name" name="city_name" value="{{ old('name', $city-name) }}">
</div>
@if ($neighborhood->id == '')
<div class="form-group">
<label for="name">Nome do Bairro</label>
<input class="form-control" id="name" name="neighborhood_name" value="{{ old('name', $neighborhood->name) }}">
</div>
@endif
<button type="submit" class="btn btn-default">Criar</button>
</form>
@endsection
My question is how do I save the information in the function store. Here is the function:
public function store(Request $request, City $city, Neighborhood $neighborhood)
{
$data = $request->all();
$city->name->$data['city_name'];
$neighborhood->name->$data['neighborhood'];
Client::create();
Neighborhood::create();
$city->neighborhoods()->create($neighborhood);
}
And below is the error:
ErrorException (E_NOTICE)
Array to string conversion
The error I was able to interpret, so it seems on the line 54 the variable $ data ['city_name'] is an array and must be a string. But how to do this?
I would also like help with the 56 lines that should be to create in the bank and line 57 that creates the neighborhood attached to the city.
I already have a one-to-many relationship in my Model City so I can use the command on line 57. Below is the example.
class City extendes Model
{
protected $fillable = ['name'];
public $timestamps = false;
public function neighborhoods()
{
return $this->hasMany( related: Neighborhood::class);
}
}
I hope I've been able to make myself understood. Thanks in advance for the help.