lundi 16 juillet 2018

Update multiple row at a time using laravel function

here is my index.blade.php code:

@extends('adminlte::page')

@section('content')

<div class="container">
<div class="row">
  <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">Companies</div>
                <div class="panel-body">
            <form action="" method="post">
            {!!csrf_field()!!}
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Add Company</button><br><br>
                    <table id="data_table" class="display">
                      <thead>
                          <tr>
                    <th>Checkbox</th> 
                    <th>Company ID</th>
                    <th>Name</th>
                    <th>Status</th>
                    <th>Created at</th>
                    <th>Updated at</th>
                    <!-- <<th>Edit</th>
                    <th>Delete</th> -->
                 </tr>
                      </thead>
                      <tbody>
                        @foreach ($companies as $key => $com)
                  <tr>
                    <td>
                      <input type="checkbox" name="multi_id[]" value="">
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <!-- <td>
                      <a href="#" class="edit-model" data-id="" data-company_name="" data-status="">
                          <i class="glyphicon glyphicon-pencil"></i>
                      </a>  
                    </td>
                    <td>
                      <a href="#" class="delete-model" data-id="" data-company_name="" data-status="">
                          <i class="glyphicon glyphicon-trash"></i>
                      </a>     
                    </td> -->
                  </tr>
                @endforeach
                      </tbody>
                    </table>
            <button type="submit" class="btn btn-link" name="action" value="delete"><i class="glyphicon glyphicon-trash"></i></button>
            <button type="submit" class="btn btn-link" name="action" value="save"><i class="glyphicon glyphicon-pencil"></i></button>
          </form>
            </div>
      </div>
    </div>
</div>

and this is my ajaxcontroller.php code:

<?php

namespace App\Http\Controllers;

use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\companies;
use App\User;
class AjaxController extends Controller
{
public function index()
{

    $companies = companies::all();
    $user = User::all();
    return view('ajax.index',compact('companies'));
}
public function delete(Request $request)
{


    switch ($request->input('action')) {
    case 'delete':
        $multi_id = $request->input('multi_id');
        companies::whereIn('id',$multi_id)
        ->delete();
        return redirect('/companies')->with('success','deleted');
        break;

    case 'save':
        $companies = companies::findOrFail($request->company_id);
        $companies->update($request->all());
        return redirect('/companies')->with('success','updated');
        break;

    // case 'advanced_edit':
    //     // Redirect to advanced edit
    //     break;
}

}
}

and this is my route:

Route::post('/delete','AjaxController@delete');

I want to edit multiple row and update all the row using the "save" button, how can I do that? please anyone can help me in this case.In the "delete" function case "save" is my update function. also I am in the beginning level.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire