---
# System prepended metadata

title: Laravel Create Job

---

# Laravel Create Job

要在Laravel應用啟動時，就執行Schedule
需要先註冊他
```
    'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\StartupServiceProvider::class,// add line 
    ])->toArray(),
```
![](https://hackmd.io/_uploads/rkx9gd4Cn.png)

![](https://hackmd.io/_uploads/r1tdx_4An.png)

請建立一個class 
```
php artisan make:job MyApiCallJob 
```


StartupServiceProvider.php
```
<?php

namespace App\Providers;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Artisan;
use App\Jobs\MyApiCallJob;
class StartupServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
         
 
        Queue::push(new MyApiCallJob()); 
    }
}

```

```
class MyApiCallJob implements ShouldQueue
{

 public function __construct()
    {
        //
    }


  /**
     * Execute the job.
     */
    public function handle(): void
    {
    
    //裡面放置要執行邏輯
```