每个任务只允许一个反馈[Laravel 5]

时间:2015-09-01 09:22:40

标签: php validation laravel-5.1

我正在尝试建立一个反馈系统,用户每个'任务'项只能提供一个反馈。 目前,一个用户可以提供他/她想要的尽可能多的反馈,但当然我想限制这一点,看到它背后有评级,等等。

非常感谢任何帮助!

由于

这些是我的模特:

//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');
    }

我的数据库标签:

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();
        });

我一直在我的控制器中尝试一些东西,但直到现在我还没有到达任何地方:

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();
}

1 个答案:

答案 0 :(得分:0)

找到了解决方案!

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);

        foreach($user->feedbacks as $feedback)
        {
            if ($tnb->user_id == $user->id)
            {
                \Flash::error('You may not give a feedback on your own tasks and bookings!');
                return redirect()->back();

                if($feedback->user_id == $user->id)
                {
                    \Flash::error('You have already posted a feedback!');
                    return redirect()->back();
                }
            }
        }


        $tnb->feedbacks()->save($feedback);

        \Flash::success('Your feedback has succesfully been sumbitted!');
        return redirect()->back();
    }