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

MarlonV's avatar

check-in / check-out system

Hello Guys,

I need some advice. I have a table with children. This contains a column with barcode. Think of it as an check-in checkout system, where only the barcode user is dependent. Now I want to submit this barcode on the checkin table. (children hasMany) Does a barcode first have to be searched for right child, so that the child_id can be posted in the checkin table?

children also have a manytomany relationship with events. (child_event pivot). It must be checked whether the current date matches the created_at in the pivot table. I also want to attach a value to this so that I know whether there has already been checked in earlier that day.

Would anyone want to help me with this, because I really have no idea where to start. I am new to this, and I still have a lot to learn.

thank you in advance!

0 likes
17 replies
bobbybouwmann's avatar

What have you tried so far?

From your story I think you understand how all the relationships should work. You only have to put them together ;)

We can give you feedback on the structure you have thought out if you wish.

Snapey's avatar

Check in process;

  • Get barcode from form
  • Get event from URL and load event or use Route Model Binding
  • find child by barcode value
  • Check event attendances for today, if child already attending, show error
  • add attendance record for child at event

Checkout is similar, but you need to ask the person capturing the data if it is a check-in or out.

MarlonV's avatar

Still struggling with it. Who can help me further?

What i got so far: (and that is not much)

    public function checkin(Request $request)
    {
          $validatedData = $request->validate([
            'barcode' => 'required|max:255',
         ]);

         $barcode = $request->get('barcode');
        
         $event = Event::with('children'->whereYear('start_date', '=', date('Y'));

         $result = Child::where('barcode', 'like', '%'.$barcode.'%');
        if (condition) {
    }

        $checkin = checkin()->create();

    
        return redirect()->route('admin.children.index')->with('success', 'success.');
                
        }

Model

    public function children()
    {
        return $this->belongsTo(Child::class, 'user_id');
    }

    public function events()
    {
        return $this->hasMany(Event::class);
    }

Thanks in advance

Snapey's avatar

Does the person doing the checking in not know what event it is?

What if the child is not on the list for the event?

How do you intend to store the checkin/checkouts How can a single checkin contain many events?

MarlonV's avatar

Hey Snapey,

okay, let's do this step by step. Perhaps I am not very clear, English is not my birth language. I am building an app for a local non-profit organization. It is a children's camp where parents register their child (ren) for an event. This is once a year for 4 days. Children can apply per day. Instead of linking a barcode to an event, I linked the barcode to the child. I want to checkin this child so that I get a kind of attendance list.

The 1st step: Checkin the child in using the barcode.

The 2nd step: Check whether the child has already been checkedin earlier that day.

The 3rd step: is to see if the child is registered for the event. ( event.date = current.date)

The 4th step is: Has it been paid?   Perhaps my whole approach is wrong here. I do not know.

But step 1. I have to start looking for a barcode from a relation, if it is found he can save the child_id. Otherwise not found.

Snapey's avatar
Snapey
Best Answer
Level 122

OK, step 1.

Locate the child

Not

$result = Child::where('barcode', 'like', '%'.$barcode.'%');

but

$child = Child::where('barcode', $request->barcode)->firstOrFail();
MarlonV's avatar

Okey, got it, I had some problems getting the right information out of the DB, but thats's fixed now. If not found you will be redirected tot a 404 page. (firstOrfail). I also understand why '%'.$barcode.'%' is not needed, because you already know what the exact value must be.

For step 2 we need i child to be saved in the database before we can check i guess, so i think it's the last step.

For step 3 we need to know if the child is listed for the event:

$events = Event::with('children')->get();

What i have got so far:

    public function checkin(Request $request)
    {
          $validatedData = $request->validate([
            'barcode' => 'required|max:255',
         ]);
      
         $child = Child::where('barcode', $request->barcode)->firstOrFail();
         $event = Event::whereDate('start_date', '=', '2019-08-19')->with('children', $child)->get();

         dd($event);

Thanks in advance

Snapey's avatar

you should be able to simply get all events for child and see if results contains the current event id.

MarlonV's avatar

Okay, approach the event from this perspective. This is much better indeed, now I have the child with the events.

But how do I check if event->start_date is the same as current date? the error i get "Undefined variable: event"

         $child = Child::where('barcode', $request->barcode)->with('events')->whereDate($event->start_date, '=', '2019-08-19')->firstOrFail();

Snapey's avatar

You could create a new relationship on the Child model

public function todaysEvents()
{
    return $this->hasMany(Event::class)->whereDate('start_date', today());

}

so, same as events() but with the where condition added

use it like;

$child = Child::where('barcode', $request->barcode)->with('todaysEvents')->firstOrFail();

Then you can check $child->events->count

Puzzled by your query though. Is there a range of dates (start_date to end_date) ? or is every event a single day?

MarlonV's avatar

every event is a single day. separated by time.

we have a disco again in the evening that week. so I have a start_date of 12.04.19 10:00 and an end_date of 12.04.19 16:00 and that same day a other event from 20:00 till 22:00 but validation per day is sufficient. we do not check this, but we would like to know how many children we can expect.

thank you very much for your support so far. I'm going to play with it.

MarlonV's avatar
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'events.child_id' in 'where clause' (SQL: select * from `events` where date(`start_date`) = 2019-05-25 and `events`.`child_id` in (1))

Got an error, so if i understand correctly; Events doesn't contain child_id. Witch it's true because it is a many to many relation. Children and events with a child_event pivot table.

Snapey's avatar

Sorry, I did not know the relationship. I did say it should be same as the current Child events() method.

So I assume;

public function todaysEvents()
{
    return $this->belongsToMany(Event::class)->whereDate('start_date', today());

}
MarlonV's avatar

Haha, no problem. I'm the one who's not paying attention. I thought once a relationship was established, you didn't have to do this again. In this case you have installed a whole new one with certain conditions. Learning big time here.

Okey, so now i'am getting a result. I will try to figure out how exactly I should perform that check.

MarlonV's avatar

Okey, i'am stuck again.

Problem 1 : if $child fails, $checkin also fails. Problem 2: IS their need to store the name? is there a way to get the name fro the id?

What i have got so far:

public function checkin(Request $request)
    {
          $validatedData = $request->validate([
            'barcode' => 'required|max:10',
         ]);
      
         $child = Child::where('barcode', $request->barcode)->with('todaysEvents')->first();
         $checkin = Checkin::whereDate('created_at', '=', today())->where('child_id', $child->id)->first();

         
         if (empty($child)) {
            return redirect()->route('admin.checkins.index')->with('warning', 'No events');     
        }else{
            
            $checkin = new Checkin();
            $checkin->child_id = $child->id;
            $checkin->name = $child->first_name;
            $checkin->status = 'checked in';
            $checkin->save();
   
            return redirect()->route('admin.checkins.index')->with('success', 'Checked-in');
       }
Snapey's avatar

So there are multiple paths?

         $child = Child::where('barcode', $request->barcode)->with('todaysEvents')->first();

If $child is null then might as well return an error because there is no child with that barcode.

If $child is set, but $child->todaysEvents is empty then the child is recognised but they don't have a registration for an event today.

Only if these two pass do you want to see if the child has already checked in for today?

I think a bit of a flowchart as to how you want the logic to work would help.

problem 2

Why would you need to save the name?

Checkin belongs to child. Given a Checkin object you can always load Child model and get $checkin->child->first_name

MarlonV's avatar

Hey Snapey,

The flowchart: before the kid checked-in check if listed for event. if not notify check if already is checked in. if yes, give status checkout and notify with warning check if payment balance is 0, if not notify. ( user hasmany payments)

A total payment against the amount payd i guess. If they add a child after a payment is done there is a negative balance. i'll reset the DB every year.

Problem 2, tried that but din'dt work. Found out i have the relation wrong. Will correct that.

Please or to participate in this conversation.