1

I have created a view page where I am getting an Eloquent variable from my controller. That variable points to the elements in a table. This is my current code.

  <table border="0px">
    <tr>
    @foreach ($teacher as $value) 
        <?php    
            $tid = $value->teacherID;
        ?>

        <tr>
            <td> <a href="info/{{ $tid }}"> {{ $value['teacher_name'] }} </a></td>
            <td>{{ $value['course_name'] }}</td>
        </tr>
        <hr>

    @endforeach
    </tr>
</table>

I am getting an unexpected output with this code. I am getting number of blank spaces in the beginning and then my table(mysql) elements. Please help me resolve the issue guys. Thanks, Regards.

2 Answers 2

2

Laravel follow this approach:

Controller function:

myFunction()
{
    $myData = array('1' => 'One', '2' => 'Two');    // sample data. You can get this data from DB
    return view('myView', array('data' => $myData));
}

myView.blade.php:

Here you can use the $data array like:

{{ $data['1'] }}
{{ $data['2'] }}

Now track your methods accordingly.

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

7 Comments

Thanks for replying @Mayank. returnView::make('viewb')->with('teacher', $teacher); //My controller stuff
Can I not continue with the same logic?
Yes you can return view('myView', array('data' => $myData)); and returnView::make('viewb')->with('teacher', $teacher); is same.
Ok, Then you mean to say I need to use index numbers while retrieving it. But what if new entries are kept on adding onto my table. Acc to you, I should retrieve it by indices which would be a static way. Right?
You can use foreach also. Its just a demo example bro
|
0
public function getTeachers(){
    $teachers = Teachers::all();
    return('teachers',compact('teachers'));
}

in blade file you can read properties in

 @foreach ($teacher as $value) 
    if($value != '' || $value != null)
        ID:{{$value->teacherID}}
 @endforeach

5 Comments

Thanks for replying @Mohammed. I am done with the functionality part, but while viewing I get some empty rows, which I do not want. Please help me out.
@AvinashJaiswal check whether the value exist or not in view!or you may have empty records in your DB.
Right! Cheers @MohamedNizar
Did you find solution from my answer?
Yes got it. Thanks once again!

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.