0

I need to get data sent from android device.Those sent data should be stored in my table called 'leave' where there are three fields they are 'id','reason' and 'description'. What i am able to do till now is i have set up my route as:

    Route::any('jsontest','JsonController@JsonFunction');

And in my controller:

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Input;

   class JsonController extends Controller
    {
   public function jsonFunction(request $request){
   $requestObj=Input::all();
   if(count($requestObj) > 0)
   {
   return response()->json(['success'=>true,'request_param'=>$requestObj]);
   }
   else
   {
  return response()->json(['success'=>false,'error'=>'send some data']);

   }
   }

   }

now when I hit this in browser : http://rentma.net/attendance/public/jsontest then this comes:

    {"success":false,"error":"send some data"}

which is the message from else part.And when I pass parameters from url like this: http://rentma.net/attendance/public/jsontest?name=suzan then this comes:

    {"success":true,"request_param":{"name":"suzan"}}

you can try it also since it is already in server. but what i want is i want datas to be passed from android device and show that data in view. How can I do that? can anyone help?

Tekraj Shrestha
  • 1,228
  • 3
  • 20
  • 48

1 Answers1

1

I think the best is to check laravel events

there was something like Event::listen() for inserting in db check here

once event is triggered, make an ajax call to get the data you need and modify the view to show it

$.ajax({
  url: 'test-grab',
  data: {token: _token},
  success: function(response){
      $('body').html(response) // or if it's to a class/id mark the class
  }
});
Community
  • 1
  • 1
  • hello Martin Kirilov I did whatever I could and I coudnt make it work. can you please provide me a tutorial a very simple one could work. just to receive data from android and to display in view or just to store in database. It would be great of you. – Tekraj Shrestha Mar 16 '17 at 15:08
  • yeah, https://www.youtube.com/watch?v=TM1Suk7sLbc - part1 https://www.youtube.com/watch?v=H-SE1m_A-SA - part2 it is native php you should just use laravel method instead of regular php. It is good to know that if you are working ot localhost, on your android emulator, localhost won't be on 127.0.0.1 so if you have setted virtual host it might not work. You may want to check 10.0.2.2 to connect to VH from android device to localhost, or to open port forwarding if you are using router. After you make the request you'll be able to insert into db and then using events you can display changes –  Mar 16 '17 at 21:00
  • Thank you. I will try give it a shot. And I have already pushed this whole project in server itself so sending data from android wouldn't be a problem. – Tekraj Shrestha Mar 17 '17 at 05:29