dongxi0523 2015-09-01 09:22
浏览 59
已采纳

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

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();
}
  • 写回答

1条回答 默认 最新

  • dongzhang4301 2015-09-01 12:05
    关注

    Found the solution!

    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();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么