lukegalea16
2 months ago
26
14
Laravel

Call controller function from another controller

Posted 2 months ago by lukegalea16

Knowing that calling a controller function from another function is not good practice for Laravel, I tried adding a service which can be used to call another function from another controller, however it is not working as expected.

<?php

namespace App\Http\Controllers\api\v1;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services;

//Import models
use App\device_data_message;
use App\device_geo_message;
use App\decoded_data_message;

//This controller handles incoming callbacks/messages - currently from SIGFOX only
class CallbackController extends Controller
{

public function sigfoxstore(Request $Request)
    {                 
        if ($Request->has('data')) {
            $device_data_message = new device_data_message();
            $device_data_message->device = request('device');
            $device_data_message->seqNumber = request('seqNumber');
            $device_data_message->data = request('data'); 
            $device_data_message->bsId = '0';
            $device_data_message->nbRep = '0';
            $device_data_message->snr = '0';
            $device_data_message->rssi = '0';
            $saved = $device_data_message->save();
            $this->decodedDataMessageService->store($Request);
            if($saved){
                echo json_encode(array());
            }            
        }
}

In app\Services I created the following...

<?php 

namespace App\Services;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\data_decoding\v1\DecodedDataMessageController;

//Import models
use App\device_data_message;
use App\device_geo_message;
use App\decoded_data_message;

public function store(Request $Request)
{
   this->DecodedDataMessageController->dataparsingstore($Request);
   echo json_encode(array()); 
}

?>

After saving to device_data_message, I wish to pass the $Request to DecodedDataMessageController to save in another table and I tried to do so by adding a service in between.

Please sign in or create an account to participate in this conversation.