Rehan
Rehan

Reputation: 4013

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.

Upvotes: 5

Views: 68394

Answers (2)

Hiroki
Hiroki

Reputation: 4163

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

Upvotes: 0

Carlos
Carlos

Reputation: 1321

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

Upvotes: 3

Related Questions