doulan8054
2017-03-30 16:37
浏览 442
已采纳

laravel中的数组到字符串转换

I am trying to fetch some data from table in laravel 5.0 like so

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.

图片转代码服务由CSDN问答提供 功能建议

我试图从laravel 5.0中的表中获取一些数据,如此

  public function index()
 {
 $ data = DB :: table('modules') - &gt; get(); 
 
返回视图(“BaseView.home”) - &gt; with  ('data',$ data); 
} 
   
 
 

这是我的观点

  @foreach(  $ data as $ modules)
&lt; li class =“nav-item dropdown”&gt; 
&lt; a class =“nav-link dropdown-toggle”href =“#”id =“navbarDropdownMenuLink”data-toggle =  “dropdown”aria-haspopup =“true”aria-expanded =“false”&gt; 
 {{$ modules-&gt; module_name}}&lt; i class =“plusMinus fa fa-plus-square plusMinusSpacing”aria-hidden =  “true”&gt;&lt; / i&gt; 
&lt; / a&gt; 
&lt; div class =“dropdown-menu”aria-labelledby =“navbarDropdownMenuLink”&gt; 
 {!!  $ moduleCategories = DB :: table('module_categories') - &gt; where('modules_id','=',$ modules-&gt; id) - &gt; get()!!} 
 @foreach($ moduleCategories as $ 类别)
&lt; a class =“dropdown-item”href =“#”&gt; {{$ category-&gt; categories_name}}&lt; / a&gt; 
 @endforeach 
&lt; / div&gt; 
&lt;  ; / li&gt; 
 @endforeach 
   
 
 

$ module-&gt; id是从另一个查询结果中获取的。 现在当我尝试运行这个时,我得到 Array to string conversion 。 有人可以指出错误。 期望的输出是> 在某种意义上,可以有多个类别名称匹配该条件。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dstd2129 2017-03-30 17:19
    已采纳

    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:

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

    Note: Expecting if you are using laravel naming convention.

    On your Modules model, add a relationship to ModuleCategory like so:

    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
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douweng7308 2017-07-31 05:58

    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.

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

    I.e.

    //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
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题