Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pain12's avatar

Deny table acces in database

Hey guys. I have data in my Database like time and day. Now I want to use that data to deny access to a certain table on this day and time. My idea was to create a function in js that give me the actual time and look if the slots in the database fitt. But since I am building my project in Laravel I was wondering if there is a function like that, that gives me the time and date. Also, I don't really know how I should deny a table access. Do I need to use raw sql commands or can I do that with laravel? Maybe someone has an idea or where I can look this up.

0 likes
15 replies
Snapey's avatar

You really need to work with Laravel a bit more.

You should be thinking in terms of MVC Model, View, Controller

There will no doubt be a route which shows the content of this table (represented in a Model). This route connects to your Controller.

In the controller you allow the user to get data from the table (via the model) and use a Scope to limit what data is accessible based on the current date/time.

Pain12's avatar

Thanks but I could also just do everything in SQL or not? Maybe faster

jlrdw's avatar

You could still write a query scope for that.

A scope does not have to be eloquent it can be regular SQL.

That's your choice. But a scope would be a great place to narrow something like that down.

Pain12's avatar

I scope is something like a filter right? So I could scope down all posts that don't have todays day and actual time? And then go to deny access to a table? But the questions remains. How do I get the day and time in laravel?

Cronix's avatar

How do I get the day and time in laravel?

now() will give you a carbon object with the current date/time that you can manipulate.

1 like
Cronix's avatar
Cronix
Best Answer
Level 67

It sounds like this is a good candidate for middleware: https://laravel.com/docs/6.x/routing#route-group-middleware and https://laravel.com/docs/6.x/middleware

Create a middleware that checks the date/time in the database table and reject requests that are outside of those bounds. Then place the routes that use that table within that middleware group and it's all automatic.

It's really no different than making sure a user is an admin or something, and rejecting the request if they are not, like if you are protecting an admin backend or something common.

1 like
Pain12's avatar

From what i have seen at the end of a middleware you return something right?

class RoleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, ...$roles)
    {
        if (! $request->user()->hasAnyRole($roles)) {
            abort(403);
        }

        return $next($request);
    }
}

But what if I just want to deny access to one specific table and return nothing?

Cronix's avatar

The code you are showing would do something like that. If the user doesn't have the appropriate role, laravel returns a 403 preventing access via abort(403);. You could return whatever you want there.

Then, if they DO pass the condition, it processes the next request via return $next($request). It only hits that if it passes the condition above it. Otherwise it gets rejected.

Using the example you posted, you'd just change the if() condition so it would be checking the database table, getting the date/time you were talking about and comparing it to now(). If now() is not within the time, then abort(403). If it is, then process $next($request) and allow it to continue.

1 like
jlrdw's avatar

@pain12 I would also suggest you make up a "learning" project, and just play around with various things, middleware, query scopes, some query builder, some eloquent, db facade, Auth, play around with manipulating date, practice some queries with date like getting data between two dates, things like that. That way you can test out techniques on a "practice" project first.

1 like
Pain12's avatar

The project I am working on is kind of a "practice" project. University gave us a subject and we are all on us how to work on it. The thing is before this corse in university my only programming experience was SQL and VBA(Excel) cause I study economics. So this is kind of huge for a start =D I kind of struggle with the syntax in Laravel, guess the only way is to practice.

Project wise I am 90% finished. Just that for the problem I am facing know I don't find any tutorials =DD

From the complexity, Laravel is a bit of an overkill for my project xD Everything is so professional the routing, security and so on. But its also kind of fun. So jear appreciate the help ur giving.

jlrdw's avatar

God speed in your future career.

Pain12's avatar

Ok i have something now: I created a middleware:

 public function handle($request, Closure $next)
    {
        
    
    $time = (now()->format('H:i'));
    $day = date("Y-m-d");
    $number = date('N', strtotime($day));

    if(auth()->posts()->monday <> $day){
        deny acces to table 'body';
    }
    else{
        ...
    }    
        return $next($request);
    }

And the middleware runs only if we visit the post page. What I didn't find was how to deny access to this one table called "body" from the database.

And how do I check this for every day? Do I go on with ifelse...... or is there a better method? Also, I don't know if I can access the table like that

auth()->posts()->monday

My post table looks something like this

$post->monday = $request->has('monday') ? 1 : 0;
        $post->tuesday = $request->has('tuesday') ? 2 : 0;
        $post->wednesday = $request->has('wednesday') ? 3 : 0;
        $post->thursday = $request->has('thursday') ? 4 : 0;
        $post->friday = $request->has('friday') ? 5 : 0;
        $post->saturday = $request->has('saturday') ? 6 : 0;
        $post->sunday = $request->has('sunday') ? 7 : 0;
mohito_benzerito's avatar

You should be thinking in terms of MVC Model, View, Controller

There will no doubt be a route which shows the content of this table (represented in a buyanessay). This route connects to your Controller.

In the controller you allow the user to get data from the table (via the model) and use a Scope to limit what data is accessible based on the current date/time.

amazing advice, thank you

Please or to participate in this conversation.