【问题标题】:Laravel 5.2 passing variable to query builderLaravel 5.2 将变量传递给查询生成器
【发布时间】:2017-01-30 04:24:31
【问题描述】:

我会喜欢使用查询生成器从数据库中获取用户的电子邮件地址并向他们发送电子邮件,但到目前为止,我已经坚持了好几天了。

公共函数 postSearchAction(Request $request){

      $data = array(
            'service' =>  $request->get('service'),
            'location' => $request->get('location'),
            'allProviders' => $request->get('allProviders'),
            'date' => $request->get('date'),
            'optional_skills' => $request->get('optional_skills'),
            'landmark_homepage' => $request->get('landmark_homepage'),
            'name' => Auth::user()->name,
            'userEmail' => Auth::user()->email,
            'requesterMobile' => Auth::user()->phone_number
        );




         $email = DB::table('users')->where('main_service', 'LIKE', '%'.$data['location'].'%')
            ->where('city_service', 'LIKE', '%'.$data['location'].'%')
            ->pluck('email');




      To send emails

        \Mail::send('auth.emails.serviceProviders', $data, function($message) use ($data)
        {
                $message->to($email, 'oluyemi')->subject('Test');
        });


    }

现在我尝试使用 dd($mail) 但该方法返回一个空数组。 我认为值得一提的是,一旦我将查询构建器中的变量更改为预期值,一切正常。

有人应该请帮助,不知道做错了什么。我真的需要使用变量。

感谢您的帮助。

【问题讨论】:

  • 您确定$data['location'] 有值吗?有没有错误?
  • @timenomad 感谢您的回复.....是的,如果我这样做 dd($data['location']) 它会返回值。 dd($email) 返回 []
  • $email = DB::table('users')->where('main_service', 'LIKE', '%'.$data['service'].'%') -> where('city_service', 'LIKE', '%'.$data['location'].'%') ->pluck('email');
  • 查看此链接以获取答案stackoverflow.com/questions/29548073/…

标签: php eloquent laravel-5.2 laravel-query-builder


【解决方案1】:

请向我们展示原始 SQL 查询,以便我们查看问题所在。
在 $email 查询之后,输入:

DB::enableQueryLog();  
dd(DB::getQueryLog());

这将是最后一项。

添加 ->toSql() 而不是 ->pluck() 和 dd() 它。

【讨论】:

  • 我刚试过这个 $email = DB::table('users')->where('main_service', 'LIKE', '%'.$data['service'].'% ') ->where('city_service', 'LIKE', '%'.$data['location'].'%') ->tosql(); dd($电子邮件);。并得到“从users中选择*main_service LIKE?和city_service LIKE?”
  • 注意是toSql(大写的S)。你能发布输出吗?
猜你喜欢
  • 2016-12-19
  • 2017-01-08
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多