lundi 22 juin 2020

how to fix POST http://localhost:8000/cour 500 (Internal Server Error) laravel,ajax and Yajra pakage

how can i solve this problem, i'm working with package yajra but i don't know where i have problem so please help to solve it and thank you there is my controller and view

   <?php

namespace App\Http\Controllers;

use App\Cour;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
//use Validator;

class CourController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(request()->ajax())
        {
            return datatables()->of(Cour::latest()->get())
                    ->addColumn('action', function($data){
                        $button = '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-sm">Edit</button>';
                        $button .= '&nbsp;&nbsp;';
                        $button .= '<button type="button" name="delete" id="'.$data->id.'" class="delete btn btn-danger btn-sm">Delete</button>';
                        return $button;
                    })
                    ->rawColumns(['action'])
                    ->make(true);
        }
        return view('cour');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rules = array(
            'prof'    =>  'required',
            'module'     =>  'required', 
            'chapitre'    =>  'required',
             'titre'    =>  'required',
            'description'     =>  'required',
            'file' => 'required|file|max:5000|mimes:pdf,docx,doc',
            
        );

        $error = Validator::make($request->all(), $rules);

        if($error->fails())
        {
            return response()->json(['errors' => $error->errors()->all()]);
        }

        $file = $request->file('file');

        $new_name = rand() . '.' . $file->getClientOriginalExtension();

        $file->move(public_path('images'), $new_name);

        $form_data = array(
            'prof'        =>  $request->prof,
            'module'         =>  $request->module,
            'chapitre'        =>  $request->chapitre,
            'titre'         =>  $request->titre,
            'description'         =>  $request->description,
            'file'             =>  $new_name
        );

        Cour::create($form_data);

        return response()->json(['success' => 'Data Added successfully.']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(request()->ajax())
        {
            $data = Cour::findOrFail($id);
            return response()->json(['data' => $data]);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $file_name = $request->hidden_file;
        $file = $request->file('file');
        if($file != '')
        {
            $rules = array(
                'prof'    =>  'required',
                'module'     =>  'required',
                'chapitre'    =>  'required',
                'titre'     =>  'required',
                'description'     =>  'required',
                'file' => 'required|file|mimes:pdf,docx,doc|max:5000',
            );
            $error = Validator::make($request->all(), $rules);
            if($error->fails())
            {
                return response()->json(['errors' => $error->errors()->all()]);
            }

            $file_name = rand() . '.' . $file->getClientOriginalExtension();
            $file->move(public_path('images'), $file_name);
        }
        else
        {
            $rules = array(
                'prof'    =>  'required',
                'module'     =>  'required',
                'chapitre'    =>  'required',
                'titre'     =>  'required',
                'description'     =>  'required',
            );

            $error = Validator::make($request->all(), $rules);

            if($error->fails())
            {
                return response()->json(['errors' => $error->errors()->all()]);
            }
        }

        $form_data = array(
            'prof'        =>  $request->prof,
            'module'         =>  $request->module,
            'chapitre'        =>  $request->chapitre,
            'titre'         =>  $request->titre,
            'description'         =>  $request->description,
            'file'            =>   $file_name
        );
        Cour::whereId($request->hidden_id)->update($form_data);

        return response()->json(['success' => 'Data is successfully updated']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Cour::findOrFail($id);
        $data->delete();
    }
    public function download($file)
    {
       return response()->download('storage/images'.$file);
    }
}
  1. cour view

    Laravel 5.8 - DataTables Server Side Processing using Ajax
    Laravel 5.8 Ajax Crud Tutorial - Delete or Remove Data
    Create Record
    Id professeur module chapitre titre description Action

    × Add cour @csrf
          <div class="form-group">
              <label class="col-md-4 text-right">Enseignant:</label>
              <div class="col-md-8">
                  <input type="text" name="prof" class="form-control input-lg"  placeholder="le nom du prof" />
              </div>
          </div>
    
          <div class="form-group">
              <label class="col-md-4 text-right">Module:</label>
              <div class="col-md-8">
                  <input type="text" name="module" class="form-control input-lg"  placeholder="module"/>
              </div>
          </div>
    
          <div class="form-group">
              <label class="col-md-4 text-right">Chapitre:</label>
              <div class="col-md-8">
                  <input type="text" name="chapitre" class="form-control input-lg"  placeholder="chapitre"/>
              </div>
          </div>
    
          <div class="form-group">
              <label class="col-md-4 text-right">Titre:</label>
              <div class="col-md-8">
                  <input type="text" name="titre" class="form-control input-lg"  placeholder="titre"/>
              </div>
          </div>
    
          <div class="form-group">
              <label class="col-md-4 text-right">Description:</label>
              <div class="col-md-8">
                  <input type="text" name="description" class="form-control input-lg"  placeholder="description"/>
              </div>
          </div>
    
          <div class="form-group">
              <label class="col-md-4 text-right">choisir le fichier</label>
              <div class="col-md-8">
                  <input type="file" name="file" id="file" />
                  <span id="store_image"></span>
              </div>
          </div>
    
           <div class="form-group" align="center">
            <input type="hidden" name="action" id="action" />
            <input type="hidden" name="hidden_id" id="hidden_id" />
            <input type="submit" name="action_button" id="action_button" class="btn btn-warning" value="Add" />
           </div>
         </form>
        </div>
     </div>
    </div>
    
    × Confirmation Are you sure you want to remove this data? OK Cancel $(document).ready(function(){ $('#user_table').DataTable({ processing: true, serverSide: true, ajax:{ url: "", }, columns:[ { data: 'file', name: 'file', render: function(data, type, full, meta){ return ""; }, orderable: false }, { data: 'prof', name: 'prof' }, { data: 'module', name: 'module' }, { data: 'chapitre', name: 'chapitre' }, { data: 'titre', name: 'titre' }, { data: 'description', name: 'description' }, { data: 'action', name: 'action', orderable: false } ] }); $('#create_record').click(function(){ $('.modal-title').text("Ajouter un coure"); $('#action_button').val("Add"); $('#action').val("Add"); $('#formModal').modal('show'); }); $('#sample_form').on('submit', function(event){ event.preventDefault(); if($('#action').val() == 'Add') { $.ajax({ url:"", method:"POST", data: new FormData(this), contentType: false, cache:false, processData: false, dataType:"json", success:function(data) { var html = ''; if(data.errors) { html = ''; for(var count = 0; count ' + data.errors[count] + ''; } html += ''; } if(data.success) { html = '' + data.success + ''; $('#sample_form')[0].reset(); $('#user_table').DataTable().ajax.reload(); } $('#form_result').html(html); } }) } if($('#action').val() == "Edit") { $.ajax({ url:"", method:"POST", data:new FormData(this), contentType: false, cache: false, processData: false, dataType:"json", success:function(data) { var html = ''; if(data.errors) { html = ''; for(var count = 0; count ' + data.errors[count] + ''; } html += ''; } if(data.success) { html = '' + data.success + ''; $('#sample_form')[0].reset(); $('#store_image').html(''); $('#user_table').DataTable().ajax.reload(); } $('#form_result').html(html); } }); } }); $(document).on('click', '.edit', function(){ var id = $(this).attr('id'); $('#form_result').html(''); $.ajax({ url:"/cour/"+id+"/edit", dataType:"json", success:function(html){ $('#prof').val(html.data.prof); $('#module').val(html.data.module); $('#chapitre').val(html.data.chapitre); $('#titre').val(html.data.titre); $('#description').val(html.data.description); $('#store_file').html(""); $('#store_file').append(""); $('#hidden_id').val(html.data.id); $('.modal-title').text("Edit New Record"); $('#action_button').val("Edit"); $('#action').val("Edit"); $('#formModal').modal('show'); } }) }); var user_id; $(document).on('click', '.delete', function(){ user_id = $(this).attr('id'); $('#confirmModal').modal('show'); }); $('#ok_button').click(function(){ $.ajax({ url:"cour/destroy/"+user_id, beforeSend:function(){ $('#ok_button').text('Deleting...'); }, success:function(data) { setTimeout(function(){ $('#confirmModal').modal('hide'); $('#user_table').DataTable().ajax.reload(); }, 2000); } }) }); });

routes :

Route::resource('cour', 'CourController');

Route::post('cour/update', 'CourController@update')->name('cour.update');

Route::get('cour/destroy/{id}', 'CourController@destroy');


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2YY9FM1
via IFTTT

Aucun commentaire:

Enregistrer un commentaire