Call to undefined method "Builder::getAfterFilters" while upgrading to Laravel 4 - laravel-4

I try to upgrade from Laravel 3 to 4 but i get this error everywhere
Call to undefined method
Illuminate\Database\Query\Builder::getAfterFilters()
Someone know where this can come ?

I had this error too so I'll just post my observations here. It can always help someone !
It appears that getAfterFilters() is a method that is required for all controllers in L4.
If the error says it's not defined, you probably forgot to extend BaseController in your class.
Knowing that, the obvious fix would be to extend BaseController... but you don't nececarly have to if it's not needed.
In my case, my class had to be a valid controller because of a very stupid reason. I was using the following route syntax :
Route::get('sse', 'SSE#deamon');
SSE was not extending BaseController (didn't need to imho)
But this route syntax requires you to use a controller class that extends BaseController... so I changed it for:
Route::get('sse', [function() {
SSE::deamon();
}]);
and it now works without the missing getAfterFilters() error !

Okay, Here is one more thing that might happen...
the method you are running on your controller might not be defined correctly in the route... for example, this is what I did myself:
Route::get('vendors/getData', 'Vendors#getData');
Route::resource("vendors","VendorsController");
which should have been
Route::get('vendors/getData', 'VendorsController#getData');
Route::resource("vendors","VendorsController");
instead! So basically what Amaud said is perfect! my getData function was not extending base controller as I tried running the function of the model instead of the controller! You might want to check that out before pulling your hair out!

I had the same problem. In routes.php, I missed out the Controller word.
My mistake
Route::get('/list-users/{status?}/{page?}', 'User#listUsers');
Should be
Route::get('/list-users/{status?}/{page?}', 'UserController#listUsers');

Related

STI ::find() fails in controller but succeeds in tinker

I'm using MannikJ/laravel-sti but the following code has a different behaviour in tinker and in a controller (assuming the definitions in larevel-sti's documentation, with classes Root, Sub1 and Sub2):
Root::find(1234)
In tinker, it correctly returns an instance of Sub1. But in a controller, it returns null.
I get the correct instance in the controller if I replace the code with:
Sub1::find(1234)
Thanks to the maintainer (cf. issue #1), we found that the problem is that we use constructors in our objects but laravel-sti's trait also does. Constructors are a known issue in PHP with traits…

Trying to understand the view method and general method calling in laravel

I am a new to laravel and trying to understand where the view method comes from and what mechanism allows it to be shown in the web.php folder in laravel.
For example :
Route::get('/', function () { return view('welcome'); })
I guess the view function is defined in some class. Bu which class is it and where is that class made reference to in order to access its method?
Thanks a lot if you can help me understanding this!
In most IDEs you can hold CTRL and left-click the function to view it's definition. view() is not defined in a class. It comes from a file called helpers.php.
This file is included at the beginning, so its functions can be used afterwards.
PHP is not only object oriented. Procedural and object oriented programming can be mixed together.
What I do usually in these cases is to search in the whole project (and remember to include vendor directory in your search) for: "function YOUR_FUNCTION_NAME" because somewhere in PHP there must be that function declared, whether is in a class or in a simple .php file.
view() method is a helper method inside src/Illuminate/Foundation/helpers.php. All the methods that declare here will be available everywhere inside Laravel application. You can check view() method here

Customizable model file for a Laravel 5 package

I'm developing a Laravel 5 package where I have a "Member" model which currently extends App\User model. I would like to know the best practice to let any developer use a custom "Member" model instead of the one from the package. This is for example to allow a developer use another table.
One approach that seems to work without having done a deep test with it is to make an alias in my package service provider in the register() method:
$MemberModel = 'MyVendor\MyPackage\Member';
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('MyMember', $MemberModel);
In this case I have to:
Replace all entries in the code from the original class to the alias
Delete all php "use" entries related to it
Get the value of $MemberModel from a config file or the database
But I don't know if it is a good way to solve it or It may cause any conflict.
Is there any other and better approach for this goal? Thanks in advance!
I finally had to test by myself this approach without haven't read the solution anywhere else, but anyway everything seems to work fine in my source code.
If anyone is looking for doing anything similar, the code example in my question works because the $MemberModel is defined with a value. If you want to get that value from a Model instance, as me, you have to add that code in the boot() method of the service provider.

Laravel 5.1 index & create authorization

I'm using Laravel 5.1's authorization features, documented here. My controllers implement AuthorizesRequests and I have my policies set up connecting policies to their models to create an ACL of sorts. In my controllers, I'm checking for authorization in each method. For example, in an 'AgencyController' the 'update' method calls $this->authorize($agency), which then checks my AgencyPolicy's update method to know rather or not the current user is allowed to update the agency, just as described in the documentation. This works the way I want it to.
However, what I can't seem to figure out is how to use authorization for other methods like index() and create() where there isn't a specific model being used. Calling $this->authorize('index') seems to return false, even if I have an index($user) function in my policy class that only returns true.
I'm new to using Laravel's authorization helpers, so I might be going about this wrong or missing something obvious. Any help pointing me in the right direction would be gretaly appreciated!
You have to pass it the class name of the model you're checking:
$this->authorize('index', Agency::class);
With some help from someone in the Laravel slack group I was able to find the answer to this myself.
Without an instance of the model, the authorize() calls couldn't map to the correct policies. But by simply passing the class to them, it is able to and works.
For example, instead of calling $this->authorize('index') in my controller's index method, I'm now calling $this->authorize('index', Agency::class) to give it the correct model to use.

Load a library in a model in CodeIgniter

Why won't my model load the encryption library?
class User_model extends Model {
function User_model() {
parent::Model();
$this->check_login();
}
function check_login() {
$this->load->library('encrypt');
$email = $this->encrypt->decode($email);
....
}
}
This giving me a PHP error: Call to a member function decode() on a non-object on line X -- where X is the $this->encrypt->decode($email); line?
Edited to show that the problem was that check_login was called from the constructor
You don't need to load the library in the MODEL, MODELS are always called from the CONTROLLERS so you just have to load the Libraries in the Controller, and the functions will be available in the models called from him!
Regards,
Pedro
Libraries should automatically be assigned to the Model instance so it should work fine.
Remember if you can't access the super-global you can always use $ci =& get_instance() to grab it at no extra cost to your memory.
But still... your code example should work >.<
I was calling check_login from within the constructor, and that was causing the problems.
The solution is to call $this->_assign_libraries(); right after loading a library in a constructor.
Thanks to this codeignitor forum thread:
http://codeigniter.com/forums/viewthread/145537/
I have tried many of them, but in the end, what I did is this in a model:
$this->load->library('mylib');
$mylib= new Mylib();
$mylib->somemethod();
This works for me.
you might want to change the name of the object for the library you are loading
beacause CI also has got the encrypt class
just do
$this->load->library('encrypt',NULL,'myencryptobj');
$this->myencryptobj->yourfunction();
Hope this helps
i was also facing issue about facebook api, then I tried required_once the lib file of facebook in model. it worked for me.
require_once "application/libraries/facebook.php";
then make its object if you need.

Resources