Repository: laravel/horizon (language: php)

Issue:
BatchesController missing check for existence of batch
### Horizon Version

5.24.2

### Laravel Version

11.2.0

### PHP Version

8.3.4

### Redis Driver

PhpRedis

### Redis Version

6.0.2

### Database Driver & Version

_No response_

### Description

If you try to access a batch that is not existent (in the database) or call any action like retry the xhr response is a 500 (Attempt to read property "failedJobIds" on null).

The batch retrieval checks for the existence before calling toBatch
```
public function find(string $batchId)
{
        $batch = $this->connection->table($this->table)
                            ->useWritePdo()
                            ->where('id', $batchId)
                            ->first();

        if ($batch) {
            return $this->toBatch($batch);
        }
}
```

but show or retry just assume the batch is existent.
```
public function show($id)
{
        $batch = $this->batches->find($id);

        $failedJobs = app(JobRepository::class)
                        ->getJobs($batch->failedJobIds);

        return [
            'batch' => $batch,
            'failedJobs' => $failedJobs,
        ];
}

public function retry($id)
{
        $batch = $this->batches->find($id);

        app(JobRepository::class)
                        ->getJobs($batch->failedJobIds)
                        ->reject(function ($job) {
                            $payload = json_decode($job->payload);

                            return isset($payload->retry_of);
                        })->each(function ($job) {
                            dispatch(new RetryFailedJob($job->id));
                        });
}
```


### Steps To Reproduce

Access any batch url via horizon that is not existent.

i.e:
/horizon/batches/9bc9d622-c2d0-45ca-aa13-4e7dfb1efed0
BatchesController missing check for existence of batch
### Horizon Version

5.24.2

### Laravel Version

11.2.0

### PHP Version

8.3.4

### Redis Driver

PhpRedis

### Redis Version

6.0.2

### Database Driver & Version

_No response_

### Description

If you try to access a batch that is not existent (in the database) or call any action like retry the xhr response is a 500 (Attempt to read property "failedJobIds" on null).

The batch retrieval checks for the existence before calling toBatch
```
public function find(string $batchId)
{
        $batch = $this->connection->table($this->table)
                            ->useWritePdo()
                            ->where('id', $batchId)
                            ->first();

        if ($batch) {
            return $this->toBatch($batch);
        }
}
```

but show or retry just assume the batch is existent.
```
public function show($id)
{
        $batch = $this->batches->find($id);

        $failedJobs = app(JobRepository::class)
                        ->getJobs($batch->failedJobIds);

        return [
            'batch' => $batch,
            'failedJobs' => $failedJobs,
        ];
}

public function retry($id)
{
        $batch = $this->batches->find($id);

        app(JobRepository::class)
                        ->getJobs($batch->failedJobIds)
                        ->reject(function ($job) {
                            $payload = json_decode($job->payload);

                            return isset($payload->retry_of);
                        })->each(function ($job) {
                            dispatch(new RetryFailedJob($job->id));
                        });
}
```


### Steps To Reproduce

Access any batch url via horizon that is not existent.

i.e:
/horizon/batches/9bc9d622-c2d0-45ca-aa13-4e7dfb1efed0


Discussion / hints:
We'd appreciate PR's for these!
Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!
We'd appreciate PR's for these!
Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

Fix patch:
diff --git a/src/Http/Controllers/BatchesController.php b/src/Http/Controllers/BatchesController.php
index 53e88ae3..3f092928 100644
--- a/src/Http/Controllers/BatchesController.php
+++ b/src/Http/Controllers/BatchesController.php
@@ -59,12 +59,14 @@ public function show($id)
     {
         $batch = $this->batches->find($id);
 
-        $failedJobs = app(JobRepository::class)
-                        ->getJobs($batch->failedJobIds);
+        if ($batch) {
+            $failedJobs = app(JobRepository::class)
+                            ->getJobs($batch->failedJobIds);
+        }
 
         return [
             'batch' => $batch,
-            'failedJobs' => $failedJobs,
+            'failedJobs' => $failedJobs ?? null,
         ];
     }
 
@@ -78,14 +80,16 @@ public function retry($id)
     {
         $batch = $this->batches->find($id);
 
-        app(JobRepository::class)
-                        ->getJobs($batch->failedJobIds)
-                        ->reject(function ($job) {
-                            $payload = json_decode($job->payload);
+        if ($batch) {
+            app(JobRepository::class)
+                            ->getJobs($batch->failedJobIds)
+                            ->reject(function ($job) {
+                                $payload = json_decode($job->payload);
 
-                            return isset($payload->retry_of);
-                        })->each(function ($job) {
-                            dispatch(new RetryFailedJob($job->id));
-                        });
+                                return isset($payload->retry_of);
+                            })->each(function ($job) {
+                                dispatch(new RetryFailedJob($job->id));
+                            });
+        }
     }
 }


Test patch: