So this is my line of code
public function getVisitedCountriesCountAttribute()
{
$this->dispatch(new CalculateTotalCountriesVisited($this));
return $this->total_countries;
}
here the dispatch method throws an error saying
Call to undefined method Illuminate\Database\Query\Builder::dispatch() (View: /Users/mereeva/sites/rdb-ws/resources/views/users/trips/status.blade.php)
I don't seem to understand what could be the reason behind this. i followed all the steps in https://laravel.com/docs/5.1/queues
Any help will be appreciated. Thanks
If you want to dispatch from your model you have to use the trait Illuminate\Foundation\Bus\DispatchesJobs; in it, otherwise you won't be able to do $this->dispatch(....
Another way without using this trait would be to use the dispatch helper:
dispatch(new CalculateTotalCountriesVisited($this));
Which would automatically resolve a Dispatcher from the app container.
Related
I have defined a function in helper for saving data using model but it throws "__toString() must not throw an exception" error.
may be it is the problem of magic function. Any help is appreciated.
I'm currently in the process of writing unit tests for a Trait. This Trait included a method called registryPersist() which in tern calls a Facade method called Registry::persist(). This method as the name implies saves the registry value to the database and then calls Registry::set() in order to set the registry to the local scope. It is a fairly straight forward procedure.
I tried following the suggestion from the Laravel docs and mocking the Registry::persist() method with the following code:
Registry::shouldReceive('persist')->with('test', 'ok', 'model.users')
However given that persist also calls set i get the BadMethodCallException exception because set is not mocked. (Received RegistryAccessControl::set(), but no expectations were specified)
The official mockery docs suggest to create a mock instance how ever this will not work because when i actually try to call the Trait method, the facade is not mocked.
$user = factory(User::class)->create();
$user->registrySet('test', 'ok');
The Trait Method:
public function registryPersist(string $name, $value): void
{
Registry::persist(
$name,
$value,
$this->getNamespace()
);
}
Is there any way to effectively test this method?
Furthermore, given that registrySet() only forwards the parameters to the Facade do you think that the test that im trying to write is a good idea?
Thanks in advance.
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');
I want to call a method before the execution of every and each controller's method. I don't want to go and call the method in every method. I just want to call it from one place and it will be called before any method of any controller in magento.
And I am sure we can do this but I don't know how it can be accomplished.
Please provide your suggestions.
Hope we can resolve this or may some expert guys already resolved this.
Thanks.
You need to create an Observer that binds to the controller_action_predispatch Event. That will fire before every controller in the Magento codebase. There's a useful wiki page here that walks you through the process.
You have to create a method called preDispatch in your controller. This method is executed before the requested controller action.
something like:
public function preDispatch()
{
parent::preDispatch();
//my code here
}
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.