2

I want to store html table into my database but it is throwing this error

array to string conversion

i'm posting html table from javascript to my php controller

var html = '<table><thead><tr><th>Product Name</th><th>Price</th></tr></thead><tbody><tr><td>Alphabet</td><td>200</td></tr></tbody></table>; 

 $.ajax({
    url:'store',
    type:'POST',
    data:{table:html},
    success:function(data){
       console.log(data)
    }
 });

laravel:posted data to be inserted

 Route::post('store',function(){

   $toStore = addslashes(['table' => $_POST['table']]);   

   \App\product_table::create($toStore);

 });

2 Answers 2

2

You're getting this error because addslashes() expects string and you pass an array to the function.

You can just pass request array to the create() method:

\App\product_table::create(request()->all());

If you want to use addslashes() anyway, do this:

\App\product_table::create(['table' => addslashes(request('table'))]);
Sign up to request clarification or add additional context in comments.

Comments

0

the addslashes function accept only strings, and you have to use Request class in order to get your submitted data:

use Illuminate\Http\Request;

...

Route::post('store',function(Request $request) {

    $toStore = ['table' => addslashes($request->input('table'))];   

    \App\product_table::create($toStore);

});

1 Comment

You've copy-pasted ['table' => addslashes(...)] from my answer, but you've forgot to remove extra ) from the end.

Your Answer

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