[英]array to string conversion in laravel
I am trying to fetch some data from table in laravel 5.0 like so 我试图从laravel 5.0中的表中获取一些数据
public function index()
{
$data = DB::table('modules')->get();
return view("BaseView.home")->with('data',$data);
}
this is my view 这是我的看法
@foreach($data as $modules)
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $modules->module_name }}<i class="plusMinus fa fa-plus-square plusMinusSpacing" aria-hidden="true"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
{!! $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get() !!}
@foreach($moduleCategories as $category)
<a class="dropdown-item" href="#">{{ $category->categories_name }}</a>
@endforeach
</div>
</li>
@endforeach
$module->id is obtained from another query result. $ module-> id从另一个查询结果中获取。 Now when I try to run this I am getting
Array to string conversion
. 现在,当我尝试运行它时,我得到
Array to string conversion
。 Can someone point out the mistake. 有人可以指出错误。 The expected output is > 1 in the sense there can be multiple categories names matching that condition.
在某种意义上,预期输出> 1,可以有多个类别名称匹配该条件。
The problem is that you are trying to put php logic inside an echo {{ ... }}
. 问题是你试图将php逻辑放在echo
{{ ... }}
。 You are trying to echo out a collection or an array of data, hence the error you are getting, Array to string conversion
. 您正在尝试回显一个集合或数组,因此您将获得错误,
Array to string conversion
。 The proper way to do this is to do the logic in the controller. 正确的方法是在控制器中执行逻辑。 But for a quick fix, replace:
但要快速解决,请更换:
{!! $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get() !!}
to 至
<php $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get(); ?>
Never use {{ ... }}
or {!! ... !!}
永远不要使用
{{ ... }}
或{!! ... !!}
{!! ... !!}
to put logic, those are to echo out a string. {!! ... !!}
放置逻辑,那些是回显出一个字符串。
==EDIT== == ==编辑
I suggest to use laravels eloquent relationship methods, this will simplify the code, and seperate the logic from the view. 我建议使用laravels雄辩的关系方法,这将简化代码,并从视图中分离逻辑。
Note: Expecting if you are using laravel naming convention. 注意:期望您是否使用laravel命名约定。
On your Modules
model, add a relationship to ModuleCategory
like so: 在
Modules
模型上,添加一个与ModuleCategory
的关系,如下所示:
public function module_categories()
{
return $this->hasMany('App\ModuleCategory');
}
On your controller, on the index method replace : 在您的控制器上,在索引方法上替换:
$data = DB::table('modules')->get();
with 同
$data = Modules::get();
and finally on the view, change it like so: 最后在视图上,改变它:
@foreach($data as $modules)
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $modules->module_name }}<i class="plusMinus fa fa-plus-square plusMinusSpacing" aria-hidden="true"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
@foreach($modules->module_categories as $category)
<a class="dropdown-item" href="#">{{ $category->categories_name }}</a>
@endforeach
</div>
</li>
@endforeach
As I commented on Carlos's solution, here is how I solved the same error... 正如我对卡洛斯的解决方案发表评论,以下是我如何解决同样的错误......
//Controller
$users = User::where('blah', 'blah')->get();
return view('index', [
'users' => $users,
]);
//View
<script type="text/javascript">
angular.module("app").factory("DataService", function() {
return {
users: {!! $users !!},
};
});
</script>
As discussed above, this will result in the Array to string conversion
error, since $users
is an array. 如上所述,这将导致
Array to string conversion
错误,因为$users
是一个数组。
However, this error won't happen if $users
is a `collection' 但是,如果
$users
是“集合”,则不会发生此错误
Ie 即
//Controller
$users = User::all();
return view('index', [
'users' => $users,
]);
//This is ok
Or, 要么,
//Controller
$users = collect([]);
return view('index', [
'users' => $users,
]);
//This is fine too
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.