1
I am developing a CRUD and in my controller I have 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(City) and the Neighborhood(Neighborhood) at the same time if you do not have neighborhood related to the City. Follow below 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 I will record the information in the Function store. Below is 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 follows the error:
Errorexception (E_NOTICE)
Array to string Conversion
The mistake I was able to interpret, it seems on the line 54 the variable $data['city_name'] is an array and needs to be a string. But how to do this?
I would also like a help with lines 56 that should be created in the bank and line 57 that creates the Neighborhood attached to the city.
I already have a one-to-Many interface in my Model City so I can use the command on line 57. Below.
class City extendes Model
{
protected $fillable = ['name'];
public $timestamps = false;
public function neighborhoods()
{
return $this->hasMany( related: Neighborhood::class);
}
}
I hope I made myself understood. I thank you in advance for your help.
Excuse me. I will read the above-mentioned material.
– Raphael Ohlsen