Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TOUTOU's avatar

Array to string conversion

Hello Laracast community, I am using laravel as a back end but when i try to post someting i get an error : ARRAY TO STRING CONVERSION this is my add function:

public function addIPMSAN (Request $request)
    {
      
        $model = new IPMSAN($request->all());
        $model->save();

        $response = array ('response' =>'IPMSAN record successfully created with id !', 'success'=>true);
        return $response ;
        
    }

and this is my form in frontend i know the problem is from using date can i get some help here plz

IPMSAN/Central
  <div class="form-group">
    <label for="name_ipm" class="col-sm-4 control-label">Name</label>
    <div class="col-sm-16">
      <input type="text" class="form-control" name="name"  [(ngModel)]="model.name_ipm" #name="ngModel">
    </div>
    <div *ngIf="name.errors && (name.dirty || name.touched)" class="alert alert-danger">
      <div [hidden]="!name.Errors.required">
        <span class="glyphicon glyphicon-exclamation-sign"></span>&nbsp;Name is Required
      </div>
    </div>
  </div>

  <hr>

  <div class="form-group">
    <label for="refrence" class="col-sm-4 control-label">Reference</label>
    <div class="col-sm-16">
      <input type="text" class="form-control" name="refrence" required [(ngModel)]="model.refrence_ipm" #refrence="ngModel">
    </div>
    <div *ngIf="refrence.errors && (refrence.dirty || refrence.touched)" class="alert alert-danger">
      <div [hidden]="!refrence.Errors.required">
        <span class="glyphicon glyphicon-exclamation-sign"></span>&nbsp;Refrence is Required
      </div>
    </div>
  </div>
  <hr>
  <div class="form-group">
      <label for="latitude" class="col-sm-4 control-label">Latitude</label>
      <div class="col-sm-16">
        <input type="text" class="form-control" name="latitude" required [(ngModel)]="model.latitude_ipm" #latitude="ngModel">
      </div>
      <div *ngIf="latitude.errors && (latitude.dirty || latitude.touched)" class="alert alert-danger">

        <div [hidden]="!latitude.Errors.required">
          <span class="glyphicon glyphicon-exclamation-sign"></span>&nbsp;latitude is Required </div>
      </div>
    </div>
    <hr>

    <div class="form-group">
      <label for="longitude" class="col-sm-4 control-label">longitude</label>
      <div class="col-sm-16">
        <input type="text" class="form-control" name="longitude" required [(ngModel)]="model.longitude_ipm" #longitude="ngModel">
      </div>
      <div *ngIf="longitude.errors && (longitude.dirty || longitude.touched)" class="alert alert-danger">

        <div [hidden]="!longitude.Errors.required">
          <span class="glyphicon glyphicon-exclamation-sign"></span>&nbsp;longitude is Required </div>
      </div>
    </div>
    <hr>
  <div class="form-group">
    <label for="deployment_date" class="col-sm-4 control-label">Deployment Date</label>
    <form class="form-inline">
      <div class="form-group">
        <div class="input-group">
          <input type="text" class="form-control" placeholder="yyyy-mm-dd" name="deployment_date" [(ngModel)]="model.deployment_date" ngbDatepicker #deployment_date="ngbDatepicker">
          <div class="input-group-append">
            <button class="btn btn-outline-secondary" (click)="deployment_date.toggle()" type="button">
              <img src="assets/img/calendar.svg" style="width: 1.2rem; height: 1rem; cursor: pointer;" />
            </button>
          </div>
        </div>
      </div>
    </form>
  </div>
  <hr/>

  <div class="form-group">
    <label for="maintenance" class="col-sm-4 control-label">Maintenance</label>
    <form class="form-inline">
      <div class="form-group">
        <div class="input-group">
          <input type="text" class="form-control" placeholder="yyyy-mm-dd" name="maintenance" [(ngModel)]="model.maintenance" ngbDatepicker #maintenance="ngbDatepicker">
          <div class="input-group-append">
            <button class="btn btn-outline-secondary" (click)="maintenance.toggle()" type="button">
              <img src="assets/img/calendar.svg" style="width: 1.2rem; height: 1rem; cursor: pointer;" />
            </button>
          </div>
        </div>
      </div>
    </form>
  </div>
  <hr>

  <div class="form-group">
    <div class="col-sm-offsset-4 col-sm_16">
      <a class="btn btn-info" (click)="goBack()">Cancel</a>
      <button type="submit" class="btn btn-success" id="btn-save" [disabled]="!addIPMSANForm.form.valid">submit</button>
      <button  class="btn btn-danger " (click)="addIPMSANForm.form.reset()"> Reset</button>
        <div class="alert alert-success alert-dismissible" id="msg1" style="display: none;"></div>

    </div>
  </div>

</form>

0 likes
42 replies
Cronix's avatar

Try changing

$model = new IPMSAN($request->all());
$model->save();

to

$model = IPMSAN::create($request->all());

Whatever fields are in the $request must be $fillable on the model.

TOUTOU's avatar

it give me an error: IPMSAN not found and in my Model i already add the $fillable

public $fillable = ['name_ipm','refrence_ipm','latitude_ipm','longitude_ipm','deployment_date','maintenance'];

Cronix's avatar

Ok, I assumed you were importing the model since you weren't including a namespace in your posted code. If it is located in /app/IPMSAN, then it would be

$model = \App\IMPSAN::create($request->all());

And IMPSAN would need to have

namespace App;

at the top.

If it's located somewhere else, you'd need to adjust everything I said to reflect its location.

TOUTOU's avatar

i try it and i get some new error : message": "Class 'App\Controller\Controller' not found",↵ i try also to relect its location it ggive me : "message": "Class 'App\Http\Controllers\App\IMPSAN' not found"

Cronix's avatar

You really need to post more relevant code then. It's really difficult to guess what could be wrong when I can just see it.

Post your whole controller, and the whole model, along with the location to each, like /app/Http/Controllers/YourController.php. Not just the method. The whole thing.

rin4ik's avatar

hey it should be

$model = \App\IMPSAN::create($request->all());
jlrdw's avatar

Sounds like a namespace mess up, fix your namespaces.

rin4ik's avatar

@TOUTOU your model name IPMSAN not IMPSAN

$model = \App\IPMSAN::create($request->all());
Cronix's avatar
namespace App;
namespace App\Controller;
namespace App\Http\Controllers;

If that's any indication.... There is only 1 namespace per file...

Cronix's avatar

@rin4ik I already suggested that and he said it didn't work. Wait til he posts his complete, formatted code. There are lots of things wrong here.

rin4ik's avatar

@Cronix it gives him this error

"message": "Class 'App\Http\Controllers\App\IMPSAN' not found"

Snapey's avatar

Clearly there are problems;

namespace App; namespace App\Controller; namespace App\Http\Controllers;

TOUTOU's avatar

@rin4ik yes it's work but still the same error :Array ti string conversion

Cronix's avatar

Fix your code formatting please.

jlrdw's avatar

Also time to breathe in, and watch some intro videos, starting with the free php series.

Cronix's avatar

You also need to post the location of both your model and controller, like /app/Http/Controllers/MyController.

jlrdw's avatar

Microwave just dinged, popcorn and soda time.

Cronix's avatar

I posted the link that shows how. 3 backticks on it's own line and nothing else. Then your code. Then a newline with 3 more backtics with nothing else.

But don't worry about it now since you posted it on the other place. Now we need to know the file paths of your controller and model.

TOUTOU's avatar

my Controller : /app/Http/Controllers/IPMSANController. my Model :/app/IPMSAN.php

Cronix's avatar
Cronix
Best Answer
Level 67

Ok, in your controller, change

namespace App;
namespace App\Controller;
namespace App\Http\Controllers;
use App\Controller\AppController;

to just

namespace App\Http\Controllers;

In the controller, change

$model = \App\IPMSAN::create($request->all());

to

$model = IPMSAN::create($request->all());

These aren't laravel issues. They're basic php issues.

TOUTOU's avatar

thank you i fixed it but it still the same ERROR array to string conversion

Cronix's avatar

Ok, let's see the output of the $request.

public function addIPMSAN (Request $request)
    {
        dd($request->all());  // add this
        $model = \App\IPMSAN::create($request->all());
rin4ik's avatar

what if you remove this and redirect back

 $response = array ('response' =>'IPMSAN record successfully created with id !', 'success'=>true);
    return $response ;
 
return back();
Next

Please or to participate in this conversation.