简体   繁体   中英

array to string conversion in 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.

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

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM