5

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.

7
  • This seems to be $category->categories_name an array so you need to iterate over it Commented Mar 30, 2017 at 16:45
  • try @foreach($moduleCategories->categories_name as $category) and in the href link $category->name or however you named the field Commented Mar 30, 2017 at 16:48
  • Use whereIn for array Commented Mar 30, 2017 at 16:58
  • @Carlos the problem is in the query not in the foreach Commented Mar 30, 2017 at 17:03
  • @Thamilan I am getting this error when I do this Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in Commented Mar 30, 2017 at 17:04

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

9 Comments

I get it. But I think I need to escape the > because it is not closing properly.
what part are you referring > to?
-> in the query. It is outputting the whole thing after ->
i suggest you research a little over Laravels eloquent relationship laravel.com/docs/5.4/eloquent-relationships#one-to-many, i will update my answer with a quick example
Why have you removed the second query? and what does this $modules->module_categories as $category do?
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.