2

ErrorException Array to string conversion

    $presocio              = new Presocio;
    $presocio->prestamo_id = $request->prestamo_id;
    $presocio->ncuota      = $request->ncuota;
    $presocio->montopag    = $request->montopag;
    $presocio->fechapag    = $request->fechapag;
    $presocio->save();

In the end I managed to make it work like this, it works perfectly. it can be done in different ways, example with ::create ::insert

    $prestamo           = new Prestamo;
    $prestamo->socio_id = $request->socio_id;
    $prestamo->monto    = $request->monto;
    $prestamo->cuotas   = $request->cuotas;
    $prestamo->alias    = $request->alias;
    $prestamo->save();

    $idprestamo = $prestamo->id;

    if (count($request->ncuota) > 0) {
        foreach ($request->ncuota as $item => $v) {
            $presocio = new Presocio;
            $presocio->fill(
                array(
                    'prestamo_id' => $idprestamo,
                    'ncuota'      => $request->ncuota[$item],
                    'montopag'    => $request->montopag[$item],
                    'fechapag'    => $request->fechapag[$item],
                )
            );
            $presocio->save();
        }
    }
    toast('Pago Programados Registrado', 'success');
    return redirect('prestamo');
8
  • 1
    Which line in this code causes this error? Commented Nov 28, 2019 at 4:26
  • you want to save data in Database? Commented Nov 28, 2019 at 4:28
  • 1
    share your view code Commented Nov 28, 2019 at 4:38
  • send array from my view, and the error of: ErrorException Array to string conversion Commented Nov 28, 2019 at 5:27
  • they are all arrays, what do you want to do with these arrays? Commented Nov 28, 2019 at 6:24

3 Answers 3

1

Update since we now have the form supplied. You are using form names such as ncuota[] instead of ncuota which makes it an array. Are you able to make more than 1 Preseocio? if this is the case you want to loop over the items in the controller.

for ($i = 0; $i < count($request->ncuota); $i++)
{
    Presocio::create([
        'prestamo_id' => $request->prestamo_id[$i],
        'ncuota' => $request->ncuota[$i],
        'montopag' => $request->montopag[$i],
        'fechapag' => $request->fechapag[$i],
    ]);
}

Otherwise just remove the [] off the end of the form names.

class Presocio
{
    ...
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'prestamo_id',
        'ncuota',
        'montopag',
        'fechapag',
    ];
    ...
}

Presocio::create($request->all());

Now, Thats not the issue. That is just a bit of house keeping.

Your issue is that one of your request fields is an Array. Which ever one it is you will need to convert it to a JSON object or find a better way of storing it. If you dont care and want to keep it as an array, modify the database field to be a jsonb field.

Sign up to request clarification or add additional context in comments.

6 Comments

the create method takes an array
That's what the request is. That is not the array im talking about converting. He is trying to store an array in a string field.
good now, but yes they have some input that is an array and is causing the error most likely
I have my html table and with jquery add rows, the inputs are "name []" I execute dd ($ presocio); brings array of data sent.
@SergioNC Can you show which line of code error occurs?
|
0

Try This create method

remove your all code and write only this code in your store method

$input = $request->all();
Presocio::create($input);

4 Comments

please select this answer if this answer is helpful THANK YOU@SergioNC
please write in cooment not as a answer.
explain how that is different than what they are doing and how this solves their problem
I have my html table and with jquery add rows, the inputs are "name []" I execute dd ($ presocio); brings array of data sent.
0

You can do that like:

for($i=0; $i < count($request->input('prestamo_id', 'ncuota', 'montopag', 'fechapag')); $i++) {

    $presocio              = new Presocio;
    $presocio->prestamo_id = $request->prestamo_id[$i];
    $presocio->ncuota      = $request->ncuota[$i];
    $presocio->montopag    = $request->montopag[$i];
    $presocio->fechapag    = $request->fechapag[$i];
    $presocio->save();
}

5 Comments

with jquery I add row, to have more fields. When sending, only the last row is saved by the controller
@SergioNC That is why everyone told you please put you Html and jquery code into your question.
@SergioNC If your code is too lengthy. don not worry. You should put your logic only. Like how you can add a row?
I generate several rows to fill, but when I make the shipment, just register the first row
@SergioNC I have tried the above code. It is working fine using loops. I don't know your shipment logic. You should try to echo '<pre>'; print_r($request->prestamo_id[$i]); inside for loop. check all the entered value returns or not??

Your Answer

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