Laravel: Class declaration Compatible error - laravel

I am using laravel 5.6, when create controller and when run controller through route, I am facing error like
Declaration of App\Http\Controllers\XyzController::xyz(Illuminate\Http\Request $request) should be compatible with App\Http\Controllers\Controller::xyz($job)
My Code is
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class XyzController extends Controller
{
public function xyz(Request $request)
{
return view('xyz.xyz');
}
}

Missing route parameter: $job
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class XyzController extends Controller
{
public function xyz(Request $request, $job)
{
return view('xyz.xyz');
}
}

The base Controller that XyzController extends from defines a method named xyz with a different signature than the one you are defining.
You will need to adjust the method in XyzController to match the signature of xyz in the base Controller or adjust the base Controller to have a different signature.
Example of the problem:
class A
{
public function xyz($obj) {}
}
class B extends A
{
public function xyz(Illuminate\Http\Request $request) {}
}
Declaration of B::xyz(Illuminate/Http/Request $request) should be compatible with A::xyz($obj)

You forgot to use controller?
use App\Http\Controllers\Controller as Controller

Related

Laravel :: call method in helper class with constructor

as I mentioned in last question I am beginner in Laravel therefore I need some help ,I have to make helper class to create random Id for some tables ,I create this class with table constructor to recieve deffirent tables :
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\DB;
class RandomId
{
public function __construct($my_table)
{
$this->my_table=$my_table;
}
public function get_id()
{
$id = mt_rand(1000000000, 9999999999);
if($this->check_id($id)){
get_id();
}
return $id;
}
public function check_id($id)
{
$table=DB::table($this->my_table)->where('id',$id)->get();
if (count($course)==0){
return false;
}
return true;
}
}
then I add it as alias in config/app.php
'RandomId' => App\Helpers\RandomId::class,
now I want to call it in controller ,I did somethinf like this but don't work :
<?php
namespace App\Http\Controllers\course;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\RandomId;
class Course_controller extends Controller
{
public function add(Request $request)
{
$id=RandomId::get_id('courses');
dd($id);
}
}
I get this error :Non-static method App\Helpers\RandomId::get_id() should not be called statically
It's because get_id function is not static. You should add static keyword when defining static methods:
public static function get_id()
{
....

Controller not found media model in laravel 8

I tested to using laravel but i get some error unexpected, i already follows tutorial and some video. i think, i do it right. but i get this error. The problem controller not found media model.
Class "App\Media" not found
Route
Route::get('/', [MediaController::class, 'index']);
MediaController
namespace App\Http\Controllers;
use App\Media;
class MediaController extends Controller
{
public function index(){
return Media::all;
}
}
Media model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Media extends Model
{
protected $table = 'media';
public $timestamps = false;
protected $primaryKey = 'Media_ID';
}
Your use statement for your Media model is incorrect. As of Laravel 8 the default namespace for models is App\Models where previously they where in the App namespace.
namespace App\Http\Controllers;
use App\Models\Media;
class MediaController extends Controller
{
public function index(){
return Media::all;
}
}
Ensure that the Media.php file exists in your app/Models directory and is capitalised correctly.
Then in your web.php file, don't forget to include the use statement for your MediaController at the top.
use App\Http\Controllers\MediaController;
Use this code:
Your Controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MediaController extends Controller {
public function index() {
return Media::all;
}
}
Your Route:
Route::get('/', 'MediaController#index');

Custom validation using make:request returns controller not found

Using php artisan make:request StoreUserData I've create my rules to the request:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserData extends FormRequest {
public function rules(){
return [
'name'=>'required|integer',
'surname'=>'required|max:255|string',
];
}
}
And I'm trying to use it in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDataController extends Controller {
public function store(StoreUserData $request){
return 'valid';
}
}
Here the error I get: Class App\Http\Controllers\StoreUserData does not exist.
PS. Is not a problem of routing.
I'm following this documentation since I'm using Laravel 5.6 https://laravel.com/docs/5.6/validation#creating-form-requests
Actually when you use StoreUserData in controller method then you have to import that class otherwise it will assume the class is in App\Http\Controllers namespace and thats why it throw Class App\Http\Controllers\StoreUserData does not exist.
just add the below import at top of your controller class
use App\Http\Requests\StoreUserData

How to use namespaces in case of inheritance?

I am new to OOPS thus want to clarify things. I have this code below which works perfectly fine.
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
Now I don't intend to use use keyword as it is going to be used once also wanted to experiment and thus, I used the code below.
namespace App\Http\Controllers\Admin;
class AdminController extends App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Now the above mentioned code without use keyword throws a fatal error exception. Why does that happen? In theory,I think I am doing exactly what is supposed to be done then why the exception?
You will have to import it from global namespace , below code will work fine
namespace App\Http\Controllers\Admin;
class AdminController extends \App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Use keyword import class from global namespace, but
class AdminController extends App\Http\Controllers\Controller
it will import parent class relative you current namespace (namespace App\Http\Controllers\Admin) , so translated path will be: App\Http\Controllers\Admin\App\Http\Controllers\Controller which is invalid.

Request class injection in repository class

I have following repository class
<?php
namespace App\Repositories;
use App\Customer;
use App\Comment;
use App\CustomerCode;
use Illuminate\Support\Facades\Request;
class CustomerRepository {
use ValidatesRequests;
public function getAll($search=null,Request $request)
{
if($search){
return Customer::where('name','like',"%$search%")->paginate(5);
}
return Customer::paginate(5);
}
}?>
and controller class
<?php
namespace App\Http\Controllers;
use App\Repositories\CustomerRepository;
use Illuminate\Http\Request;
class CustomerController extends Controller{
private $customerRepo;
function __construct(CustomerRepository $customerRepo){
$this->customerRepo = $customerRepo;
}
function index(Request $request){
return $this->customerRepo->getAll($request->input('search',null));
}
}
this gives me error
Type error: Argument 2 passed to
App\Repositories\CustomerRepository::getAll() must be an instance of
Illuminate\Support\Facades\Request, none given, called in
C:\xampp\htdocs\avmaxapi\app\Http\Controllers\CustomerController.php
on line 21
i know i havenot passed 2nd argument but should not it inject automatically
and i donot want to pass request object everytime from controller;
use use Illuminate\Http\Request;
and not: use Illuminate\Support\Facades\Request;
at the following:
<?php
namespace App\Repositories;
use App\Customer;
use App\Comment;
use App\CustomerCode;
use Illuminate\Http\Request;
class CustomerRepository {
use ValidatesRequests;
public function getAll($search=null,Request $request)
{
if($search){
return Customer::where('name','like',"%$search%")->paginate(5);
}
return Customer::paginate(5);
}
}?>

Resources