-1

I am using the Laravel 5.7 PHP framework for this. I am storing client data based on their email address. The client data 'orders' field is of type JSON. Each time the client uses their email to make an order, I want to check if a record exists with that email, if it does, it will pull the order JSON from that record and add the new order, if not, it will create a new model make a new record.

I get the following exception: "Array to string conversion". When tracing, I find that it comes from this line of code which follows the convention from Laravel 5.7 documentation:

$client_data = ClientData::firstOrNew(['email' => $request->email]);

This is what I have in my ClientData Model:

protected $table = ['client_data'];
protected $guarded = ['created_at'];

I verified that the $request->email is indeed a string with an email address in it.

I also tried it with the following line of code, but got the same exception thrown:

$client_data = ClientData::where('email', $request->email)->first() ?: new ClientData(['email' => $request->email]);
Jason
  • 11
  • 2
  • What do you get if you do `Log::info($request->email);` and look for the result in `/storage/logs/`? – aynber Apr 16 '19 at 17:49
  • I get the correct email address. I have already mentioned that I verified that `$request->email` is indeed a string with an email address in it. – Jason Apr 16 '19 at 17:54
  • I think you may try to use `firstOrCreate` – loic.lopez Apr 16 '19 at 18:09
  • When you use your last example but split it up over two lines, which one does the error happen on? `where..first` or `new ...`? – Thomas Apr 16 '19 at 18:17
  • @Thomas new this error is triggered when passing the array into the new Model(array). Could this be a bug in Laravel? – Jason Apr 16 '19 at 18:18
  • I think it's unlikely to be a bug in Laravel, but I can't see any problem with your code. Which line actually triggers the bug? – Thomas Apr 16 '19 at 18:29
  • @Thomas After that line of code, the trace goes into the Model and does like 10 more operations before it reaches Grammar.php which is the file that actually throws the exception. I am on the road right now, when I get a moment, I’ll post the stack trace. – Jason Apr 16 '19 at 18:53
  • Before the stack trace, please try changing $table from array to string. – Tarek Adam Apr 16 '19 at 18:56

1 Answers1

0

protected $table = ['client_data']; I think should not be an array, but a string.

Tarek Adam
  • 3,387
  • 3
  • 27
  • 52
  • Omg. I think you’re right! I am now more than confident that this is what is causing the issue. I will try it as soon as I get in front of a computer. Thank you! I think I’m overwhelmed with these projects that I’m starting to overlook these little requisites. – Jason Apr 16 '19 at 18:58
  • Taking a break from the computer is often the best path to the solution. – Tarek Adam Apr 16 '19 at 18:59
  • `protected $table = 'client_data';` –  Apr 16 '19 at 19:28
  • I guess that works... Kinda hoping for something more creative. `protected $table = ['client_data'][0];` – Tarek Adam Apr 16 '19 at 20:23