Tinker, Artisan, & CLI, with Nuno Maduro

Matt Stauffer:
Welcome back to Laravel Podcast season four. Today we're talking to Nuno Maduro, the creator of Laravel Zero, and Collision, and lots of other great tools about interacting with Laravel from the command line Artisan, Tinker and much more. Stay tuned.

Matt Stauffer:
All right. Welcome back to the Laravel Podcast season four, where every single episode is about a single topic. Today our topic is about the command line with Laravel. That's going to be Artisan, and Tinker and some other standalone tools. And my guest is Nuno Maduro, who is a Laravel employee. He also created Collision, which you've probably seen, which is a command line air handler, which is integrated in a Laravel core. He created Laravel Zero, which I talk about all the time. We'll talk about later. He also created a testing framework called pest and quite a few other things. So I'm going to let Nuno introduce himself though.

Matt Stauffer:
So Nuno as I start with everybody, when you meet somebody at the grocery store or if these days they're ... I don't know if you guys actually go to the grocery store there, we don't in the US as much as we used to for sure, what do you tell people that you do?

Nuno Maduro:
That's a great question. Honestly, I just say that I work with computers and I do websites. People usually think, "What do you mean? You do websites, but what do you do exactly on that?" I just say, "I don't know how to explain to you. I just do websites. People can click on buttons and, I don't know, do stuff with websites." I don't have a great explanation for it, I'd say.

Matt Stauffer:
And yours is uniquely difficult because working for Laravel I think like for me, I get to say, "Okay, well, people pay my company to build this certain type of web application." What's a web application? Okay. But for you, you work for Laravel. So if you were talking to somebody who's in the programming industry and actually understand some of these concepts, how would you tell them what is Laravel, what do you do at Laravel, all that kind of stuff?

Nuno Maduro:
Yeah. So usually what I say is that when you develop a website locally, my job at Laravel is to make sure you can deploy that website to a server that will basically make your website available in the world because it's basically what I do with Laravel, right? I work with products like Forge and Vapor. That's where I spend the most of my time. That's what I try to explain to people is that you do the website, then I make sure that that website reaches the entire world.

Matt Stauffer:
I love that. That's awesome. And so you have created Collision, which is a beautiful kind of command line air handler. You created Laravel Zero, which is a tool to use the Laravel framework to create your own standalone tools. You also created Pest, and I'm literally just reading off your Twitter bio because I know there's other stuff that's not here. And then there's one that I don't think people hear about a lot, which we talked about, I think last time, which was Larastan, is that still something you're doing a lot with?

Nuno Maduro:
Yeah, correct. So Larastan at the beginning, I did more of it, but since then ... so first of all Larastan, for people who don't know, it's kind of static analysis tool for a Laravel like to try to find bugs in your code before actually run them. So at the beginning I was actually developing a lot on Larastan, but then some external contributors make sure I don't spend much time on it. And they are actually doing all the work for me. So yeah, Larastan is still a thing. He has like millions of downloads, but I don't spend that much time on it, which is a good thing.

Matt Stauffer:
It's cool when you get to a point where it kind of has a life of its own, right?

Nuno Maduro:
Yeah. That's the best part.

Matt Stauffer:
That's awesome. All right. So today we're talking about the command line. So I actually talked about this beforehand and we said, "Well, we got a lot of different aspects of the command line here. We've got Tinker, and we've got Artisan, and we've got what is a command line in general?" So what we agreed on is that we're going to start just generally from the idea of the command line and then later we'll dive into what specific tools are there. So if you had to describe the idea of the command line to a five-year-old, how would you do that?

Nuno Maduro:
So I did my best trying to find a sentence that resembles what the command line is to a five-year-old. In my way of describing it would be it's the simplest way for a person that work with computers to actually interact with the computer itself. So in my opinion, like most of the people interact with computers using buttons, graphics, or images. But the reality is for people like me and you, Matt, we just like to use the console for stuff. If you want a copy of a folder from one place to the other, you just use a CP command instead of actually drag things around. So yeah, that is the explanation I found for this. What do you think?

Matt Stauffer:
I like that one. I think one of the things I think about is, because you could say, "Well, which version is simpler?" So maybe dragging a simpler to learn. But if you think about simple in terms of like how close is what I'm doing to the native thing that a computer speaks? It's simplest in terms of there's the least layers between me and the computer. It's the closest to the metal version of it or something where it's like, I'm as close to speaking the language that, that computer understands as I possibly can get. So it totally makes sense. I like that. Because I was trying to think I'm like, because when I tell people about the command line, I'm often talking to people who are older, I'm like, "Oh, you've seen those screens with the black and green or you've seen the thing in the matrix." That doesn't work with the five-year-old, right?

Nuno Maduro:
Yeah. Yeah.

Matt Stauffer:
Those references don't work. So I love that. I think that's really good.

Nuno Maduro:
Still about command line for experts like us, I also think it's like a common language that everyone that works in IT understands, you know what I mean?

Matt Stauffer:
Yeah.

Nuno Maduro:
But there is not that much with UI that you can do with a command line. Correct?

Matt Stauffer:
Yeah.

Nuno Maduro:
You can display text, you can display color sometimes. The best that you can do out of it is like a table. So everyone can understand those things, which makes things so much easier. In the other sense you have, like if you had to develop a UI for stuff, everything would be so much complicated. So it makes things so much easier, I would say.

Matt Stauffer:
Yeah, it totally makes sense. So we have these additional kind of concepts in the Laravel world, which is how we are interacting with Laravel through the command line, which is kind of why I grouped these all into an episode. It's also why you're the person here because you've been interacting with so many different aspects of this. So what we were talking about is we've got Tinker, we've got Artisan, and then we've got kind of custom one off applications that aren't actually in Laravel, but are in the Laravel ecosystem. Could you talk a little bit about, first of all, just what Tinker and Artisan are?

Nuno Maduro:
Yeah. So basically Tinker is what we call a Read Eval Print Loop and basically what it does, to make it simple, is that every time you type something on the console, Tinker will evaluate that and actually run that code against Laravel. So for example, imagine that you want to display just all the users you have in your database, you could write using Tinker in your command line stuff such as user all. And then you would get all the users in your application. So basically Tinker is the fastest way to interact with your application using code, just like normal code. So I think people usually before Tinker, what they used to do is that they probably would create the fake route just to execute some kind of code. Tinker is quite good at it, which is basically you write a little bit of code in your console and that code will be run against your application and do what is meant to do, which is quite nice.

Matt Stauffer:
And you suggested, this and I'm actually going to switch up what I said and actually go through with what you suggested, what is a really common use case for Tinker for you?

Nuno Maduro:
Well, I use it quite a lot to use eloquent. So, for example, if I want to see find a user by a specific query, I usually use eloquent for that just because it's more familiar to me to use eloquent code to find users on the database, for example, or to do even a complex SQL query. Another example is that I just want to see how this specific code behaves in production. So let's imagine that you want to run a specific command job in production and you also don't want like to commit code to deploy or just to ad coded, for example, in-

Matt Stauffer:
Application testing. Just to test it.

Nuno Maduro:
-application coding production, and you just want to run a specific job in production. You can quickly do dispatch the passed the class job and that's it. So that's also another common case I use Tinker for. So yeah, Tinker for me, I do think in the Laravel skeleton is there by default is like a production dependency because I actually use it in production.

Matt Stauffer:
Yeah. Yeah. I use it in production a lot more than I use it locally, actually.

Nuno Maduro:
Yeah. What do you use locally, for example, if you want to quickly check-

Matt Stauffer:
Usually if I'm doing anything quickly in Tinker, often it has to do with seeding things and stuff like that when I need the local work. So if I'm doing checks locally, it's very often that I'm in the middle of working on something. So it's like you said, I just add that to the route and then just refresh the page. Whereas in production it's like you said, if you want to check something quickly, you don't want to modify that code on production because that's not a good way to do things. Quickly modifying a route locally just doesn't bother me that much. You know what I mean?

Nuno Maduro:
Yeah. Yeah.

Matt Stauffer:
I'll still use Tinker and I'll still use Tinker well as well, which we can talk about in a second. But then the most common use case for Tinker for me locally is I spun up a new version of website and I want to create the first user, but I almost always do that in Tinker.

Nuno Maduro:
In Tinker. Yeah. I think for people who quickly want to see what the code does very quickly, I think you have some people that indeed use TinkerWell locally, but also think that some of the people just use the web routes folder just as you have said. Right?

Matt Stauffer:
Yeah.

Nuno Maduro:
I think there is also some people right now using Marcel's tool, TinkerWell, which basically is like a UI where you can put some code and will behave like Tinker on the console, which is quite nice. I've been using TinkerWell a little bit as well locally right now, but you cannot use TinkerWell ... I mean, I think you can actually. I'm mistaken here. I think Marcel has developed some kind of SSH feature, which you can run TinkerWell as well in production.

Matt Stauffer:
Yeah. Yeah. I have not used that, but I agree. So for anybody who doesn't know, TinkerWell is like a downloadable app. It's like Electron where it's actually running on your machine and on the left side, you're seeing your code and on the right side, you're seeing the output of your code. I use TinkerWell more often than anything just to test how something works in Laravel in general, not on this specific application. But if I'm just saying, "You know what? If I collect this and then map that, and then spread this, whatever, how's that going to turn out?" So I think it's a great tool for that.

Matt Stauffer:
And for anyone not familiar with English as a first language, Tinker means to play around with something. To Tinker with something it's often compared to people who are like inventors or people who are working in the garage. So the idea behind Tinker is that when you want to Tinker with something, you want to try something out, you can kind of go in there and just Tinker with it. The funny thing is I didn't Google, but I searched for TinkerWell on DuckDuckGo. And it says, "Are you sure you didn't mean Tinker Bell?" Though TinkerWell does come up first, but everything after that is Tinker Bell.

Nuno Maduro:
Yeah. You were saying that you just want to make sure that your the code works in the standard Laravel application. You also sometimes, I don't know, you want to tweet a quick tip about Laravel and you want to make sure that tip actually works as expected or doesn't have like a missing comma on the right. And you just want to copy paste that snippet on TinkerWell or just Tinker and make sure it works.

Matt Stauffer:
Yeah, absolutely. All right. So that's Tinker. So then we have Artisan, can you give us the high level introduction to what Artisan is?

Nuno Maduro:
Yeah. And for people to understand the context of what we are talking about, I think, Matt, you already explained it, but yeah, we are talking about command line and in the command line in Laravel eloquent system, we have three big groups. One of them being Tinker, which we just talked about it, and we have Artisan, which is a kind of the entry point to Laravel, but using the console. So to make it simple, when you have a Laravel application, basically you have two kernels, right? One of them is the HTTP kernel, which is the HTTP entry point for your Laravel applications. So imagine that you have a route which displays all the users, for example, that goes through the HTTP kernel. Now in Laravel you also have another kernel, which is a console kernel, which is used by Artisan.

Nuno Maduro:
So Artisan is like the entry point for Laravel, but it uses the console. So basically you can type PHP Artisan and click on enter, and you will see the least of available commands on the console to interact with your Laravel application. Now by default, when you install Laravel, we have a bunch of commands like PHP Artisan Down, which is basically put your application down for a few minutes. You have PHP Artisan Up to put again up your application and many more, right?

Matt Stauffer:
Yep.

Nuno Maduro:
So one cool thing about Artisan is that you can create your custom Artisan commands. So basically on this predefined list you can add some commands on your own to that list. And the use case for this would be, for example, imagine that you have a very specific action in your application that you want to perform manually from time to time. And that action could be, for example, perform a backup. I think this is the best example. So imagine that you want to perform a backup and you want that backup to be manually. So you can just go to your application in type PHP Artisan Backup, and that will back up your application. There is a lot of stuff into this as well, because you can use scheduler and et cetera. I don't know, Matt, if you want to enter in those details, but yeah, I think this would be a quick explanation about Artisan.

Matt Stauffer:
Nuno, I'd actually love if you were to give just like a high level introduction of the scheduler and everything like that. So please do share about that.

Nuno Maduro:
Yeah. Yeah. So as I was saying, you can add commands and execute them manually, but some of those commands you actually want to execute them from a predefined time to time. So imagine that you want to execute a backup, but instead of doing manually, doing every week, so you can define on the common class itself, a schedule method that says, "Oh, I want to schedule these to run every week." And then every week you can say, for example, every day or every minute, even. So this is a very powerful feature of Laravel that I'm not sure if it exists in any other framework in the world, which allows you to run a console command from using a specified time that you put in the class. It's very nice. So that's what schedule is about.

Matt Stauffer:
Yeah. Okay. So Artisan is sort of the entry point to Laravel from the command line. I love that you paired it saying like your HTTP routes are your entry line from the web, from the HTTP. And then these are your entry points. There's a whole bunch of prebuilt commands, like you said, but you can also make your own commands. So what does that third group? You said the first group is Tinker, second group is Artisan. What's that third group?

Nuno Maduro:
Well, I think third group is the group that I'm most involve with, which has made me create the Laravel Zero framework. So the third group is like the standalone CLI tools. I know that Matt, you have a love with this kind of-

Matt Stauffer:
Oh, I love them. I really do.

Nuno Maduro:
So standalone CLI tools is kind of tools that you develop, but doesn't have the web component on it or they are not just a simple script, I would say. So standalone tools, it's kind of the fastest way to you to develop something to help your team. So, for example, imagine that your company sees something that can be automated using the CLI. And a common use case for this would be, you want every develop to have the same setup, for example.

Nuno Maduro:
Now there are two ways of doing this. You can execute like commands locally in every machine, or you can just develop a CLI tool that does that for you, for example. I actually have an example here, which is Takeout, which is developed by taken by Matt and your colleagues, I would say. And Takeout, it's very nice because it's exactly the reason why I developed Laravel Zero, which is a command line tool that makes the developing setup be the same for everyone. It uses Docker behind the scenes, which is even more funnier because you don't have to have Docker knowledge to actually use Takeout.

Nuno Maduro:
Now, Takeout is just an example, but we can talk about another one, Expose by Marcel Pociot. So Expose is a CLI tool, a standalone tool, that you can use in our local development, and it will spin up or kind of expose your local web project to the world, which is quite nice. You don't have to have knowledge about any specific technology. You just run Expose and then you have your app site share with the world, which is quite as nice. And really the purpose of these CLI tools, which is making something very easy from the command line, right?

Matt Stauffer:
Yeah. Yeah. So Laravel Zero, for those who aren't familiar, it's not an official Laravel project, but it is basically Laravel bundled and configured in such a way where it's targeted specifically for making these command line only apps. So it is the majority of the Laravel framework with some new niceties on top of it that make it easier to build command line stuff. So when I'm building these command line tools like Takeout or Lambo or whatever else, I've built them as bash scripts in the past. And that's a lot of not my normal familiar programming language. I can't write tests for it and everything like that. So Laravel Zero allows me to build these command line tools, even with like really nice interactive menus that are color coded and everything, but all still writing Laravel code and with the access to Laravel's collection pipeline and all these various other things, which is really wonderful, and HTTP library and everything.

Nuno Maduro:
And actually funny use story about Laravel Zero is that I was actually doing a lot of tooling at the company I was working for at the time. I was looking at that command tools and I thought, "Wouldn't be great to actually you have Laravel to actually develop these command line applications?" As you said, it's so much nicer to use Laravel than just bash scripts or use Laravel than standalone or normal PHP. So that's why the reason why I developed Laravel Zero. It's kind of a skeleton to get started easily developing these standalone CLI tools. And also, you mentioned something which is like Laravel and it has this cool things to for the CLI. You mentioned it this way, but there is a little bit more into it that I would like to go a little bit deeper on that if you don't mind.

Nuno Maduro:
So one cool thing is that it's actually like 30% of Laravel I would say behind the scene. So we don't actually have all the level code on the vendor folder. We don't have the HTTP layer, the kernel HTTP layer that got stripped out. We just have really the console aspects of Laravel which is quite nice. And one cool thing about Laravel Zero as well is it solves a problem that I had when I was developing standalone tools. And that problem is about distribution and you face it sometimes in Takeout, in Lambo. We talk about it, right?

Matt Stauffer:
Yeah.

Nuno Maduro:
So the problem about this solution in CLI tools is that you want to be able to share that CLI tool across the world with your team or even with open source. And you want to be able to share that tool. Now, usually the way that people like to install tools and/or CLI tools and PHP do just the composer global require Takeout or composer global require Lambo. So that's the way people want to install things. And we need to kind of be ready for this as developer of those open source tools. Now, if we actually are explicit about those dependencies of our package, you will face collisions all the time, right?

Matt Stauffer:
Yep.

Nuno Maduro:
So the very simple example would be, imagine that Lambo is using Laravel 7, but Takeout this using Laravel 8. If you try to install both tools in the same environment using composer global require that will not work because it will face a conflict. Now, Laravel Zero takes out this problem. It makes it so much easier having like a compilation step or a building step. Now this is the job of a framework. It makes things easier. So what it does is basically take the entire vendor, the entire application, and compiles everything to a single file making your CLI tool depending less or without dependencies at all.

Nuno Maduro:
I push it for that in Takeout and also in Lambo. I know that people are stuck using this feature that was developed like four years ago or five years ago. But it's the history of frameworks, right? They take time to actually start being used by the ecosystem. But yeah, this is like the most powerful feature in Laravel Zero. My favorite one is that it just compiles everything to a single file and distributes your CLI tool as a single file, which makes things so much easier.

Matt Stauffer:
I appreciate you mentioning that. Not only because it's interesting, but I think that everyone listening should know about this because it is a common issue that you as a consumer of these global packages are going to run into. So it's really helpful for you to know how to solve this problem. So even though it sounds like this might not be like a one on one thing like an early learning, it actually is very helpful. And if you're not familiar or if you don't remember from the previous episode about composer, composer global is not being installed. Those things aren't being installed in a project by project basis. Your composer global is being installed on your computer once at the root level of your user. So what that means is if I say composer global require, one of those tools, Takeout or whatever, there's a composer adjacent that is actually at the root of your user folder in your computer.

Matt Stauffer:
And now that tool, Takeout or whatever, will be available on your command line no matter what project you're in, which is very cool. But like Nuno mentioned, you can then run into these dependency issues. So if you're trying to composer global require something that hasn't been bundled like he's talking about here, and you say, "I'm trying to composer global require this thing. And all these other things I have installed are conflicting." The first thing I would do is go to your global composer folder and then just delete your composer about lock and delete your vendor folder. And if you didn't listen, go back and listen to the composer of the episode. In composer 2, that will be less of an issue, but right now that's actually a safer for those of you nerds who are saying, "Why don't you go composer global update?" It's not the same thing, unfortunately. That's the safest option.

Matt Stauffer:
So if you're on Macro Linux, I know that's going to be basically your home folder. So like tilde/dotcomposer, that's a hidden file or folder named composer. And in there you just delete composerdotlock, delete the vendor folder, and then you run composer install there. There's a very good chance that if you get the most up to date version of all your global dependencies, you're just going to be fine. It's seldom that you get the most up to date version of all your global dependencies and they still have a conflict. So just a note to everybody, it's a troubleshooting tip. If you listen to this in the future and you're on composer version two, you should be able to just say composer global update and it should be able to do that for you without you having to go delete any files.

Matt Stauffer:
Okay. I mean, you and I obviously are canoed on that all day long. Let's go back over to the more introductory stuff. So we have talked about the fact that we're talking about interacting with Laravel from the command line. We do have this idea of these third party tools, like Takeout and Lambo, and Expos and stuff like that. We're going to set those aside for now and talk about just the day to day experience of being a Laravel programmer working primarily with Artisan and Tinker. So we've talked about some common use cases for each of these. Let's talk about some common challenges or gotchas or anything like that. What do you think trips people up, or what do you think are things that are challenges or maybe things where people should use them more when it comes to Artisan and Tinker? So when you're thinking about interacting with your Laravel app from your normal web based level app from the command line, what are difficulties people have, or what are the things you wish that people knew about?

Nuno Maduro:
So, one thing I really would like people to know about is some people underestimate the power of Artisan itself. One thing I do use a lot and I think is a kind of a hot tip is that when your boss or your tech lead asks for a feature, try to develop that feature first in the command line. I do use this tip a lot. For example, the recent feature on vapor. Taylor was asking me, "Hey, Nuno. We do have this particular feature that we would like you to develop for vapor. What do you think about it?" And I said, "Oh, Taylor, I'm going to code an entire pack of that feature using the console." And this is so much nicer because you don't need to worry about front-end at all, which is probably like 50% of the time. And also it makes sure that you can actually deliver the entire feature without any problems.

Nuno Maduro:
So if in this case, vapor is using a WVS behind the scenes, so you don't really control everything. Correct? So a ton of stuff can go wrong because you don't know the entire WVS system. So it's so much nicer that you can spend like some time using the console just to develop this POC, and you develop the POC and then you deliver, and you're sure, "Okay. The feature works from A to Z and now I can already about the front end, because I know that the feature will work." I can execute that feature in the console and you see it works. And it's so much nicer, so much easier to actually create commands from Laravel. You just type PHP, main command, and you type the name, and bam, you can put the code inside of the handle method and you can develop the entire feature there and make sure it works from A to Z. And then you worry about front-end.

Nuno Maduro:
And it cuts in time. Because if you think a little bit, sometimes you spend time working on front end that then will not actually be used at the end of the feature because you didn't work yet on the backend. So yeah, I do use the command line a lot for this kind of things. I think it's kind of a heightened power behind Artisan itself.

Matt Stauffer:
Yeah. Yeah. I love that. So just for everyone to know I actually paused for a second and asked Nuno, when he said POC he meant POC proof of concept, just so you know. So he's basically saying is I want to be able to show that this thing works. I love that. We did that at Titan. I remember back when Adam worked at Titan, we had this really, really, really, really complicated thing that we were trying to build for people and we didn't know quite what the user interaction was going to look like. We imagined this idea, Adam and I did together, of sort of a black box where you put in a little bit of information and then it does all sorts of complexities and it outputs a little bit of information. We were kind of nervous that like having to build that at the same time as we built the front end interface was going to really complicate things.

Matt Stauffer:
So I said, "Adam, why don't you go build this black box and just give me an Artisan interaction with it." So we made an Artisan command that asked maybe two or three questions, that was it. And then he built this massive and complicated calculation black box based on the user specifications or the client's specifications. And then it output really simple data. And once we got that working in a way that made sense and tested it in a way that made sense against the user specifications, then he built the user interface on top of that black box.

Matt Stauffer:
So I totally loved that idea because one of the things I talk a lot of with the Titan folks is that context switching is one of the most costly things that can affect a developer. We all know this in terms of you're really deep in something, and then you have a meeting, or you got a phone call, or you get a tap on the shoulder, but there's also context switching when it comes to thinking about different aspects of your code, whether it's context switching, thinking about front-end versus backend, or the more common when we think about is coding versus being the product owner where you have to ask these questions of as a user, "What do I want?"

Matt Stauffer:
That's kind of a different way, but even just this that you're talking about, "Am I in the front end space and am I thinking about designs and I'm thinking about user flows? Or am I in the backend space and I'm thinking about how to actually implement this feature?" Being able to stay just in one of those mental spaces for the duration of your proof of concept is awesome. So I love that and I really appreciate you bringing that up.

Nuno Maduro:
I also think it gives you some sense of delivery. When you are at 50% of the project and you know that the backend works, you can work like very comfortably on the front end because you are sure the backend works. I know that here, for example, I do need to implement a loader because this feature is a little bit slow over there. It gives you like the complete knowledge about the entire feature from A to Z. I do use Artisan a lot for that. Yeah.

Matt Stauffer:
I love that. Yeah. So one of the things, like I said before, we like to talk about here is the things that kind of get people stuck and you shared some really great ideas for how to use Artisan to develop the original feature. One of the things I want to keep talking about here though, is if somebody's new to Laravel, often they're not developing their own features. They're really just using Artisan as it is. They're not creating custom Artisan commands just yet. And that's just fine, y'all, is just a start with your interactions with Artisan just being, what does it look like to interact with Artisan on a day to day? And one of the things I would just want to ask is, do you have any either things to help people avoid any problems or tips and tricks or anything like that, things you've wished everybody knew about just interacting with Artisan as it exists out of the box?

Nuno Maduro:
I think to be honest, Artisan, it's really good. Nothing else. I'm not sure if I have something about that because I don't remember to actually struggle with Artisan when I first get started. Probably like the list of commands, I wasn't sure about what I was able to do. So starting with checking the docs could be probably a good idea to make sure that you fully master the tooling before actually starting using it. But yeah, besides knowing what are all the powers of Artisan, and I'm not sure if I have struggled with something, to be honest.

Matt Stauffer:
Yeah. I love what you just said there, because I do think that there's two common things I see. And the first one is people who build tools themselves or do things by hand, not knowing it already exists in Artisan. So what you said is perfect. The number one most common problem I see people having with Artisan is not knowing something that can be done in Artisan and not using it. So I would say it's a great idea. Go just type PHP artisan in your new Laravel app, go read what they all do. And if any of them don't make sense, go read the docs for them. That's a great call.

Nuno Maduro:
I do have a tip about that though.

Matt Stauffer:
Yeah, go ahead.

Nuno Maduro:
Some people won't know, but they can actually type --help just after every command. So, for example, if you are using the php artisan migrate command, which the goal is migrate every new migration to your database, you can actually type --help just after that command to understand the possible options about the command itself.

Matt Stauffer:
Yeah. So if you don't know what something is, you can actually get the docs for it by typing php artisan view:cache --help. And you're actually going to learn all about that thing. Yeah. It's like the man page for it.

Nuno Maduro:
Correct. So every command like you will ... So first of all, the list will display you tons of commands. And for each command you can actually type --help just after it. And you will get description about the command itself, but also all the available options that you have about that command. And a good example for this will be if you type, for example, PHP Artisan queue work, which is the way of consuming queue jobs in Laravel. If you type --help, you will be able to find tons of options about this queue, like the memory usage, the delay time and all those cool things about Laravel.

Matt Stauffer:
That's awesome. I like that a lot. So I had one other tip for me, which is when you're working on most command lines, you might find yourself in a circumstance where you want to put a PHP namespace in one of your commands. So, for example, if you're making a controller, you do PHP artisan make:controller, space the name of that control controller. What if that controller is going to be in a namespace? So if you ever try php artisan make:controller API/DotController, it won't work. And that's not an artisan thing. It's actually a command line thing. If you have slashes in the command line like that, they're going to be considered an escaping slash instead of an actual character.

Matt Stauffer:
So the solution there is if you're going to be writing that slash you want to do a forward slash before it, so you would do php artisan make:controller API/\DotController. And that forward slash is basically telling your command line, hey, that backward slash is not an escaping slash it's actually a character of its own. And just remembering the direction of the slashes and stuff like that or remembering you can do that sometimes trip people up. I think the other thing you can do is just in case it in quotes. If you encase the controller name and quotes, you may not have to escape it. Is that right? Does that sound right to you?

Nuno Maduro:
I think you still have to escape it.

Matt Stauffer:
You do? Okay, cool.

Nuno Maduro:
I still struggle with that to be honest. And every time I'm using, for example, Laravel scout, I do need to insert to specify that the model, full qualify namespace. I still struggle with the fact that it asks me all the time to add a double a slash before. So it's something that I wish it could be buried in the command line, I would say.

Matt Stauffer:
Yeah. The difficulty with that unfortunately is just the fact that this is a result of ... Artisan doesn't even know that you typed in slash there in the first place. So it can't catch it for you because it's literally not seeing the slash. So if you've ever found yourself in a circumstance where you tried to make API/DotController and instead you end up with API DotController, that's what happened, is that Artisan didn't even get that slash in the first place because your command line escaped it for it. I think that's the other most common thing. I didn't prepare you for this, so feel free to just say no, but are there any other tips, tricks, challenges, or gotchas for Tinker that you want to share with people?

Nuno Maduro:
Well, there is one cool thing about Tinker, but I'm not sure if most of the people like this, one of them is that it can somehow infer the use it model for you, which is quite nice. So, for example, if you are using just the user model, you don't actually need to type app/models/user. And you can just type user, for example. And TinkerWell will kind of understand automatically that you are referring to the user class or the user model class, which is quite nice. So that is one thing I use all the time in Tinker, which is very cool.

Matt Stauffer:
I love that. That would be my first tip for sure. My second tip is sort of esoteric, but it has confused me and so many of my friends so many times that I'm going to let you all know. There is something in PHP called the destruct. I think that's what it is, the magic method. And basically, when a class gets shut down and it's no longer being used ... I hope that's the right one, destruct or destroy, something like that. We have some hooks in the framework and in some third party packages that run when a class gets destroyed. And I can't remember the specifics. I learned it all and have since forgotten. When you're doing certain things and certain packages that is most common with an exception handler, like I know that Bugsnag does this and I think others do, sometimes that method never gets called in Tinker because Tinker is not actually a full web request. It's really a request that's just sitting open as long as your Tinker session is open.

Matt Stauffer:
So, for example, if you're trying to test your Bugsnag by sending an error report in Tinker, you're not going to ever see it send. You're going to think you're crazy. No, that's not what it is. I don't think I've ever run into anything else that has that problem. But just to know that the full request of Tinker is not the same as a full request of an HTTP request because you don't get the end of that request. You don't get the end of that session in the same way in Tinker as you do in your thing. So again, 99.9% of the time that's not going to be an issue whatsoever, but if you feel like you're losing your mind and something that you're doing in Tinker is not working. First thing to check is, is that thing being triggered based on the end of the request, because it's possible the request is never ending because you're in Tinker.

Matt Stauffer:
Number one common use case for that for me and friends is Bugsnag or another exception handler does something at the end of the request, the Tinker requests never ends. It never gets sent out to your error handler.

Nuno Maduro:
Yeah, correct. So the method is indeed destruct. I do think that happens because you probably are using Tinker by creating a new variable and the variable is never unset.

Matt Stauffer:
Exactly.

Nuno Maduro:
Do you know what I mean?

Matt Stauffer:
Yeah.

Nuno Maduro:
So you do new job, for example, and you expect like the job to use destruct at the end or Bugsnag, for example, but since the variable is never unset because you are always in that session, I think it's because of that reason that the destruct never call it. But as you said, it's like 99% of even more, I think, you wouldn't find this problem in most of the use cases in Tinker.

Matt Stauffer:
Yep, absolutely agreed. Sweet. Well, let's move on to the next section. Is there anything else that you want to talk about in this topic? I know that while you were very knowledgeable about a lot of this more entry level stuff, I also know that you're super knowledgeable, especially relative to your average Laravel programmer about the standalone applications and everything. But across all three of these sections, is there anything else you would like to talk about, even if it's maybe a little bit more advanced before we kind of start wrapping up?

Nuno Maduro:
Yeah. I would like to talk about how can you make your standalone CLI applications or even the Laravel Artisan commands that you shared with the world using packages, how can you make them more successful? I never shared this with anyone actually, but I think one of the reasons why I have developed some successful packages, one of them being Collision, another one being PHP Insights, or even Pest itself is that their interaction with the console is really good. In the UI aspect of it, it's really good. Now I was saying at the beginning that, "Oh, on the console, you don't need to worry. It's just about texts and a little bit of color and you don't need to worry about it." But I also think that the difference between making a CLI good to a CLI very, very good is the interaction that people have on the output of that console.

Nuno Maduro:
The best example I have here, and probably some of our listeners are not familiar with, is PHP Insights. I don't use PHP Insights a lot right now. I was using before, but not a lot right now. But one of the reasons why PHP Insights got like 4,000 stars on GitHub and also millions of downloads, the reason is the UI of it and the console. You have literally cards on the output of the console. You have like a beautiful table percentage. It really looks like a website in the console. It is just crazy. So also Collision, Collision is the best example. Most of the people don't remember even how exceptions were before in the console, but they were truly a mess.

Nuno Maduro:
You didn't have columns. You didn't have a snippet of code, for example. It was complicated to understand what the exception is about. So Collision is like that CLI tool that makes all of that output pretty. And once it's pretty, people like it that very much. So that's the difference between making something good from something very, very good in the console. So one tip for standalone CLI tools, but also for package developers that are providing Artisan commands is make the CLI output very pretty. People like that very much.

Matt Stauffer:
I love that.

Nuno Maduro:
And even Taylor, in the Laravel new installer, he has put the dragon man. You saw it.

Matt Stauffer:
Yeah. He's really kind of put some love into it. I love that.

Nuno Maduro:
Yeah. People like it. Yeah.

Matt Stauffer:
It's interesting because there's a difference to be had on the console between appealing UIs, user experiences and user interfaces, and also quick ones. And that's something we've been struggling with on Takeout a lot is we wanted to make this thing where the user interface exposes a lot of information and makes you know all the choices and looks really nice, and also allows you to move really quickly. And the good thing is you can combine rich menu systems together with defaults I've found or command line flags that the power users can pass, but that the new users don't have to, to really make a more rich experience. Because a lot of people say when I first get started with Takeout, I want that menu experience that allows me to pick which one is available. But when it's something I'm doing frequently, I just want to be able to hit the thing once and not have to tap through enter down, down, enter down, down, down, down, down, enter, and then enter to hit the default or whatever.

Matt Stauffer:
So one of the things we were building right now, Jose is doing this, not me and there's a third party contributor who's been helping with us, is building a parameter that you can pass that says --defaults or --yes, or something. So that if you hit a command and you know that you're just going to accept the defaults of all those beautiful menus that we have, then you could just bypass all of it. So there's this weird interaction between you both want them to be really nice to work with and you also there's sometimes this expectation in the command line that I want it to be really snappy or often I want it to be able to be run with no user interaction whatsoever. So I love that idea that we both want to make it super clear to people who don't know what they want or who need information out, but also allow them a really fast path through if they know exactly what they want. And we got to consider both of those things, which is a challenge, but also an exciting opportunity.

Nuno Maduro:
Correct. That's a very good point. I also have another point. I think it's a little bit similar to the one you just mentioned is that the type of user that will interact with a console is different from a type of user that will interact with the web. So the user that works with the web, they don't care about what's happening behind the scenes. So normally, they click on the install button, for example, or the add user button and they just want that it works. People from the console, usually some people want just to have a user and don't care about what's happening behind the scenes, but some people actually want to have more a verbose option. So, for example, they expect to always type ---V and actually see the verbose output from your common interface. This is very, very common. Every time you interact with the console, people would expect that if they type -V, they expect your console output to display all actions they are doing behind the scenes. You have something like this on Takeout?

Matt Stauffer:
No, but we're working on something actually so that you'll be able to type -V and it'll tell you all the Docker commands it's running. We're also working on it with Lambo. Jon Sugar has been helping me a lot with the remade PHP version of Lambo. He has this unbelievably rich system that captures varying levels of detail about what's happening. We're just right now, piping it all through the -V, -VV, or -VVV so you can choose the verbosity. The idea is if you just run Lambo, you're just going to say, "Oh, we're creating this. We're creating that. Oh, your new Laravel app's ready to go." But if you type -V, it's going to actually show you the output of all of the commands that we're running so you can see it. So it's that same kind of idea.

Matt Stauffer:
The cool thing is there are tools available to you in the symphony command line, things to know what level of verbosity was what's requested. So I love that because I think that one of the problems we had done in Lambo was we were building our own verbosity system and we're like, "Wait a minute, there's already this concept existing -V. Why don't we just tap into that instead?"

Nuno Maduro:
Yeah. And it's very common across all the CLI tools in every language-

Matt Stauffer:
Yeah, yeah. It's not just a PHP thing.

Nuno Maduro:
... like -V people expect to command V verbose. And a very important tip for people that actually just using Artisan as normal, not package developers, not standalone CLI tools developers is that you can actually use -V on the Laravel command itself if you just are curious about what's happening. For example, imagine that you type artisan migrate and you are having an error in the console. So if you just want to see the entire output error of that error in the console, you can just type -V and you will see the entire thing. Same goes if no error occurs and you just want to see exactly what are the steps behind the scenes, you can type -V and you will see the entire.

Matt Stauffer:
Yeah. I love that. That's a great tip. So are there any other things, because we're in the middle of ... is there anything else you'd like to talk about this topic and you give some really great ideas about these tools. Are there any other things you'd like to talk about, about interacting with Laravel and the command line, about Artisan, and about Tinker, about anything else?

Nuno Maduro:
I think I'm good. Yeah.

Matt Stauffer:
Okay. So if someone were to want to learn more about any one of these three, where would you send them?

Nuno Maduro:
Well, I can tell you how I have started, but I can also tell you my experience about how they should get started. So basically I've started with Laracasts in my case. It's the second time I'm in this podcast and the second time I'm talking about Laracasts. But yeah, I started with Laracasts to understand all the power in Artisan and with Tinker as well. But to be honest, if you are not a video learner, you can just use the docks of Laravel. They are very, very good. And you will understand all the power of Artisan, all the power of Tinker, just reading the docks very slowly in the afternoon. For CLI standalone tools, I recommend you to take a look at laravelzero.com and you will find all of the documentation there to get started developing your own CLI tool.

Matt Stauffer:
Yup. All right. So the last piece of every single one is a personal fun moment. You know what I realized is that often I ask people about one of their hobbies and I'll ask them about some hobby I know for them and I realized, I don't know your hobbies. So that's actually my personal fun moment. What is your most common hobby that is not a coding.

Nuno Maduro:
Oh, that's not coding. That's an interesting question.

Matt Stauffer:
Yeah. Exactly. Yeah. What do you do when you're not at the computer? I guess it could be at the computer, so when you're not coding.

Nuno Maduro:
I think I have two main hobbies. The first one is video gaming. I play a lot.

Matt Stauffer:
I remember you told me about that last time for sure. What's your favorite game right now?

Nuno Maduro:
I've been playing a lot of Counter Strike. But I do diverse sometimes. Sometimes I'm into Counter Strike and I do play for three months, but then I change again to League of Legends. It depends what is the thing of the moment between my group of friends. So video games is one of them and also soccer or football as we call it here in Europe. So I play like two times a week. It's very cool. I love it. So those are my main hobbies.

Matt Stauffer:
Do you have a favorite team?

Nuno Maduro:
I do. It's a Portuguese one called Benfica.

Matt Stauffer:
Okay. I will not even try and say that back in because I know I'm going to ruin it.

Nuno Maduro:
Can you try?

Matt Stauffer:
Can say it again and I'll try.

Nuno Maduro:
Benfica.

Matt Stauffer:
Benfica.

Nuno Maduro:
It's nice.

Matt Stauffer:
Is it like with a B-E-S.

Nuno Maduro:
It's B-E-N-F.

Matt Stauffer:
N-F. Oh, okay.

Nuno Maduro:
Benfica.

Matt Stauffer:
Yeah, I got it. Cool. Yeah. My son, he's eight and he's really, really into soccer. We just moved to Atlanta, which has a team. So that's his team, but his godfather was like, "Yeah, but you gotta have a European team too." So I think he got him an Arsenal jersey. So I think he's an Arsenal fan just arbitrarily because of this godfather loves Arsenal. So we were going to watch some soccer, some football, this weekend just so he can actually ... because he plays it at the eight-year-old, very, very, very casual level. But he's now getting to the point where he wants to watch the games and stuff. I'm like, "Okay, cool. I don't know anything, but watch it with you."

Nuno Maduro:
So how that is working out for you because you need to play with him, right?

Matt Stauffer:
Yeah. So our yard is not big enough to really play. So what we do is we just go and we just kick the ball back and forth. And at first I would go super easy on him. Now I really don't, because he's really learning some moves. And again, I think there are eight-year-olds who are like really serious and on traveling teams. He's not at that level, but he's at the level where me as like your average kind of clumsy adult who doesn't really know how to play the game at all, he's getting to the point where in a year he'll be able to kick my butt all the time.

Matt Stauffer:
So I'm like, "Okay, this is kind of cool." So I am feeling the pressure to be good enough to play along with him. But I also want to find a good park around here that has like an actual big open space because he's now at the point where my backyard is not good enough for him. So that's kind of fun actually. I know a little bit about a bunch of sports and I'm not particularly good about any of them. So I now feel this pressure. I'm like, "I better be good enough to compete at soccer."

Nuno Maduro:
Yeah, it's a good excuse to work out.

Matt Stauffer:
Yeah, exactly. And even in our backyard, by the time we're done, we'll play 20, 30 minutes of soccer, I'm going to be drenched in sweat. So we are definitely having a good time on it. I'm having to teach myself some moves just to be able to keep up with him. Awesome. Well thank you for sharing that with us. Now that I asked you that question, it's like, I actually think that I asked you that exact same question last episode, last time I had you on last season. Just for the rest of y'all listening, if you didn't get a chance to listen, take a listen to Nuno's episode on season three. I will make sure that we link it in the show notes. This is the last thing, how can people follow you? Is there anything you want them to go check out? You've mentioned laravelzero.com. Is there anything else that you want them to check out and how can they follow you?

Nuno Maduro:
Yeah, so people can check my website to follow my work, nunomaduro.com, and they can also check out my recent opensource project, which is pestphp.com, which is an alternative to PHP unit for PHP testing. Check it out and give me feedback.

Matt Stauffer:
Awesome. You're also enunomaduro Twitter and we'll have that link.

Nuno Maduro:
Exactly. Exactly.

Matt Stauffer:
And he works for Laravel so you should see his name around. Well, Nuno, thank you. I mean, you know how much I benefited from your work because I've built several things on Laravel Zero. And for anyone who didn't hear it when he slyly snuck it in there, I'm going to say it outright. I will build these things and then he'll put it in a pull request that I'll build something on top of Laravel Zero. He's like, "You're probably going to want to do it this way." So for each of the packages I built he did that, the whole thing where he talked about where you compile the thing, he's like, "You're probably going to want to compile it." And he'll teach me exactly how to do it. So I have benefited greatly from both the ecosystems that you've created and then also your help.

Matt Stauffer:
So thank you for my take. For anybody who uses these tools that I make, Lambo and all this kind of stuff that, you've got something to thank him for. And also, now that you're working here, you've built some features in Forge that we've been waiting for ages and you and James have. So thank you for all your contributions to the community. And thank you for hanging out with me today.

Nuno Maduro:
Thank you so much for having me, man.

Matt Stauffer:
That's a pleasure. All right. See you all next time.

Creators and Guests

Matt Stauffer
Host
Matt Stauffer
CEO Tighten, where we write Laravel and more w/some of the best devs alive. "Worst twerker ever, best Dad ever" –My daughter
Tinker, Artisan, & CLI, with Nuno Maduro
Broadcast by