Reputation: 223
I am trying to insert data but unfortunately i am getting error Array to string conversion please help me how can i resolve that thanks.
please check error https://flareapp.io/share/x5Mznjem
return request
{
"_token": "3qLsIoNwWiOuze8aurlSQGqU4FsgttXgY6sMFYnw",
"icon": "0AKj2DRZii6yhJsoWKLNUbmOWKrXzOqKoFJTF4LI.jpg",
"name": "fdgdfg",
"person_name": "dfg",
"contact_number": "43543543",
"city": [
"2",
"3",
"4",
"5"
],
"location": [
"1",
"3",
"4"
],
"address": "A-232 Gulshan-e-hadeed ph 2"
}
controller
public function store(Request $request)
{
// return $request->all();
$request->validate([
'name' => "required",
'icon' => 'nullable',
'person_name' => 'required',
'contact_number' => 'required',
]);
$agency = Agency::create($request->except('_token'));
foreach ($request->city as $key => $value) {
$agencyCityLocation = new AgencyCityLocation;
$agencyCityLocation->agency_id = $agency->id;
$agencyCityLocation->city_id = $value;
$agencyCityLocation->location_id = $request->location;
$agencyCityLocation->save();
}
return redirect()->route('agency');
}
Upvotes: 0
Views: 686
Reputation: 102
Replace this
$agencyCityLocation->location_id = $request->location;
By
$agencyCityLocation->location_id = $request->location[$key]
Upvotes: 1
Reputation: 3933
The issue is this line:
$agencyCityLocation->location_id = $request->location;
As you wrote $request->location
is an array...
"location": [
"1",
"3",
"4"
],
...and I assume, that in $agencyCityLocation->location_id
only one string is expected and not an array.
One solution would be to iterate through the locations as well (as you do with the cities), but actually we don't know how you want to save your data into the database. Do you want one DB entry for each city - location combination or is the city combined with the location?
Upvotes: 0