Passing array value in db

Published 1 week ago by asadbinamer

I am storing form values in db but the values are in array and it is not storing the data and giving me the following error

ErrorException in Grammar.php line 135: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in D:\xampp\htdocs\assignment\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 681 and defined

Best Answer (As Selected By asadbinamer)
ctroms

@asadbinamer Based on the migration you posted on this discussion

It looks like you have your question and answer attributes set as string column types. Try to change them to text or json if your DB supports it.

$table->text('question'); 
$table->text('answer'); 

Then in your AddQuestion model cast the question and answer attributes to array.

protected $casts = [
    'question' => 'array',
    'answer' => 'array',
];
asadbinamer

My form code

{{csrf_field()}}
<div class="container">
      <div class="row">
    <div class="col-sm-4">
     <label for="answer">Question</label>

  
         <input class="form-control" name="question[]" id="answer" value="" type="text" placeholder="Enter Answer" />
   </div>
         
         <div class="col-sm-4">
         <label for="answer">Answer</label>

        
         <input class="form-control" name="answer[]" id="answer" value="" type="text" placeholder="Enter Answer" />
         </div>
         <div class="col-sm-4">
         </br></br>
         
          <p><a href="#" class='remove'>Delete Question</a></p>
          </div>
          </div>
        </div>
        

Add Question

asadbinamer

My controller code

AddQuestion::create([

             'question'=>request('question'),
             'answer'=>request('answer'),
             'question_id'=>$question->id
          
        ]);
jlrdw
jlrdw
1 week ago (125,290 XP)

Where is your form action?

asadbinamer

it is there but not shown in code

ctroms
ctroms
1 week ago (53,420 XP)

Hello All. This seams to be a duplicate of this discussion

Since I was the only one to reply to that discussion so far, here is my only relevant post from that thread.

@asadbinamer Please post your migration too. and how are you setting the > $question variable in your Model create method?

@asadbinamer I would suggest pasting a link to this post as the accepted answer in your other post so that you are receiving replies to one post. It will help everyone else stay on the same page and ultimately help you to have everyone working off of the same discussion thread.

ctroms
ctroms
1 week ago (53,420 XP)

@asadbinamer Based on the migration you posted on this discussion

It looks like you have your question and answer attributes set as string column types. Try to change them to text or json if your DB supports it.

$table->text('question'); 
$table->text('answer'); 

Then in your AddQuestion model cast the question and answer attributes to array.

protected $casts = [
    'question' => 'array',
    'answer' => 'array',
];
ctroms
ctroms
1 week ago (53,420 XP)

@asadbinamer Sorry. I am just re-reading what you posted earlier and should have asked for further information before posting my previous reply.

Your question and answer form inputs are arrays so your controller is receiving all questions as an array and all answers as an array.

Assuming the AddQuestion class is your Eloquent model, are you trying to loop through each question and answer combination and add them as a single record or are you trying to add all questions from the questions field in the form to a questions column and all answers from the answers field in the form to an answers column of a single record.

asadbinamer

@ctroms how to call them in controller means how to request them ??

ctroms
ctroms
1 week ago (53,420 XP)

Your form has an input named question[] and one named answer[]

When your user submits the form, your store() method on your controller receives the request.

I'm referring to this controller method as defined in your your other post .

public function store(Request $request ,Question $question)

When you use the request helper request(question), an array is returned containing the values of all of the inputs on your from with the name "question[]".

Your form above only displays one input for question and one for answer but you would only append '[]' to your input name if you were intending on having multiple inputs named question.

Let me ask a different question, are you intending on having more than one question input and answer input on this form or will you only ever have one question input and one answer input. Maybe post your entire form including the opening '' and closing '' tags.

ctroms
ctroms
1 week ago (53,420 XP)

Ah. I see you accepted an answer. I assume your issue is resolved?

asadbinamer

@ctroms yes it is resolved ......bundle of thanks :)

ctroms
ctroms
1 week ago (53,420 XP)

No Problem. Have fun!

Sign In or create a forum account to participate in this discussion.