在Laravel中从控制器传递到视图时数据丢失
在我的一个控制器中,我将从有说服力的查询中获得的结果传递给视图。但无论我如何将结果传递给视图,我都不会在视图中得到结果 控制员:在Laravel中从控制器传递到视图时数据丢失,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,在我的一个控制器中,我将从有说服力的查询中获得的结果传递给视图。但无论我如何将结果传递给视图,我都不会在视图中得到结果 控制员: class ProductCategoriesController extends BaseController { public function __construct() { parent::__construct(); // Add location hinting for views Vie
class ProductCategoriesController extends BaseController
{
public function __construct()
{
parent::__construct();
// Add location hinting for views
View::addNamespace('product-categories', app_path() . "/MyVendor/ProductsManager/Views/admin/product-categories");
}
public function index()
{
$categories = ProductCategory::all();
return View::make('product-categories::index')
->with('title', 'All Product Categories')
->with('categories', $categories);
}
...
}
如果我在索引
控制器内查看dd
的$categories
值,我会得到正确的有说服力的集合,如下所示:
object(Illuminate\Database\Eloquent\Collection)[655]
protected 'items' =>
array (size=1)
0 =>
object(MyVendor\ProductsManager\Models\ProductCategory)[653]
protected 'table' => string 'product_categories' (length=18)
protected 'guarded' => ...
object(Illuminate\Database\Eloquent\Collection)[655]
protected 'items' =>
array (size=1)
0 => null
但是如果我dd
在索引
视图中$categories
的值,我会得到一个空的有说服力的集合,如下所示:
object(Illuminate\Database\Eloquent\Collection)[655]
protected 'items' =>
array (size=1)
0 =>
object(MyVendor\ProductsManager\Models\ProductCategory)[653]
protected 'table' => string 'product_categories' (length=18)
protected 'guarded' => ...
object(Illuminate\Database\Eloquent\Collection)[655]
protected 'items' =>
array (size=1)
0 => null
我在其他控制器上也采用了类似的方法,它们工作得很好。我不知道这个控制器有什么问题。我已经使用Laravel很长一段时间了,以前从未遇到过这个问题。也许我遗漏了什么,或者最近的软件包更新出了问题。不管是什么,都快把我逼疯了。你知道问题出在哪里吗
另外,我正在使用Laravel Framework 4.2.6版我终于找到了问题所在。我使用
robclancy/presenter
包在视图中包装和渲染对象。为此,模型必须实现一个PresentableInterface
接口。实现该接口需要该方法具有getPresenter
方法。我在模型中添加了getPresenter
方法,但是我忘记了从该方法返回presenter。返回演示者解决了问题
getPresenter
方法中的代码就是我忘记编写的代码
public function getPresenter()
{
return new ProductCategoryPresenter($this);
}
谢谢大家的帮助。您也可以发布视图和模型代码吗?也许您可以使用相同的变量名调用
view::composer
,如果您将categories
重命名为其他名称会怎么样?您可能会使用此名称对该视图进行其他全局绑定。重命名绑定。您是否尝试过View::make('product-categories::index',['title'=>'所有产品类别','categories'=>$categories)
?