我试图将语言环境数据传递给雄辩的查询,但它无法得到它.$locale
值来自URI.
class MyController extends Controller { public function index($locale = 'en') { $news = News::join('categories', 'categories.id', '=', 'news.catid') ->select('news.*', 'categories.category') ->where([['news.published', '1'], ['news.deleted', '0']]) ->where(function ($query) { $query->where('news.language', $locale) //$locale is not recognized naturally ->orWhere('news.language', 'all'); }) ->orderBy('news.published_at', 'desc') ->take(4) ->get(); . . . } }
如何将$locale
值传递给子查询?
还有就是use
,你可以用它来创建一个结构封闭周围的变量:
->where(function ($query) use($locale) { $query->where('news.language', $locale) ->orWhere('news.language', 'all'); })
请参见手册 "从父作用域继承变量" 中的示例#3 .