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
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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
Please or to participate in this conversation.