Getting error exception "array to string conversion"

Posted 4 months ago by Gabotronix

I'm getting the following error thrown when I try to save an email into a database, what I'm trying to do is the classic subscription system.

This is my full error:

exception: "ErrorException"
​
file: "C:\xampp\htdocs\Restaurante1\vendor\laravel\framework\src\Illuminate\Database\Grammar.php"
​
line: 39
​
message: "Array to string conversion"
​
​​
8: Object { file: "C:\xampp\htdocs\Restaurante1\app\Http\Controllers\SubscriptionController.php", line: 44, function: "save", … }
​​
9: Object { function: "store", class: "App\Http\Controllers\SubscriptionController", type: "->" }

​​```
This is my JQuery code:
```php
$(document).ready(function(){
    
    
    
    $(".startSubscription").click(function(){
        var form = $(this).closest('form');
        $('.form_error_text').hide();
        $('.form_valid_text').hide();
        console.log('button start subscription clicked');
        
        var emailval=$("input[name='email']").val();
        
        
        $.ajaxSetup({
            
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')}
        
        });
        
        $.ajax({
            
            async: true,
            cache: false,
            url: '/subscriptions',
            type: 'POST',
            data: 
            {
                
            'email': emailval
            
            },
            dataType: 'JSON',
            
            success: function (data) { 
                $('.form_valid_container').html('<span class="form_valid_text">✓ '+ data.success +'</span>');
                form.trigger("reset");
                console.log(data.success);
            },
            
            error: function (data){
                var errors = data.responseJSON;
                console.log(errors);
                
                $.each(errors , function(){
                    $('.form_error_container').html('<span class="form_error_text">✘ '+ errors.message +'</span>')
                }); 
            }

        }); 
    });
    

});

And my controller where I save the email to a database:

public function store(StoreSubbedMail $request)
    {
        $subscription = new Subscription();
        $subscription->email = $request->email;
        $subscription->save();
        
        return response()->json([
        
            'success' => 'Te subscribiste correctamente'
        ]);
    }

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.