php Laravel 中数组到字符串的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43123660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:46:56  来源:igfitidea点击:

array to string conversion in laravel

phplaravel-5

提问by Rehan

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. Now when I try to run this I am getting 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.

$module->id 是从另一个查询结果中获取的。现在,当我尝试运行它时,我得到了Array to string conversion. 有人能指出错误吗。预期输出 > 1,因为可以有多个类别名称匹配该条件。

采纳答案by Carlos

The problem is that you are trying to put php logic inside an echo {{ ... }}. You are trying to echo out a collection or an array of data, hence the error you are getting, Array to string conversion. The proper way to do this is to do the logic in the controller. But for a quick fix, replace:

问题是您试图将 php 逻辑放在 echo 中{{ ... }}。您正在尝试回显一个集合或一组数据,因此您得到的错误是Array to string conversion. 正确的方法是在控制器中执行逻辑。但是为了快速修复,请替换:

{!! $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.

我建议使用laravel eloquent关系方法,这样可以简化代码,将逻辑与视图分离。

Note: Expecting if you are using laravel naming convention.

注意:如果您正在使用 laravel 命名约定,请期待。

On your Modulesmodel, add a relationship to ModuleCategorylike so:

在您的Modules模型上,添加如下关系ModuleCategory

public function module_categories()
{
    return $this->hasMany('App\ModuleCategory');
}

On your controller, on the index method replace :

在您的控制器上,在 index 方法上 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

回答by Hiroki

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 conversionerror, since $usersis an array.

如上所述,这将导致Array to string conversion错误,因为它$users是一个数组。

However, this error won't happen if $usersis a `collection'

但是,如果$users是“集合” ,则不会发生此错误

I.e.

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