Onyx
Onyx

Reputation: 5772

Unable to Explode HTTP Request

I have an input in which I intend to write image tags in the following way:

Tag1, Tag2, Tag3

Then create an array which I can loop through by using the explode() PHP function like this:

$tagsRaw = $request['artwork-tags'];
$tags = explode(',', $tagsRaw);

foreach($tags as $tag) {
    $tag = new Tag();
    $tag->name = $tag;
    $tag->save();

    $image->tags()->attach($tag);
}

Sadly I'm getting this error:

Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\MAMP\htdocs\Art\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 681 ◀

I assume that I'm trying to explode() something which can't be exploded and that I'm not passing a string to explode().

Upvotes: 0

Views: 239

Answers (1)

user9880886
user9880886

Reputation:

You are overriding the $tag variable instantly.

$tagsRaw = $request['artwork-tags'];
$tags = explode(',', $tagsRaw);

foreach($tags as $tagName) {
    $tag = new Tag();
    $tag->name = $tagName;
    $tag->save();

    $image->tags()->attach($tag);
}

Upvotes: 3

Related Questions