I think I understand the concept of command in Laravel, in that it's a good place to put reuseable code, that can be called from controllers and the like, but I have a query:
Can I return a value back to the calling method from a command? For example, I have a controller method which creates a user in Active Directory, for which there is a command to do this. If the AD server is unreachable, I want to return a response back to the calling controller method. Is this possible?
It only shows in the documentation how to call a command using dispatch(), but nothing as to whether it can return anything.
And if you cannot return a value, can someone explain the reasoning behind why you wouldn't want to return a value? I know that queued commands may take a while and wouldn't be appropriate to wait for a response, but for commands that should be executed immediately I don't see why you wouldn't want to return a value.
Any help or advice is appreciated.
In the context of the command bus, yes you can return values on non-queued commands. In your command handler method, simply return what you want:
public function handle(){
return 'foobar';
}
And save the result of your dispatch command to a variable:
$my_command_result = $this->dispatch(
new MyCommand();
);
Commands are not for storing reusable code in controllers. Theyre getting renamed to jobs in 5.1 and their main purpose it to work as Cron Jobs. If you have a method to create a user and want to use it in many places, you could store it in the User model.
Related
I know that for some it might be stupid or funny question (but I am newbie) but I need to find know how to properly use DD() method in laravel projects.
For example - I have got tasks to debug some code and functionality in my project (PHP laravel). And it always takes me for ever to find the exact file or folder or code where the problem is.
My mentor says to use DD() method to find things faster (but for learning purposes he didn't explain me a lot about how to actually use it and said to find out my self), but said that I should start with Route (we use backpack as well for our project). So after finding Route (custom.php file) which controller connects to my required route what should I do next? How do I implement dd() method (or as my mentor says dd('call here') method) to fast find what I should be looking for to solve my problem and complete my task? Where should I write this dd() and how should I write it?
Thank you for the answer in advance!
for example I have a:
public function create(): View
{
return view('xxxxxx. \[
//
//
\]);
}
and if I put dd() anywhere in the code, I get error message in my URL :(
first of all ,in Laravel we use dd() before return in order to read any variable.
in controller we often use two kinds of variables : collection(which we get its members via foreach) or singular variable (we get it via its name)for example:$var = 1; dd($var).
notice:
if you are using ajax response you will not be able to see dd() results in page ,you can see the result via network tab in your browser (if u inspect your page).
dd stands for "Dump and Die."
Laravel's dd() function can be defined as a helper function, which is used to dump a variable's contents to the browser and prevent the further script execution.
Example:
dd($users,$variable1,$var2);
You can use dd() in blade
#foreach($users as $user)
#dd($user)
OR
{{dd($user)}}
#endforeach
#dd($var1)
You can read this article, the have more example and comparison
https://shouts.dev/articles/laravel-dd-vs-dump-vs-vardump-vs-printr-with-example
As Laravel is following model-view-controller or MVC design pattern. First go to the route and check which controller is called in the URL with the related URL.
Then go to the controller. **dd**() function is basically a dump and die. you also can do this by **print** or **echo** function too.
Lets assume that I have a controller name ProductController where I have method name index.From where I need to show a list of products in a table.
// in controller
public function index()
{
$products = Products::all();
// here you think ,I need to check whether I am getting the output or
not.
dd( $products );
//Or echo $products;
return view ('product.list',compact('products'));
}
let's suppose you are getting everything but in view when you loop through the products you declare the wrong variable name or mistakenly do some spelling mistakes. and want to see the result.
In view just do the dd() method by the following way:
{{ dd($products) }}
I have this test:
public function test_user_can_access_the_application_page(
{
$user=[
'email'=>'user#user.com',
'password'=>'user1234',
];
$response=$this->call('POST','/login',$user);
$this->assertAuthenticated();
$response->assertStatus(302)
->assertRedirect('/dashboard')
->assertLocation('/dashboard');
$response=$this->call('GET','/application/index');
$response->assertLocation('/application/index');
}
After I log in, it directs me to the dashboard ok until now, but if I want to access the other page after that, I cant. This error comes up.
Expected :'http://mock.test/application/index'
Actual :'http://mock.test'
Aren't multiple calls allowed in the same test, or is another way to access other pages after login?
(Note: It's not possible to use factories for the actingAs so I need to login).
If you can't use factories for actingAs, then you should try with cookie.
Look at the https://github.com/firebase/php-jwt library.
I guess you will need to call the function as an user, since you can only access it logged in. Laravel provides the actingAs() method for such cases.
https://laravel.com/docs/7.x/http-tests#session-and-authentication
You can create a random User who has the permission to log into your app or take a seeded one and call the function acting as the chosen User.
$response=$this->actingAs($user)->call('GET','/application/index');
If you call it without actingAs(), your middleware will redirect you back to the login or home screen (what you defined in the LoginController ).
In my opinion this test case should have its own testing method. I recommend using a test method per route or per use case. It makes your tests clearly arranged and easy to understand.
If you want to be authenticated, the easiest way is to have PHPUnit simulate authentication using the actingAs() method.
This method makes the user authenticated, so you wouldn't want to test the login method with it. You should write your login tests separate from testing the other pages.
To answer your question, yes you can make multiple requests in the same test, but in this case linking the login test to the 'application/index' page likely does not make much sense.
public function test_the_user_can_login()
{
$user = [
'email'=>'user#user.com',
'password'=>'user1234',
];
$response = $this->call('POST','/login',$user);
$this->assertAuthenticated();
$response->assertStatus(302)
->assertRedirect('/dashboard')
->assertLocation('/dashboard');
}
public function test_user_can_access_the_application_page()
{
$user = User::where($email, "user#user.com")->first();
$response = $this->actingAs($user)
->call('GET','/application/index');
$response->assertLocation('/application/index');
}
I experienced that in laravel 8 I use comment #test and involved second test!!! I mean if you use two function for test you must us #test that php artisan test, test both of them.
How how can i transfer data from cron(sheduler) to Controller which make json in laravel
this is my route:
Route::get("/data",'DataController#Data');
and this is my function Data in controller:
public function Data(){
return response()->json(['data'-> User::all()],200);
}
And how can i make cron which will make query to mysql for User, save in variable and pass to my controller?
Update:
I try to use Cache:
in my command(which will be run by crontab)
public function handle()
{
$data = User::All();
Log::info('Data: ' . $data);
Cache::put('data',$data);
Log::info('Done!');
Log::info('Data: ' . Cache::get('data'));
}
problem is fact that First info() printf me good data but when i try to get from cache i got nothink, i try to storage data to File
CACHE_DRIVER=file (from .env)
i run page on windows, and storage folder has permissions to create files
What you are trying to do makes no sense at all. The scheduler is being run through a CLI interface, which has nothing to do with the Controllers.
I would highly recommend you to not mix the Scheduler and Controllers, but instead create Helper-classes for methods, that you might have to reuse.
If you want to run the Controller anyway, you can run it with something like:
(new App\Http\Controllers\DataController())->Data()
Disclaimer: I'm not completely sure on how the response and request classes behaves, when you call a Controller outside of the normal Request-lifecycle.
Simple question, I think.
I have a route that calls a function in a controller. In that controller, I have a call to another controller function.
For example:
$_testing = with(new TestingController)->prepwork($variable1,$variable2);
Inside of TestingController prepwork(), if a condition matches, I
return Response::view(...);
Question - how come that isn't enough? the return just returns control back to the calling function (which makes sense), but how do I tell Laravel - stop what you are doing and output that view.
To make it work, I have to:
$_testing = with(new TestingController)->prepwork($variable1,$variable2);
return $_testing;
That doesn't really work as the prepwork is designed to do some heavy lifting and then output a result model. The view is only kicked off when there is an error.
And YES - I know I can do something like this:
if ($_testing->whatImCheckingForErrors) { return Response::view(...); }
I'm just trying to understand why the return Response::view doesn't end the processing... If that makes sense.
Only the very last return will end processing, because the application will receive this last return and render whatever you send in it.
If you call a method (1), that calls another method (2), that calls a third one (3), Laravel app will receive the View only when your return back from (1).
That's why when you added that last return it worked.
There are things you can do, like:
echo Response::view(...);
die;
It may work sometimes, but
BUT THIS IS REALLY BAD, DO NOT DO THIS UNLESS YOU'RE JUST TESTING THINGS!
Because Laravel won't be able to close everything and flush whatever it needs to and you can compromise your application and even server.
I have a problem, when I try to run this function in my model it does nothing. The print statement prints out.
DELETE FROM child_participantsWHERE Child_Name='test'
andParent_username='tester2'
Which when I run from command line works correctly(the record exists and is deleted). But when I try it from my web application it gives me no error but does not actually delete anything. I know i am passing data correctly because I receive it in my controller and model. What gives?
function remove_child($username, $participant_name)
{
$where = "`Child_Name`='$participant_name' and`Parent_username`='$username'";
$this->db->where($where, null, false);
$this->db->delete($this->child_table);
echo $this->db->last_query();
}
From the documentation:
If you use multiple function calls they will be chained together with AND between them:
Try changing:
$where = "`Child_Name`='$participant_name' and`Parent_username`='$username'";
to
$this->db->where('Child_Name', $participant_name);
$this->db->where('Parent_username', $username);
// translates to WHERE Child_Name='XXX' and Parent_username='XXX'
Hope this helps!
Do you get the same results when you break it out into two where method calls? I would do this over how you are using the where method.
$this->db->where('Child_Name',$participant_name);
$this->db->where('Parent_username',$username);
$this->db->delete($this->child_table);
also, turn on the profiler to see all the queries that are being run to make sure there are not other parts of code we cannot see that might be interfering or a transaction not being committed
$this->output->enable_profiler(TRUE);
Another suggestion is the practice of soft deletes so that way your data is not truly gone and also minimizes how much you need to rely on reconstructing your log file. Also to make simple CRUD operations faster you can use a very simple extension of the base model. One that I have used by recommendation is https://github.com/jamierumbelow/codeigniter-base-model
Check that does your user has delete privilege in the database. if it has than change your
code like this:
function remove_child($username, $participant_name)
{
$this->db->trans_start();
$this->db->where('Child_Name',$participant_name);
$this->db->where('Parent_username',$username);
$this->db->delete($this->child_table);
$this->db->trans_complete();
return TRUE;
}
i hope that this will solve your problem.