Melonendk's avatar

Passing an array through SQL "Array to string conversion" ?

So I'm working on a little project, where I wanna post like say a new competition, where u need to enter the participants when u create it, rather than having another table and then select each one.

So I wanna pass an array through with each participant in it.

but when I do, I get the classic error "QueryException: Array to string conversion"

Here is the basic code for when uploading/posting it to the sql

    public function store(Request $request)
    {
        $teams = array(
            'teamname1' => request('teamname1'),
            'teamcode1' => request('teamcode1'),
            'teamname2' => request('teamname2'),
            'teamcode2' => request('teamcode2'),
            'teamname3' => request('teamname3'),
            'teamcode3' => request('teamcode3')
        );

        $Competition = new Competitions;
        $Competition->name = request('name'); // Just a Name
        $Competition->body = request('body'); // Normal Description body
        $Competition->genres = request('genres'); // This return an array of selections from the categories table
        $Competition->attendees = $teams; // The array of participants
        $Competition->start = request('start'); // Timestamp for when it starts
        $Competition->end = request('end'); // Timestamp for when it ends
        $Competition->save();

        return redirect('/admin/competitions');
    }

I tried some different things but it always ends back to this error so I hope I can get a little help to fix it Thx in advance. =)

0 likes
2 replies
Melonendk's avatar

To after a little bit of tinkering, i found how to fix it and it works with. @RonB1985 help Thank u BTW =)

So to fix it I used only.

protected $casts = [
    'attendees' => 'array',
];
1 like

Please or to participate in this conversation.