创建自定义值 在 Laravel 的帮助程序方法中选择“查询”


Create a custom value Select Query in helper method in Laravel

我正在尝试在助手.php中的Laravel 5.1项目中创建如下所示的东西。

$result = App'Pages::where('id', $id)->where('active', 1)->first();

以下是我的助手.php的样子:

<?php
namespace App'Helpers;
use App;
class Helper
{
        public static function checkSlug($subject, $id, $inputSlug = null){
            $result = App'$subject::where('id', $id)->where('active', 1)->first();
            return $result;
        }
}
?>

我的助手类似乎工作正常。但我被困在这一行App'$subject::where('id', $id)->where('active', 1)->first();.当我传递变量$subject时,出现以下错误:

parse error, expecting `"identifier (T_STRING)"'

这是我如何在我的视图中使用我的帮助程序方法

{{Helper::checkSlug('Pages', $page->id)}}

现在,当我尝试使用App'$subject访问我的模型时,它不允许。我想。

任何建议都会有所帮助。谢谢!

我的意思是你不能将变量类与静态方法语法一起使用。但您可以实例化您的模型:

public static function checkSlug($subject, $id, $inputSlug = null)
{
    $clsname = 'App''' . $subject;
    $cls = new $clsname();
    $result = $cls->where('id', $id)->where('active', 1)->first();
    return $result;
}

并使用对象构建您的查询并获取您的行。