-3

I have this problem. When I create new Middleware and write this code

This is an error text syntax error, unexpected 'printf' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)

namespace App\Http\Middleware;
use Carbon\Carbon;
use Closure;
use Illuminate\Http\Request;

class CheckTime
{
    printf("Right now is %s", Carbon::now()->toDateTimeString());

    // $now = Carbon::now();
    // $start = Carbon::createFromTimeString('00:00');
    // $end = Carbon::createFromTimeString('06:00');


    if ($now->between($start, $end)) {
        return redirect('/')->with('limitTime, You cant write post at this time');
    }

    return $next($request);
    public function handle(Request $request, Closure $next)
}
  • You can't output anything or perform operations outside of a method (a function) inside a class.. Put all your code inside the `handle()` method. – Qirel Dec 10 '20 at 06:31

1 Answers1

2

You cannot have such statements outside a function in a class

namespace App\Http\Middleware;
use Carbon\Carbon;
use Closure;
use Illuminate\Http\Request;

class CheckTime
{
    
    public function handle(Request $request, Closure $next)
    {
        printf("Right now is %s", Carbon::now()->toDateTimeString());

        $now = Carbon::now();
        $start = Carbon::createFromTimeString('00:00');
        $end = Carbon::createFromTimeString('06:00');


        if ($now->between($start, $end)) {
            return redirect('/')->with('limitTime, You cant write post at this time');
        }

        return $next($request);
    }
}
Donkarnash
  • 12,433
  • 5
  • 26
  • 37