4

I'm new to laravel and I tried to clear the problem from here

I have controller like the following:

foreach($users as $user){
    $message[] = Model::where('id',$user->id)->get();
}
$data['tests'] = $message;
return View::make('user.index', $data);

My View is:

@foreach($tests['message'] as $test)
    id : {{$test->id}}
@endforeach

which gives me Undefined index: message

I had dump the the $data in the controller. my array is as shown below. I place the var_dump in the controller before the return statement. my var_dump($data) is showing:

    array(1) {
        ["tests"] => array(2) {
            [0] => object(Illuminate\ Database\ Eloquent\ Collection) #515 (1) { ["items":protected]= > array(2) {
                [0] => ["attributes": protected] => array(14) {
                    ["id"] => string(3) "12"....

        }
    }
            [1] => object(Illuminate\ Database\ Eloquent\ Collection) #515 (1) { ["items":protected]= > array(2) {
                [0] => ["attributes": protected] => array(14) {
                    ["id"] => string(3) "12"....

        }
    }
    }

what i'm doing wrong. please help me

5
  • Can you show your Model structure? It was depend on your model. Commented Nov 22, 2016 at 6:21
  • it consist only the table name Commented Nov 22, 2016 at 6:32
  • So, where your message come from? If key (message) not exist in your array, how you want to get with this key? Commented Nov 22, 2016 at 6:33
  • it came from the model table, obviously Commented Nov 22, 2016 at 6:34
  • Yes, that correct. Based on your code, I didn't see message key in your array. Commented Nov 22, 2016 at 6:35

3 Answers 3

4
@foreach($tests as $test)
//$test is an array returned by get query.
  @foreach($test as $item)
    id : {{$item->id}}
  @endforeach
@endforeach

get return array, if you want to return one element, use find() or first().

Sign up to request clarification or add additional context in comments.

2 Comments

it works, Thanks @Kris Roofe... anyway, is it the correct way of passing the data, or i'm doing dump?
in your case, use the get() is the proper way prefer to first() and find().
2

Your $tests['message'] should be $tests because you have not define message in your controller.

$message = array();
foreach($users as $user){
    $message[] = Model::where('id',$user->id)->get();
}
$data['tests'] = $message;
return View::make('user.index', $data);

View

@foreach($tests as $test)
    id : {{$test->id}}
@endforeach

3 Comments

I have tried it.. it gives Undefined property: Illuminate\Database\Eloquent\Collection::$id
array(1) { ["tests"]=> array(2) { [0]=> object(Illuminate\Database\Eloquent\Collection)#.... [1]=>....
it consist id for sure
2

Try this:

$users = App\User::all()
foreach($users as $user){
    $message[] = Model::where('id',$user->id)->get();
}
$data['tests'] = $message;
return view( 'user.index', compact('data') );

In views/user.index.blade.php

@foreach($data['tests'] as $test)
    id : {{$test->id}}
@endforeach

1 Comment

it gives me undefinde property of $id

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.