Skip to content
Advertisement

PHP7/Laravel – Declaration of, Should be compatible with

Currently facing an issue with an old Laravel project I am trying to revive and update. The error we’re receiving is being thrown on line 11 inside of MessagesController like so: “…/app/Http/Controllers/Administrator/MessagesController.php:11”

Declaration of 
AppHttpControllersAdministratorMessagesController::getUsername(IlluminateHttpRequest $request)
should be compatible with 
AppHttpControllersController::getUserName(?AppHttpControllersRequest $request = NULL)

Messages Controller:

namespace AppHttpControllersMembers;

use AppHttpControllersController;
use AppMessage;
use AppUser;
use IlluminateHttpRequest;
use DB;
use Response;

class MessagesController extends Controller
{

    //trimmed for Stackoverflow

    public function getUsername(Request $request)
    {
        $term = $request->input('term');
        $results = array();

        $queries = DB::table('users')->where('username', 'LIKE', '%' . $term . '%')
            ->take(5)
            ->get();

        foreach ($queries as $query) {
            $results[] = [
                'id' => $query->id,
                'value' => $query->username
            ];
        }

        return Response::json($results);
    }
}

Base Controller:

namespace AppHttpControllers;

use IlluminateFoundationBusDispatchesJobs;
use IlluminateRoutingController as BaseController;
use IlluminateFoundationValidationValidatesRequests;
use IlluminateFoundationAuthAccessAuthorizesRequests;
use Sentinel;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected function getUser()
    {
        $user = Sentinel::getUser();
        return $user;
    }

    protected function getUserName(Request $request = null)
    {
        return $this->getUser()->username ?? null;
    }

    protected function getUserID()
    {
        return $this->getUser()->id ?? null;
    }
}

Advertisement

Answer

Your parent Controller has not included use IlluminateHttpRequest;.

Secondly your parent getUserName method, has the request parameter set to nullable. So change your MessagesController to this.

public function getUsername(?Request $request)
{
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement