0

i have edit.blade.php (artbook view) and inside list of photos (from multiple upload - tables in relationship artbooks 1 : N photos):

<div class="dataTables_wrapper">
  <div class="table-responsive">
    <table class="table table-striped" id="table">
      <thead>
        ...
      </thead>
      <tbody id="tablecontents">
        @foreach(($artbook->photos) as $photo)
        @if(!is_null($photo))
        <tr class="row1" data-id="{{ $photo->order }}">
          <td>{{ $photo->order }}</td>
          <td><img class="img-responsive" src="/storage/photos/{{ $photo->filename }}" style="width:100%; height:auto;"/></td>
          <td><a class="btn-dark btn-sm"><i class="fa fa-arrows my-handle" aria-hidden="true" style="color:#fff;"></i></a></td>
        </tr>
        @endif
        @endforeach
      </tbody>
    </table>
  </div>
</div>

in the same view I have script:

<script type="text/javascript">
    $(function () {
        $("#table").DataTable();
        $( "#tablecontents" ).sortable({
            items: "tr",
            cursor: 'move',
            opacity: 0.6,
            update: function() {
                sendOrderToServer();
            }
        });
        function sendOrderToServer() {
            var order = [];
            $('tr.row1').each(function(index,element) {
                order.push({
                    id: $(this).attr('data-id'),
                    position: index+1
                });
            });

            $.ajax({
                type: "POST", 
                dataType: "json", 
                url: "{{ url('artbook/edit/'.$artbook->id) }}",
                data: {
                    order:order,
                    _token: '{{csrf_token()}}'
                },
                success: function(response) {
                    if (response.status == "success") {
                        console.log(response);
                    } else {
                        console.log(response);
                    }
                }
            });
        }
    });
</script>

Function of my ArtbookController:

public function updatePhotoOrder(Request $request)
    {
        $photos = Photo::all();  
        console.log('test');
        foreach ($photos as $photo) {
            $photo->timestamps = false; // To disable update_at field updation
            $id = $photo->id;
            foreach ($request->order as $order) {
                if ($order['id'] == $id) {
                    $photo->update(['order' => $order['position']]);
                }
            }
        }
        return response('Update Successfully.', 200);
    }

and here is route:

Route::post('artbook/edit/{id}','ArtbookController@updatePhotoOrder');

in model my Order field is fillable, and after testing I get an error:

localhost/artbook/edit/1 500


Edit:

[2020-09-29 21:53:58] local.ERROR: Use of undefined constant console - assumed 'console' (this will throw an Error in a future version of PHP) {"userId":1,"exception":"[object] (ErrorException(code: 0): Use of undefined constant console - assumed 'console' (this will throw an Error in a future version of PHP) at /public_html/app/Http/Controllers/ArtbookController.php:91)
[stacktrace]
#0 /public_html/app/Http/Controllers/ArtbookController.php(91): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Use of undefine...', '/home/fldd/doma...', 91, Array)
#1 [internal function]: App\\Http\\Controllers\\ArtbookController->updatePhotoOrder(Object(Illuminate\\Http\\Request), '1')

Can someone help me? So much thanks!

12
  • check the logs to find out why you are getting a 500 error response ... then it will tell you the line where you are trying to use javascript in your PHP file, which you obviously can't do Commented Sep 29, 2020 at 21:32
  • It can be problem with route in ajax? Commented Sep 29, 2020 at 21:40
  • stop guessing and check the error logs, there is no guessing involved Commented Sep 29, 2020 at 21:41
  • I checked. See my edit at the top if you can Commented Sep 29, 2020 at 21:42
  • 1
    PHP is not javascript ... it is a completely different language with its own syntax ... in your view you are not "executing javascript" it is all just TEXT to be returned (there is no context), your browser is what can understand it as javascript and execute it ... the error gives you the exact line of the problem; no guessing involved Commented Sep 29, 2020 at 21:59

1 Answer 1

1

Ok I fixed the problem: Changed this:

<tr class="row1" data-id="{{ $photo->order }}">

to this:

<tr class="row1" data-id="{{ $photo->id }}">

Thanks lagbox for help!

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.