Hey,
I'm building a turbo charged Chirper version as seen on bootcamp.laravel.com with some additional features, as for now, i've added the ability to add files to any chirp and share it (from local dir into public dir) and set the view counts for the share (random tokenized str for url of the share with external blade).
now i want to add another input for each Chirp (model) so the end user can set deletion time for the chirp (by minutes) and i can't seem to be able to make it happen since GPT isn't updated about laravel 11 and keeps directing my to the Kernel.php file that does not exist as you probably know.
For now all i did is adding this method to the ChirpController:
public function shareFile(Request $request, $id)
{
$chirp = Chirp::findOrFail($id);
$chirp->view_count = 0;
// Validate the request for a view limit and time limit
$request->validate([
'view_limit' => 'nullable|integer|min:1',
'time_limit' => 'nullable|integer|min:0', // New validation for time limit
]);
// Set the view limit and time limit
$chirp->view_limit = $request->input('view_limit', 1); // Default to 1 if not provided
$chirp->time_limit = $request->input('time_limit', 0); // Default to 0 (unlimited)
// Decode the file paths from JSON
$filePaths = json_decode($chirp->files, true);
if (empty($filePaths)) {
return response()->json(['message' => 'No files associated with this chirp.'], 404);
}
// Copy each file to the public directory
foreach ($filePaths as $filePath) {
$source = 'uploads/chirps/' . basename($filePath);
$dest = 'public/' . basename($filePath);
if (Storage::disk('local')->exists($source)) {
$fileContents = Storage::disk('local')->get($source);
Storage::disk('public')->put(basename($filePath), $fileContents);
} else {
return response()->json(['message' => 'File not found: ' . $filePath], 404);
}
}
// Generate a unique share token
$shareToken = Str::random(40); // Generates a random string
$chirp->share_token = $shareToken;
$chirp->save();
return response()->json([
'message' => 'Files shared successfully.',
'share_link' => route('chirps.shared', ['token' => $shareToken]),
], 200);
}
As you see i have the deletiong time taken in count there, then, i added a new command with php artisan make:command DeleteExpiredChirps that contains this code
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Chirp;
use Illuminate\Support\Facades\Storage;
class DeleteExpiredChirps extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:delete-expired-chirps';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/public function handle()
{
// Current time
$now = now();
// Find chirps where the time limit has been exceeded
$chirps = Chirp::whereNotNull('time_limit')
->where('updated_at', '<', $now->subMinutes(function ($chirp) {
return $chirp->time_limit;
}))
->get();
foreach ($chirps as $chirp) {
// Delete files from public storage
$filePaths = json_decode($chirp->files, true);
foreach ($filePaths as $filePath) {
Storage::disk('public')->delete(basename($filePath));
}
// Delete the chirp itself
$chirp->delete();
}
}
}
After that, i tried to sign it into the "new" kernel file (routes\console.php - for my understanding 🤔) without any success.
after that, i've added the input into the frontend (blade template).
Bottom line - how do I add that deletion command to my laravel app based on the input in the method + where to sign the command?
Thanks in advance..! :)