Laravel Request::All() Should Not Be Called Statically

Laravel Request::all() Should Not Be Called Statically

The error message is due to the call not going through the Request facade.

Change

use Illuminate\Http\Request;

To

use Request;

and it should start working.

In the config/app.php file, you can find a list of the class aliases. There, you will see that the base class Request has been aliased to the Illuminate\Support\Facades\Request class. Because of this, to use the Request facade in a namespaced file, you need to specify to use the base class: use Request;.

Edit

Since this question seems to get some traffic, I wanted to update the answer a little bit since Laravel 5 was officially released.

While the above is still technically correct and will work, the use Illuminate\Http\Request; statement is included in the new Controller template to help push developers in the direction of using dependency injection versus relying on the Facade.

When injecting the Request object into the constructor (or methods, as available in Laravel 5), it is the Illuminate\Http\Request object that should be injected, and not the Request facade.

So, instead of changing the Controller template to work with the Request facade, it is better recommended to work with the given Controller template and move towards using dependency injection (via constructor or methods).

Example via method

<?php namespace App\Http\Controllers;

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

class UserController extends Controller {

/**
* Store a newly created resource in storage.
*
* @param Illuminate\Http\Request $request
* @return Response
*/
public function store(Request $request) {
$name = $request->input('name');
}
}

Example via constructor

<?php namespace App\Http\Controllers;

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

class UserController extends Controller {

protected $request;

public function __construct(Request $request) {
$this->request = $request;
}

/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store() {
$name = $this->request->input('name');
}
}

Laravel Illuminate\Http\Request::input() should not be called statically

You would need to be using the Facade, Illuminate\Support\Facades\Request also available as Request, if you want to call the method statically like that, since that Facade is the static proxy for the Request, Illuminate\Http\Request, instance.

use Request;
// or
use Illuminate\Support\Facades\Request;

Request::input(...);

Otherwise you probably want to be using an instance of Illuminate\Http\Request since you can have it injected into your Controller methods:

use Illuminate\Http\Request;

public function something(Request $request, ...)
{
...
session(['cor_id' => $request->input('cor_id')]);
...
}

Calling the failed function: 'should not be called statically' in laravel 5.4

To call the test function statically you must define it as a static function, like below. Otherwise, you would have to do something like (new Helper())->test(); which is probably not something you want to do for a simple helper function that doesn't need to access $this. You can read a bit more about the use of static methods in the PHP manual https://www.php.net/manual/en/language.oop5.static.php

namespace App\Helpers;

class Helper
{
public static function test()
{
return 'success';
}
}

Laravel 5 Illuminate\Http\Request has method not allowing static call

The problem is you are using the wrong Request class. You need to import the Facade:

use Illuminate\Support\Facades\Request;

Laravel Facade error: Non-static method should not be called statically

AFAIK, when you use a class without namespace, php assumes the file should be in the same directory as current file. Only when it doesn't find, it calls the __autoload magic method. Laravel's/Composer's resolving logic sits inside __autoload method (this is the place where Laravel resolves the alias which you have registered with it).

In your case, php detects a class with same name residing in its reach so it tries to use that.

Solutions are

  1. Change the Alias to some class which is not current directory. In this case you have already named the facade a different name, so just use that

    // Register alias
    'NationFacade' => App\Facades\NationFacade::class

    // Call it like
    NationFacade::index()

    OR

    // Import
    use App\Facades\NationFacade

    // Call it like
    NationFacade::index()
  2. Use php's namespace with alias instead of Laravel's alias (i am not 100% sure if this will work)

    // Import the facade
    use App\Facades\NationFacade as Nation;

    // Call it like
    Nation::index()

If you still have doubts, please feel free to comment down below

Model::update() should not be called statically

You're using update() wrong.

mass update with conditions:

YourModel::where(/* some conditions */)
->update([
'field1' => 'value1',
'field2' => 'value2',
...
]);

mass update with no conditions

YourModel::query()
->update([
'field1' => 'value1',
'field2' => 'value2',
...
]);

single model update

$model = YourModel::where(/* some conditions */)->first();

$model->update([
'field1' => 'value1',
'field2' => 'value2',
...
]);

// Only accept fillable fields in the update

$model->fill([
'field1' => 'value1',
'field2' => 'value2',
...
])->save();

// Disregard fillable fields in the update

$model->forceFill([
'field1' => 'value1',
'field2' => 'value2',
...
])->save();

Non static method 'load' should not be called statically

add this in your controller :

use Maatwebsite\Excel\Facades\Excel; 

Laravel5 - Non-static method should not be called statically

public function test() is not a static method. When you try to access a static method with Tag::test() it will fail, because.. well the method isn't static.

You have two options:

1) Set your method to static

class Tag extends Model {
public static function test($card_id){
return DB::SELECT(DB::RAW("SELECT name FROM tagmap tm, tags t WHERE t.id = tm.tag_id AND tm.card_id = :card_id"), ['card_id'=>$card_id]);
}
}

2) Invoke it as an instance method by first instantiating your class:

$tag = new Tag();
$tag->test($card_id);


Related Topics



Leave a reply



Submit