Home > database >  Laravel api with paginate() does not work with reach native
Laravel api with paginate() does not work with reach native

Time:02-21

I have a laravel api in which I added ->paginate() so I can paginate, and use it by fetching the laravel api in my react native app, so as to apply a load more data when the app is scrolled.

This my laravel api

public function allPerson(Request $request) {
  return Person::where('active', 'online')->paginate($request->input('results', 10));
}

Api URL

https://housedata.com/allperson?page=2

React Native

  const [personData, setPerson] = React.useState([]);

    const getPerson = () => {
        axios.get('https://housedata.com/allperson?page=2')
            .then(response => {
                setPerson(response.data)    
                console.log(response.data);
            });
    }

    useEffect(() => {
        getPerson();
    }, []);


    <FlatList
        data={personData}
        keyExtractor={item => item.id}
        renderItem={({ item }) => {
          return (
            <View>
              <Text>{item.person_name}</Text>
            </View>
          );
        }}
      />

When I check console.log(response.data); I see the data is been fetch.

Also when I change ->paginate() to get() it is showing the data and working, how can I fix this.

Thanks

CodePudding user response:

the get and paginate method returns data differently the get method returns the data directly where as the paginate method will return a paginate object which contains a data property so to access it you have to do

response.data.data

axios.get('https://housedata.com/allperson?page=2')
        .then(response => {
            setPerson(response.data.data)    // <-- change this
            console.log(response.data.data);
        });
  • Related