Laravel v5.6错误,显示此“未定义偏移量:0(视图:C:\ xampp \ htdocs \ schoolmanagement \ resources \ views \ displaycombinedata.blade.php)" [英] Laravel v5.6 error showing this "Undefined offset: 0 (View: C:\xampp\htdocs\schoolmanagement\resources\views\displaycombinedata.blade.php)"

查看:50
本文介绍了Laravel v5.6错误,显示此“未定义偏移量:0(视图:C:\ xampp \ htdocs \ schoolmanagement \ resources \ views \ displaycombinedata.blade.php)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据合并到我已经创建的表中.这是将显示它的代码.这两张桌子的名字是学生,老师.在这段代码中,我收到

I am trying to combine data to table that i had already created. And this is the code which will display it. The two table name is students,teachers. In this code i am getting an error message of

未定义偏移量:0(视图:C:\ xampp \ htdocs \ schoolmanagement \ resources \ views \ displaycombinedata.blade.php)

Undefined offset: 0 (View: C:\xampp\htdocs\schoolmanagement\resources\views\displaycombinedata.blade.php)

请帮帮我.

<head>
    <title>
        Display Combined Data
    </title>
    <h1>Display Combine Data from Database</h1>
</head>
<body>
    <table>
        <thead>
            <th>S.No.</th>
            <th>Student Name</th>
            <th>Student Class</th>
            <th>Student Age</th>
            <th>class Teacher</th>
            <th>Teacher salary</th>
        </thead>
        <tbody>
            <?php for($i=1; $i<=DB::table('students')->count(); $i++):?>
                <tr>
                    <?php $result=DB::table('students')->where('students_id','=',$i)->get();
                    ?>
                    <td>{{($result[0]->students_id)}}</td>
                    <td>{{($result[0]->students_name)}}</td>
                    <td>{{($result[0]->students_class)}}</td>
                    <td>{{($result[0]->students_age)}}</td>
                    <td>{{($result[0]->class_teacher)}}</td>

                    <?php $result1= DB::table('teachers')->where('teachers_name','=',$result[0]->class_teacher)->get(); 
                    if($result1  == null) {?>
                    <td>NA</td>
                    <?php } else{?>
                    <td>{{($result1[0]->salary)}}}</td>
                    <?php } ?>
                </tr>
                <?php: @endfor ?>
        </tbody>
    </table>
</body>

推荐答案

由于

$result=DB::table('students')->where('students_id','=',$i)->get();

为空.在这段代码中,

<td>{{($result[0]->students_id)}}</td>

您正在尝试从空数组的第一条记录中获取数据.

you're trying to get data from the first record of an empty array.

相反,您应该首先检查结果是否为空,并仅在不为空的情况下尝试获取该属性.

Instead, you should first check if the result is empty, and try to get the property only if it's not empty.

@if (!empty($result))
   <td>{{($result[0]->students_id)}}</td>
   <td>{{($result[0]->students_name)}}</td>
   <td>{{($result[0]->students_class)}}</td>
   <td>{{($result[0]->students_age)}}</td>
   <td>{{($result[0]->class_teacher)}}</td>
@endIf

请注意,您实际上不应该直接在Blade文件中查询数据库!

As a note, you really should not be directly querying the database within your Blade file!

这篇关于Laravel v5.6错误,显示此“未定义偏移量:0(视图:C:\ xampp \ htdocs \ schoolmanagement \ resources \ views \ displaycombinedata.blade.php)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆