Event::listen not catching laravel.query - events

I am pretty new to Laravel. I am working on a REST api and was trying to look at the queries that were being generated from the models. In my routes I have a route group set up.
Route::(["before" => "auth", function()
{
Route::model("juror_subject", "JurorSubject");
Route::get("juror_subject", [
"as" => "juror_subject/index"
"uses" => "JurorSubjectController#index"
]);
});
I wanted to see what query was actually being run. I was watching a video by Jeffrey Way and he mentions that you can use Event::listen to see the query like so.
In routes:
Event::listen('laravel.query', function($sql){
var_dump($sql);
});
However, when I load the url:
localhost:8080/api/juror_subject
It returns the json response and never seems to fire the laravel.query event.
Am I missing some element that is needed to get event listeners to work properly? Is the type of routing I am using not firing a query? If so, how would I go about dumping the queries using a route group?

laravel.query is for Laravel 3 , use illuminate.query instead, also check this answer for more details.
Moreover, call DB::getQueryLog() to get all ran queries ( no need for listener), or use this package which is pretty neat.

Related

zend expressive - middleware checks if there is a next one available?

In my routes.global.php I've this one in routes.
[
'name' => 'test',
'path' => '/test',
'middleware' => [App\Action\Test1Action::class, App\Action\Test2Action::class],
'allowed_methods' => ['GET'],
],
And I've this return $next($request, new JsonResponse($data)); at the end of Test1Action class so it will send the data to the next action.
But is there a way inside Test1Action to check if there's another action after?
Maybe there's another way so I can do the above return if there is one after or return the json response rite away.
return new JsonResponse($data);
That way I can either use Test1Action alone or plug it in before other action.
I tried few options but didn't work. Any help will be great. Thanks.
Maybe you can check $next if it's null or not. But it might always be set, I've never tried it. However in Expressive 2 it's always set and it changes to Delegates. Also the last middleware will always be the NotFoundHandler.
Since you develop your application yourself you know the order of middleware and Actions. I would let Test1Action middleware do it's thing, add the result to the $request as an attribute and let the next middleware figure out if the data was set or not. If the data wasn't set than skip it and execute the next middleware in line. It makes it a lot easier.

How exactly do we use queue in Laravel 5.0?

I'm a new Laravel developer. I tried to use queues but don't know where to start. I did not find any matches after a google search on this.
This is how I tried to use a queue on my controller:
$date = Carbon::now()->addMinute(1);
Queue::later($date, function(){
CategoryTags::create([
'name' => Helper::nowTimestamp()
]);
});
The function inside Queue gets executed and gives an error like Queue does not exist.
I want to call a queue hourly, what am I doing wrong here? How do I implement this?
Looks like you forgot to define namespace.
use Queue;

Laravel routing group for muliple domains with wildcards, how to handle domain suffixes?

I have 3 domains which, on my local server, take the format:
mydomainfirst.local
mydomainsecond.local
mydomainthird.local
In my routes.php file, I have the following:
Route::group(array('domain' => '{domain}.{suffix}'), function() {
Route::get('/', 'Primary#initialize');
});
The idea is to take the $domain variable in my controller and extract the first/second/third part from it, which works fine. However, now my site is online, this routing file no longer works and throws a Http-not-found exception. After a while, I have figured that the problem is that the domains have now taken the format mydomainfirst.co.uk. Because there are now 2 parts to the domain suffix, it seems I need to do this:
Route::group(array('domain' => '{domain}.{a}.{b}'), function() {
Route::get('/', 'Primary#initialize');
});
To get it to work. Which is stupid. How can I tell it to just accept any suffix? Is there an 'anything' wildcard I can use?
I have tried a few things like this answer but it doesn't work with route groups.
EDIT: It seems the Enhanced Router package would at least enable me to add a where clause to the route group, but does it solve the problem of how to set a wildcard that will match an indeterminate number of segments? I need something like:
{domain}.{anything}
That will match both:
mydomainfirst.local AND mydomainfirst.co.uk
?
Ok let me first say that the code of this package actually looks good and should work. Even if you can't get it running by installing you could take the files and use the code with your own service provider etc.
But there's also a kind of quick and dirty solution. (Actually the package does it pretty similar, but it looks a lot nicer ;))
First, here's how you can do it for one route:
Route::group(array('domain' => '{domain}.{tld}'), function(){
Route::get('/', 'Primary#initialize')->where('tld', '.*');
});
So the where condition for the route group actually gets set on the individual route.
Of course you don't want to do this for every route inside that group so you can use a simple foreach loop:
Route::group(array('domain' => '{domain}.{tld}'), function($group){
Route::get('/', 'Primary#initialize');
foreach($group->getRoutes() as $route){
$route->where('tld', '.*');
}
});
Note: The loop needs to come after all routes. Otherwise they won't registered and therefore not returned with getRoutes()

makes a script run for every call to a package Laravel4

I am developing an application with Laravel 4 framework, I developed an admin package for my application,
Question:
how can I make a piece of code executable for every single call to one of the routes of this specific package? where should I put this piece of code?
Use a route filter.
Route::filter('admin', function () {
// do stuff
});
Or if you want this to be revolved out of the IoC container:
Route::filter('admin', 'Vendor\Package\Filters\SomeFilter');
Then bind it in your routes file:
Route::get("/admin", ["before" => "admin", "uses" => "SomeController#method"]);
Though you should consider using an event handler instead of this, as it seems like that's actually what you want, rather than "run this code when this route is hit".
Normally you should be saying "I want this code to be ran when this happens" when dealing with a package, which would be an event.
Define your filter like;
Route::filter('filter', function () {
// do stuff
});
or
Route::filter('filter', 'Vendor\Package\Filters\SomeFilter');
And attach to the group, and define your route within it like so;
Route::group(array('before' => 'filter'), function(){
//Define your routes here
});

Basset 4 (pre-beta)

I'm using Basset 4 to manage assets.
In the config file i'm declaring a collection 'admin'
return array(
'collections' => array(
'admin' => function($collection)
{
$collection->directory('assets/js', function($collection)
{
$collection->add('vendor/jquery-1.9.1.min.js');
});
},
),
...
)
later in a view, I would like to add an extra file in admin collection.
I've try the following code, but it doesn't work:
Basset::collection('admin', function($collection)
{
$collection->add('function.js');
});
Is there a way to add file into a collection from a view or from a controller?
Thank you
Basset isn't really designed to work like that. You should be defining all your assets within that initial call, even though the ability to add assets throughout the execution of an application is possible, it's not recommended.
When building a collection assets that are added for a particular route won't be available to the builder since Artisan doesn't fire any routes, etc.
Adjusting a collection in numerous places can often lead to confusion further down the line.
I know this isn't ideal as you're probably looking at implementing page specific JavaScript, correct? I've thought about it but can't really think of a clean solution (suggestions?), although I've heard of people assigning a unique ID to the body or perhaps some classes that their JavaScript can then attach themselves to.
It's not brilliant but that's the best I can give you at the moment.

Resources