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

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.

  • 写回答

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?