Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hidayat3676's avatar

how to make custom command in laravel

i want to create a command for a controller function such that when user type that command the controller function is called. could we do that if we can then how.

0 likes
29 replies
jlrdw's avatar

By watching some of Jeffries free introductory videos.

1 like
Cronix's avatar

when user type that command

Where would the "user" be "typing" this command? The commandline? A text field in a form? You'll have to explain better what you mean.

hidayat3676's avatar

exactly i need to call a controller function with it,is it possible?

Cronix's avatar

You don't want to do that. If you have functionality that is common to multiple places, I'd use a trait. Then the controller would use the trait, as well as the artisan command.

Cronix's avatar

"crown"? I have no idea what you mean.

hidayat3676's avatar

for the project i need this command. i hear we can make crown or command to work the way i want but i don't know it would be possible in laravel or not

Cronix's avatar

You can do what you want with commands. It's just php. You just really don't want to be calling a controller from a command. It's highly improper. Controllers are meant to respond to web requests, not the cli.

hidayat3676's avatar

@Cronix i have a function in the controller in which i'm getting data from api and storing it in the db now i don't want to have an interface for that so that every one can't access that route, what i want is to have a command through which that function can be call.

Cronix's avatar

Then just remove it from the controller and put it in the command so it can only be accessed via the command?

Cronix's avatar

Well, very basically...

  1. remove the code from the controller method
  2. put it in the handle() method of the command
  3. use php artisan yourCommandName

I don't really know what you're not understanding?

hidayat3676's avatar

i can fetch data and store also into db through a route call to controller, but i just want it through a command now

hidayat3676's avatar

@Cronix if i remove code from controller then how can i will pass that data to db from command.

Cronix's avatar

How can I know what you're doing without you posting the controller method in question?

If you're worried about other users hitting your controller, it sounds like you really should be looking at authorization and only allow certain users to be able to access that route. Like only allow "admin" users to access it. You're only giving very basic info on what you're doing and not explaining it thoroughly.

https://laravel.com/docs/5.6/authorization

jlrdw's avatar

Be better off taking all of Jeffrey's php and laravel intro videos before fumbling around with this type of thing. And actually working each and every exercise and tutorial.

Were referred to a doc reference on commands, did you even look at that.

hidayat3676's avatar

my controller code where i'm getting data from api

 public function store()
    {

          $data = fetchFromApi('https://www.thesportsdb.com/api/v1/json/1/eventsseason.php?id=4328');
          $match = new Match;
          $match->store($data);
}


i'm storing data through model into db so my  model code is below


public function store($param){

    foreach ($param['events'] as $event){
    $match = new Match;
    $match->id           = $event['idEvent'];
    $match->first_team   = $event['strHomeTeam'];
    $match->second_team  = $event['strAwayTeam'];
    // $match->tournament_id  = $event['idLeague'];
    $match->date         = $event['dateEvent'];
    $match->start_date   = $event['strDate'];
    $match->spectators   = $event['intSpectators'];
    $match->season       = $event['strSeason'];
    $match->round        = $event['intRound'];
    $match->start_time   =    date('h:i:s', strtotime($event['strTime']));
    $match->saveOrFail();

    }

}
Cronix's avatar

Then it looks like all you'd need to do is put

$data = fetchFromApi('https://www.thesportsdb.com/api/v1/json/1/eventsseason.php?id=4328');
$match = new Match;
$match->store($data);

in the command and namespace everything properly.

hidayat3676's avatar

by namespace you mean i should use my model in the command file

Cronix's avatar

You're already are using the model.

$match = new Match;

You'd just need to import the namespace of where your Match model is, just like you are doing at the top of your controller. use App\Match; or whatever.

hidayat3676's avatar

okay strongly appreciating your replies i will do that's way.

hidayat3676's avatar

but what if i put in my command code of calling this controller function

Cronix's avatar

Do it however you'd like. I'm trying to tell you how to do it properly, and calling a controller in the same codebase from a command is not proper or efficient and will use a lot more server resources since it has to bootstrap and load the entire laravel framework to respond to the controller request, whereas a command doesn't.

I don't understand your resistance to that and keep wanting to "call the controller"

Besides, if you leave it in the controller, other "users" can still hit the url unless you protect it as I mentioned before. So, what's the point?

Please or to participate in this conversation.