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
App\Http\Controllers\Administrator\MessagesController::getUsername(Illuminate\Http\Request $request)
should be compatible with
App\Http\Controllers\Controller::getUserName(?App\Http\Controllers\Request $request = NULL)
Messages Controller:
namespace App\Http\Controllers\Members;
use App\Http\Controllers\Controller;
use App\Message;
use App\User;
use Illuminate\Http\Request;
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 App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
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;
}
}
Your parent Controller
has not included use Illuminate\Http\Request;
.
Secondly your parent getUserName method, has the request parameter set to nullable. So change your MessagesController
to this.
public function getUsername(?Request $request)
{