doulu1914 2018-10-04 10:09
浏览 70
已采纳

将变量传递给函数

I want to pass a variable from a function passes to function message. I create $groupname in the else sector and try to use it in the message function query. What is in my case the best way to do this?

class ExistingGroupMembers implements Rule
{

    public function __construct()
    {
        //
    }

    public function passes($attribute, $value)
    {
        $existingMembers = Member::where([['idgroup', $value],['member', '<>', Auth::id()]])->select('id')->first('id');
        if(!empty($existingMembers)){
            return true;
        }else{
            $groupname = Group::where('id', $value)->select('name')->first();
            return false;
        }
    }

    public function message()
    {
        return 'In der Gruppe ' .$groupname. ' existieren keine User!';
    }
}
  • 写回答

2条回答 默认 最新

  • doudun1934 2018-10-04 10:13
    关注

    add your variables to $this scope. then you can access it.

    example code

    public function passes()
    {
        $this->group_name = Group::where('id', $value)->select('name')->first();
    }
    
    public function message()
    {
        return 'In der Gruppe ' . $this->group_name . ' existieren keine User!';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改