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

UsmanBasharmal's avatar

After removing pagination could not get the data via api Laravel/Vuejs

I have applied serverside pagination but it didn't work with Datatables so I removed it but now I could not get the data from my BookController via API if I put the pagination back it works I don't know what is wrong.

here is my Bookcontroller

public function index()
    {
        try {
            // $Book = Book::orderBy('name', 'ASC')->paginate(10);
            $Book = Book::orderBy('name', 'ASC')->get();
            return response()->json($Book);
        } catch (Exception $e) {
            return response()->json($e->getMessage(), 500);
        }
    }

here is API

Route::apiResources(['Book'=>'API\BookController']);

here is some code of Book.vue

export default {
    data() {
        return {
           
            Books: {},
}
methods: {
loadallBooks() {
            axios.get("api/Book").then(({ data }) => (this.Books = data));
        },
created() {
        this.loadBooks();
        Fire.$on("refreshPage", () => {
            this.loadBooks();
        });
    }
}

}

0 likes
8 replies
UsmanBasharmal's avatar

Thank you for the prompt response, I try this

 axios.get("api/Book").then(
    function(data) {
      console.log("before this: " + this.Books);
      this.Books = data.data;
      console.log("after: " + this.Books);
    }.bind(this)
  );

and the result is

before this:

after: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

and in the Network tab of the browser the result is like below

[{"id":12,"name":"English language","category_id":3,"author_id":10,"translate_id":8,"language_id":4,"publisher_id":3,"publishing_date":"2020-03-26","edition":"3","valume_number":3,"ISBN":"ENG111","deleted_at":null,"created_at":"2020-03-29 16:14:04","updated_at":"2020-03-29 16:14:04"},{"id":13,"name":"\u06e4\u067e\u069a\u062a\u0648","category_id":3,"author_id":10,"translate_id":8,"language_id":4,"publisher_id":3,"publishing_date":"2020-03-27","edition":"1","valume_number":2,"ISBN":"PASHt11","deleted_at":null,"created_at":"2020-03-29 16:15:01","updated_at":"2020-03-29 16:15:01"},{"id":4,"name":"\u0631\u06cc\u0627\u0636 \u0627\u0644\u0635\u0627\u0644\u062d\u06cc\u0646","category_id":2,"author_id":7,"translate_id":6,"language_id":3,"publisher_id":1,"publishing_date":"2020-02-19","edition":"1","valume_number":3,"ISBN":"2323323","deleted_at":null,"created_at":"2020-02-09 17:24:07","updated_at":"2020-03-26 13:23:51"},{"id":6,"name":"\u0631\u06cc\u0627\u0636 \u0627\u0644\u0635\u0627\u0644\u062d\u06cc\u0646","category_id":2,"author_id":7,"translate_id":6,"language_id":3,"publisher_id":1,"publishing_date":"2020-02-29","edition":"2","valume_number":2,"ISBN":"22323323","deleted_at":null,"created_at":"2020-02-22 14:04:52","updated_at":"2020-03-26 13:23:51"},{"id":7,"name":"\u0631\u06cc\u0627\u0636 \u0627\u0644\u0635\u0627\u0644\u062d\u06cc\u0646","category_id":2,"author_id":7,"translate_id":6,"language_id":3,"publisher_id":1,"publishing_date":"2020-02-28","edition":"1","valume_number":1,"ISBN":"12323323","deleted_at":null,"created_at":"2020-02-23 07:12:18","updated_at":"2020-03-26 13:23:51"},{"id":10,"name":"\u0631\u06cc\u0627\u0636 \u0627\u0644\u0635\u0627\u0644\u062d\u06cc\u0646","category_id":2,"author_id":7,"translate_id":6,"language_id":3,"publisher_id":1,"publishing_date":"2020-03-28","edition":"10","valume_number":3,"ISBN":"12323363","deleted_at":null,"created_at":"2020-03-01 12:31:20","updated_at":"2020-03-26 13:20:21"},{"id":11,"name":"\u0633\u0627\u0633\u062a \u0685\u0647 \u062f\u06cc \u061f","category_id":3,"author_id":10,"translate_id":8,"language_id":4,"publisher_id":3,"publishing_date":"2020-03-28","edition":"1","valume_number":1,"ISBN":"SBCDD1","deleted_at":null,"created_at":"2020-03-26 13:17:44","updated_at":"2020-03-26 13:23:51"},{"id":9,"name":"\u0635\u0627\u0644\u062d \u0627\u0646\u0633\u0627\u0646","category_id":1,"author_id":7,"translate_id":4,"language_id":3,"publisher_id":1,"publishing_date":"2020-01-01","edition":"1","valume_number":1,"ISBN":"SBN0098","deleted_at":null,"created_at":"2020-02-25 02:33:49","updated_at":"2020-03-29 16:18:20"},{"id":8,"name":"\u0635\u0631\u0627\u0637 \u0627\u0644\u0645\u0633\u062a\u0642\u06cc\u0645","category_id":1,"author_id":3,"translate_id":4,"language_id":3,"publisher_id":2,"publishing_date":"2020-01-29","edition":"1","valume_number":1,"ISBN":"ISBN003","deleted_at":null,"created_at":"2020-02-23 07:14:03","updated_at":"2020-03-29 16:19:10"},{"id":5,"name":"\u0641\u0636\u0627\u06cc\u0644 \u0627\u0639\u0645\u0627\u0644","category_id":2,"author_id":3,"translate_id":4,"language_id":4,"publisher_id":3,"publishing_date":"2020-02-28","edition":"1","valume_number":2,"ISBN":"ISBN002","deleted_at":null,"created_at":"2020-02-19 08:12:10","updated_at":"2020-03-26 13:23:51"},{"id":3,"name":"\u0641\u06cc \u0638\u0644\u0627\u0644 \u0627\u0644\u0631\u0642\u0631\u0622\u0646","category_id":1,"author_id":3,"translate_id":4,"language_id":3,"publisher_id":2,"publishing_date":"2020-01-29","edition":"1","valume_number":2,"ISBN":"ISBN001","deleted_at":null,"created_at":"2020-03-07 00:00:00","updated_at":"2020-03-27 17:06:48"}]

Sti3bas's avatar

@usmanbasharal well, it seems fine. this.Books should be an array of books objects. What problem do you have?

UsmanBasharmal's avatar

it gives me no data in the table if I put the pagination back the data will be loaded in the table

frankincredible's avatar

What happens if you log this:

axios.get("api/Book").then(
  function(response) {
    console.log(response.data)
  }.bind(this)
)
UsmanBasharmal's avatar

This is the console result [object Object]

 function(response) {
   console.log(response.data)
 }.bind(this)
)
UsmanBasharmal's avatar
UsmanBasharmal
OP
Best Answer
Level 3

I found the solution the problem was there this.Books = data.data; I change it like this.Books = data; it worked

Please or to participate in this conversation.