By watching some of Jeffries free introductory videos.
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.
can you provide me with some basic please
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.
@Cronix in the command line like where we type php artisan etc
https://laravel.com/docs/5.6/artisan#writing-commands
You wouldn't be calling "a controller function" though. What are you trying to do with this "command" exactly?
exactly i need to call a controller function with it,is it possible?
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.
what is mean by trait?
http://php.net/manual/en/language.oop5.traits.php
Laravel uses traits all over the place.
could you tell me what is crown in laravel?
"crown"? I have no idea what you mean.
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
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.
@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.
Then just remove it from the controller and put it in the command so it can only be accessed via the command?
how?
Well, very basically...
- remove the code from the controller method
- put it in the handle() method of the command
- use
php artisan yourCommandName
I don't really know what you're not understanding?
WOW
Original question you don't even mention api??????????????????
Just use curl, here is a SO article: https://stackoverflow.com/questions/33302442/get-info-from-external-api-url-using-php?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
i can fetch data and store also into db through a route call to controller, but i just want it through a command now
@Cronix if i remove code from controller then how can i will pass that data to db from command.
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.
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.
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();
}
}
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.
by namespace you mean i should use my model in the command file
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.
okay strongly appreciating your replies i will do that's way.
but what if i put in my command code of calling this controller function
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.