0

I use ajax to get data json and send it to my controller, now I want to insert this data into table pointages help me please wher is the error. here is ajax code: he give me this error enter image description here

 $('.add-all').on('click', function() {
        var items = [];
        let valu = $('#datePicker').val();
      $("tr").each(function(i,r){
        if ( i > 0 && $(r).find("input").first().prop("checked"))
        {  
          items.push({"matricule": r.cells[3].innerText, "salaire": r.cells[5].innerText, "date" : valu })
              }
                                      });
//ajax
$.ajax({ 
        method      : 'POST', 
        url       : 'mois', 
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        data      : {"items":items}, // pass in json format 
        success   : function(data) {
            console.log(data);
        },
        error : function(err){
            console.log(err)
        }
    });

//fin ajax 
      });  

Controller:

public function addMultiple(Request $request){
   foreach($request->get('items') as $item){
    $pointage = Pointage::create([
    'matricule' => $item->$item['matricule'], 
    'datep' => $item->$item['datep'],
    'solde' => $item->$item['salaire'] 
     ]);
}
return redirect("mois");
}

modele Pointage :

class Pointage extends Model
{
    /**
     * 
     *
     * @var array
     */
    protected $fillable = [
        'matricule', 'datep', 'solde',
    ];
}

table pointages:the other fields is nullable

  public function up()
    {
        Schema::create('pointages', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('matricule');
            $table->date('datep');
            $table->double('nbrj');
            $table->double('prime');
            $table->double('solde');
            $table->timestamps();
        });
    }
3
  • Can you dd($item['matricule']) before create? You have to see what type of data you have in $item. Commented Jun 23, 2019 at 20:29
  • he give me: "1" Commented Jun 23, 2019 at 21:11
  • So you have correct data. You need 'matricule' => $item['matricule'], instead of ` 'matricule' => $item->$item['matricule']`. Commented Jun 24, 2019 at 8:16

1 Answer 1

1

what do you have in the line 79? I think could be

'matricule' => $item['matricule'],

instead

'matricule' => $item->$item['matricule'],
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.