0

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');
    }

    
4
  • 1
    $agencyCityLocation->location_id = $request->location; here you need to send id but your sending array Commented Mar 15, 2021 at 11:46
  • i am selecting multiple location and city i am sending ids of city and location Commented Mar 15, 2021 at 11:50
  • $agencyCityLocation->save(); i am getting error in this line Commented Mar 15, 2021 at 11:51
  • if your selecting multiple location then your database need to allow to insert array insted of single id Commented Mar 15, 2021 at 12:00

2 Answers 2

1

Replace this

$agencyCityLocation->location_id = $request->location;

By

$agencyCityLocation->location_id = $request->location[$key]
Sign up to request clarification or add additional context in comments.

Comments

0

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?

2 Comments

is this solution ?
We don't know how the OP wants to handle this situation. He needs to iterate through all locations and all cities. We don't know, if he wants to create one DB entry for each city and location combination. But I will add this explantion to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.