0

I want to save information posted to a callback URL to a Laravel database. I have created a controller and a route in the api.php file but the posted information is not being saved. When I create a PHP file in the public folder, the info is being saved but I want it to go through a controller. Kindly let me know what I need to do to save the info.

Route: Route::post('/receivesms', 'MessageController@stk');

Controller:

namespace App\Http\Controllers;

use App\Message;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    public function stk(Request $req){

        $message = new Message();
        $message->sender = $req->input('from');
        $message->recipient = $req->input('to');
        $message->message = $req->input('message');
        $message->date_sent = $req->input('date');
        $message->message_id = $req->input('id');
        $message->link_id = $req->input('linkId');
        $message->save();

        return view('welcome');
    }
}

The posted info is as explained here. with the posted data being;

 $from = $_POST['from'];
 $to = $_POST['to'];
 $text = $_POST['text'];
 $date = $_POST['date'];
 $id = $_POST['id'];
 $linkId = $_POST['linkId']; 
4
  • can you describe more what you are trying to achive Commented Nov 30, 2018 at 6:34
  • To simplify the question, given the Post data, how do I save it in a Laravel database. Commented Nov 30, 2018 at 7:13
  • you are already doing it with stk function Commented Nov 30, 2018 at 7:19
  • This is not working. Is this how data from outside Laravel is handled? Commented Nov 30, 2018 at 8:08

1 Answer 1

1

When you put routes in api.php file api is prefixed in the route automatically refer this link Laravel Routing

so if your laravel aplication is running on https://example.com then Route::post('/receivesms', 'MessageController@stk'); will translate to https://example.com/api/receivesms

So make sure your callback your is containing api prefix, It should work.

All the Best

K

Sign up to request clarification or add additional context in comments.

2 Comments

That was it. I added the prefix and it's now working perfectly. Thanks.
Great, You may except this as the answer, would would help other quickly recognize that this helped you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.