I'm trying to make a feedback system where a user can only give one feedback per 'task'-item. Currently one user can give as many feedbacks as he/she wants but ofcourse I want to limit this, seeing there is a rating behind it, ect..
Any help would be much appreciated!
Thanks
These are my models:
//Feedback
public function user()
{
return $this->belongsTo('App\User');
}
public function tnb()
{
return $this->belongsTo('App\Tnb');
}
//Tnb
public function user()
{
return $this->belongsTo('App\User');
}
public function feedbacks()
{
return $this->hasMany('App\Feedback');
}
// User
public function tnbs()
{
return $this->hasMany('App\Tnb');
}
public function feedbacks()
{
return $this->hasMany('App\Feedback');
}
My database tabels:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
Schema::create('tnb', function (Blueprint $table) {
$table->increments('id');
$table->string('type');
$table->string('name');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->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->string('slug');
$table->date('startdate');
$table->time('starttime');
$table->date('enddate');
$table->time('endtime');
$table->timestamps();
});
Schema::create('feedbacks', function (Blueprint $table) {
$table->increments('id');
$table->string('score');
$table->string('feedback');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onCascade('update')->onDelete('cascade');
$table->integer('tnb_id')->unsigned();
$table->foreign('tnb_id')->references('id')->on('tnb')->onCascade('update')->onDelete('cascade');
$table->timestamps();
});
I've been trying a couple things in my controller but until now I'm not getting anywhere:
public function store(Request $request, Group $group, Tnb $tnb)
{
$user = auth()->user();
$feedback = new Feedback(
array(
'score' => $request->get('score'),
'feedback' => $request->get('feedback')
));
$feedback->user()->associate($user);
//dd($feedback->user()->whereId(Auth::user()->id)->count());
$tnb->feedbacks()->save($feedback);
\Flash::success('Your feedback has succesfully been sumbitted!');
return redirect()->back();
}