Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in

Tags:

php

laravel

I have an input to enter the tags. Each course will have many tags and I want to save these tags in the same column.

Input

    <div class="card border-grey border-lighten-3 px-2 py-2 box-shadow-1 mt-1">
        <h4 class="content-header-title"> Tags </h4>
            <select name="tags[]" data-tags="true" id="tags" class="form-control" multiple="multiple">
                <option value=""></option>
            </select>
    </div>

Srcipt select2

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
  <script>
    $(document).ready(function(){
      $('#tags').select2({
        placeholder: "Tags",
        tags: true
      });
    });
 </script>

Controller


    function save(Request $request)
    {
        $lesson = new Lesson;
        $lesson->tags= $request->input('tags');
        $lesson->content = $request->content;
        $lesson->save();
        return redirect(url('admin/lessons/edit'))->with('message', 'Successful');
    }

But when i click to save it appears an error like this:

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\Xampp-install\htdocs\eduwebsite\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 869

like image 538
Truc Pham Avatar asked May 30 '26 22:05

Truc Pham


1 Answers

if you want to store tags as array in database you can use serialize

$lesson->tags= serialize($request->tags);

or better way is using Array & JSON Casting

class Lesson extends Model
{
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'tags' => 'array',
    ];
}
like image 101
Mohammad Fanni Avatar answered Jun 02 '26 10:06

Mohammad Fanni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!