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

CedricBongaerts's avatar

Linking task to a group

I'm making an application where users in a specific group can post tasks to that group. I've made all the relationships, ect but there still seems to be something wrong with my code when I try to link the task to the group.

Any help would be appreciated!

This is what I currently have: App\TnB.php

    public function group() 
    {
        return $this->belongsTo('App\Group');
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }

App\User.php

    public function tnbs()
    {
        return $this->hasMany('App\TnB');
    }

App\Group.php

    public function tnbs()
    {
        return $this->hasMany('App\TnB');
    }

TnB migration:

Schema::create('tnb', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('username')->unsigned();
            $table->foreign('username')->references('id')->on('users')->onCascade('update')->onDelete('cascade');
            $table->integer('group_id')->unsigned();
            $table->foreign('group_id')->references('id')->on('groups')->onCascade('update')->onDelete('cascade');
            $table->string('desc');
            $table->timestamp('date');
            $table->timestamps();
        });

Tasks create.blade.php

{!! Form::open(['route' => ['tnb.store', $group->slug]]) !!}
                    <div class="form-group form-horizontal">
                        <div class="form-group">
                                {!! Form::label('name', 'Task/Booking:', ['class' => 'col-md-4 control-label']) !!}
                            <div class="col-md-6">
                                {!! Form::Text('name', null, ['class' => 'form-control']) !!}
                            </div>  
                        </div>
                        <div class="form-group">
                                {!! Form::label('username', 'Concirning:', ['class' => 'col-md-4 control-label']) !!}
                            <div class="col-md-6">
                                {!! Form::select('username', $usernames, null, ['class' => 'form-control']) !!}
                            </div>  
                        </div>
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                {!! Form::submit('Create task or booking', ['class' =>  'btn btn-primary']) !!}
                            </div>
                        </div>
                    </div>
                {!! Form::close() !!}

Routes.php

# Group
    $router->get('groups/{slug}', ['as' => 'group.show', 'uses' => 'GroupController@show']);
# TnB
    $router->get('groups/{slug}/tasksandbookings/create', ['as' => 'tnb.create', 'uses' => 'TnBController@create']);
    $router->post('tasksandbookings', ['as' => 'tnb.store', 'uses' => 'TnBController@store']);

TnBController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;
use App\Group;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class TnBController extends Controller
{
    public function create(Group $group)
    {
        $usernames = User::lists('username');
        return view('tnb.create', compact('usernames', 'group'));
    }

    public function store(Request $request, $slug)
    {
        $group = Group::whereSlug($slug)->first();
        $tnb = new TnB(
                        array(
                        'name'  => $request->get('name'),
                        'username' => $request->get('username')
                        ));

        $group->tnb()->save($tnb);

        return 'Succes';
    }
}

First Error I don't think that my $group->slug is connecting to the url (the form is not sending it the the right post location:

This is my view-source:

div class="container col-md-8 col-md-offset-2">
            <div class="panel panel-default">
        <div class="panel-heading">Create task or booking:</div>
            <div class="panel-body">
                
                <form method="POST" action="http://app.dev:8000/tasksandbookings?" accept-charset="UTF-8"><input name="_token" type="hidden" value="c3OFem5yIxQZyk8tnhHEMiAdkg8C2EIT2CLEshpX">
                    <div class="form-group form-horizontal">
                        <div class="form-group">
                                <label for="name" class="col-md-4 control-label">Task/Booking:</label>
                            <div class="col-md-6">
                                <input class="form-control" name="name" type="text" id="name">
                            </div>  
                        </div>
                        <div class="form-group">
                                <label for="username" class="col-md-4 control-label">Concirning:</label>
                            <div class="col-md-6">
                                <select class="form-control" id="username" name="username"><option value="0">Ced</option><option value="1">Test1</option></select>
                            </div>  
                        </div>
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <input class="btn btn-primary" type="submit" value="Create task or booking">
                            </div>
                        </div>
                    </div>
                </form>
        </div>
    </div>

I have no idea why it isn't sending it to the slug of the group.

Secondly it gives me the error of missing the two arguements.

Anyone have an idea what I'm doing wrong.

Thanks

0 likes
15 replies
willvincent's avatar
Level 54

Shouldn't the route for posting a task be:

$router->post('groups/{slug}/tasksandbookings', ['as' => 'tnb.store', 'uses' => 'TnBController@store']);

And likewise, the form action should post there instead of just to /tasksandbookings

1 like
CedricBongaerts's avatar

@willvincent that ofcourse makes more sence, but I'm still getting this error:

NotFoundHttpException in RouteCollection.php line 143:

Also when looking at the source I see that it doesn't retrieve the slug:

<div class="container col-md-8 col-md-offset-2">
            <div class="panel panel-default">
        <div class="panel-heading">Create task or booking:</div>
            <div class="panel-body">
                
                <form method="POST" action="http://app.dev:8000/groups//tasksandbookings" accept-charset="UTF-8"><input name="_token" type="hidden" value="c3OFem5yIxQZyk8tnhHEMiAdkg8C2EIT2CLEshpX">
                    <div class="form-group form-horizontal">
                        <div class="form-group">
                                <label for="name" class="col-md-4 control-label">Task/Booking:</label>
                            <div class="col-md-6">
                                <input class="form-control" name="name" type="text" id="name">
                            </div>  
                        </div>
                        <div class="form-group">
                                <label for="username" class="col-md-4 control-label">Concirning:</label>
                            <div class="col-md-6">
                                <select class="form-control" id="username" name="username"><option value="0">Ced</option><option value="1">Test1</option></select>
                            </div>  
                        </div>
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <input class="btn btn-primary" type="submit" value="Create task or booking">
                            </div>
                        </div>
                    </div>
                </form>
        </div>
    </div>

willvincent's avatar

What do you get if you put a dd($group); before the return in your create method?

1 like
CedricBongaerts's avatar

@willvincent Yea, it seems like it can't find the group..

Group {#181 ▼
  #table: "groups"
  #fillable: array:7 [▼
    0 => "name"
    1 => "owner"
    2 => "shortdesc"
    3 => "longdesc"
    4 => "groupimage"
    5 => "invitecode"
    6 => "slug"
  ]
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:10 [▶]
  #original: array:10 [▶]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}
CedricBongaerts's avatar

@willvincent ok found the error by doing this:

public function create($slug)
    {
        $group = Group::whereSlug($slug)->first();
        $usernames = User::lists('username');
        return view('tnb.create', compact('usernames', 'group'));
    }

Thanks for all your help

1 like
CedricBongaerts's avatar

@willvincent I just noticed something. When doing:

$usernames = User::lists('username');

I noticed that I ofcourse get all the users on the users table. Is there an easy way to simply get all the users in that specific group?

willvincent's avatar

You have a relationship between users & tasks, and groups and tasks, but are users related to groups directly?

Can you share code for your user and group models?

CedricBongaerts's avatar

@willvincent Yes ofcourse:

Group

    /**
     * Database relationships
     */

    public function owner()
    {
        return $this->belongsTo('App\User');
    }

    public function users()
    {
        return $this->belongsToMany('App\User');
    }

    public function tnbs()
    {
        return $this->hasMany('App\TnB');
    }

User

    /**
     * Database relationships
     */
    public function groups()
    {
        return $this->belongsToMany('App\Group');
    }

    public function ownedGroups() 
    {
        return $this->hasMany('App\Group', 'owner');
    }

    public function tnbs()
    {
        return $this->hasMany('App\TnB');
    }

TnB

    /**
     * Database relationships
     */
    public function group() 
    {
        return $this->belongsTo('App\Group');
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}
willvincent's avatar

I think this might work...

public function create($slug) {
  $group = Group::whereSlug($slug)->first();
  $usernames = Group::find($group->id)->with('user')->lists('username');
  return view('tnb.create', compact('usernames', 'group'));
}
willvincent's avatar

Not sure if that syntax is quite right, but that should be heading in the right direction

CedricBongaerts's avatar

@willvincent Yea it's close I think, but with that code, it's trying to find usernames in the groups table:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'field list' (SQL: select username from groups)

CedricBongaerts's avatar

@willvincent seems to be giving the same error. I also tried:

$usernames = User::find($group->id)->with('user')->lists('username');

But this also just gives a list of all the users.

willvincent's avatar

How about something like this?

public function create($slug) {
  $usernames = array();
  $group = Group::whereSlug($slug)->with('user')->first();
  foreach ($group->users as $user) {
    $usernames[$user->id] = $user->username;
  }
  return view('tnb.create', compact('usernames', 'group'));
}
1 like
CedricBongaerts's avatar

@willvincent The code was almost correct, I only had to remove the ->with('user') like this:

public function create($slug)
    {
        $usernames = array();
        $group = Group::whereSlug($slug)->first();
        foreach ($group->users as $user) {
            $usernames[$user->id] = $user->username;
        }
        return view('tnb.create', compact('usernames', 'group'));
    }

Thanks a whole lot!

Please or to participate in this conversation.