php - 将 mySQL 中的数据显示到 Laravel 中的 View 时出错

标签 php mysql laravel

我使用 Laravel 通过多查询将 Mysql 中的数据显示到 View。

这是我的路线:

Route::get('/procinstdetail','ProcDetailController@index');

这是我的 Controller :

use App\act_hi_taskinst;
use View;

class ProcDetailController extends Controller
{
    private $act_hi_taskinst;

    public function __construct(act_hi_taskinst $act_hi_taskinst) {
        $this -> act_hi_taskinst = $act_hi_taskinst;
    }

    public function index(act_hi_taskinst $act_hi_taskinst, Request $request) 
    {
        $procinstid = $request->get('id');

        return View::make('datatrackingdetail')->with('data', $this->act_hi_taskinst->getData($procinstid));
    }
}

还有我的模型:

class act_hi_taskinst extends Model
{

    public function getData($procinstid) {
        $data = array();

        $data['taskData'] = DB::select("SELECT NAME_, PRIORITY_, ASSIGNEE_, DUE_DATE_, START_TIME_, END_TIME_, PROC_DEF_ID_ FROM act_hi_taskinst WHERE PROC_INST_ID_ = $procinstid");

        $data['varData'] = DB::select("SELECT NAME_, TEXT_ FROM act_hi_varinst WHERE PROC_INST_ID_ = $procinstid");

        $data['subprocessData'] = DB::select("SELECT * FROM act_hi_procinst WHERE SUPER_PROCESS_INSTANCE_ID_ = $procinstid");

        return $data;
    }
}

最后是取数据查看:

<body>      
        <h2>Task</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>Name</th>
                <th>Priority</th>
                <th>Assignee</th>
                <th>Due Date</th>
                <th>Created</th>
                <th>Completed</th>
            </tr>

            @if(isset($data))
            @foreach($data as $processTaskDataValue)
            <tr>
                <td>{{ $processTaskDataValue -> NAME_ }}</td>
                <td>{{ $processTaskDataValue -> PRIORITY_ }}</td>
                <td>{{ $processTaskDataValue -> ASSIGNEE_ }}</td>
                <td>{{ $processTaskDataValue -> DUE_DATE_ }}</td>
                <td>{{ $processTaskDataValue -> START_TIME_ }}</td>
                <td>{{ $processTaskDataValue -> END_TIME_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>

        <h2>Variable</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>Name</th>
                <th>Value</th>
            </tr>

            @if(isset($data))
            @foreach($data as $processVarDataValue)
            <tr>
                <td>{{ $processVarDataValue -> NAME_ }}</td>
                <td>{{ $processVarDataValue -> TEXT_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>

        <h2>Sub Process</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>ID_</th>
                <th>PROC_INST_ID_</th>
                <th>BUSINESS_KEY_</th>
                <th>PROC_DEF_ID_</th>
                <th>START_TIME_</th>
                <th>END_TIME_</th>
                <th>DURATION_</th>
                <th>START_USER_ID_</th>
                <th>START_ACT_ID_</th>
                <th>END_ACT_ID_</th>
                <th>SUPER_PROCESS_INSTANCE_ID_</th>
                <th>DELETE_REASON_</th>
                <th>TENANT_ID_</th>
                <th>NAME_</th>
            </tr>

            @if(isset($data))
            @foreach($data as $subprocessDataValue)
            <tr>
                <td><a href= "{{ url('/procinstdetail') }}?id={{ $subprocessDataValue -> ID_ }}&endtime={{ $subprocessDataValue -> END_TIME_ }}"> {{$subprocessDataValue -> ID_ }}</a></td>
                <td>{{ $subprocessDataValue -> PROC_INST_ID_ }}</td>
                <td>{{ $subprocessDataValue -> BUSINESS_KEY_ }}</td>
                <td>{{ $subprocessDataValue -> PROC_DEF_ID_ }}</td>
                <td>{{ $subprocessDataValue -> START_TIME_ }}</td>
                <td>{{ $subprocessDataValue -> END_TIME_ }}</td>
                <td>{{ $subprocessDataValue -> DURATION_ }}</td>
                <td>{{ $subprocessDataValue -> START_USER_ID_ }}</td>
                <td>{{ $subprocessDataValue -> START_ACT_ID_ }}</td>
                <td>{{ $subprocessDataValue -> END_ACT_ID_ }}</td>
                <td>{{ $subprocessDataValue -> SUPER_PROCESS_INSTANCE_ID_ }}</td>
                <td>{{ $subprocessDataValue -> DELETE_REASON_ }}</td>
                <td>{{ $subprocessDataValue -> TENANT_ID_ }}</td>
                <td>{{ $subprocessDataValue -> NAME_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>
    </body>

但是我收到错误:

Trying to get property 'NAME_' of non-object.

我的代码有什么问题? 我是 Laravel 的新手,所以我真的不知道如何修复它。

最佳答案

在您的模型中,您正在创建包含键 taskData、varData 和 subprocessData 的数组,而您在获取数据时忘记将此键用于 $data 数组

    <body>      
        <h2>Task</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>Name</th>
                <th>Priority</th>
                <th>Assignee</th>
                <th>Due Date</th>
                <th>Created</th>
                <th>Completed</th>
            </tr>

            @if(isset($data))
            @foreach($data['taskData'] as $processTaskDataValue)
            <tr>
                <td>{{ $processTaskDataValue->NAME_ }}</td>
                <td>{{ $processTaskDataValue->PRIORITY_ }}</td>
                <td>{{ $processTaskDataValue->ASSIGNEE_ }}</td>
                <td>{{ $processTaskDataValue->DUE_DATE_ }}</td>
                <td>{{ $processTaskDataValue->START_TIME_ }}</td>
                <td>{{ $processTaskDataValue->END_TIME_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>

        <h2>Variable</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>Name</th>
                <th>Value</th>
            </tr>

            @if(isset($data))
            @foreach($data['varData'] as $processVarDataValue)
            <tr>
                <td>{{ $processVarDataValue->NAME_ }}</td>
                <td>{{ $processVarDataValue->TEXT_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>

        <h2>Sub Process</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>ID_</th>
                <th>PROC_INST_ID_</th>
                <th>BUSINESS_KEY_</th>
                <th>PROC_DEF_ID_</th>
                <th>START_TIME_</th>
                <th>END_TIME_</th>
                <th>DURATION_</th>
                <th>START_USER_ID_</th>
                <th>START_ACT_ID_</th>
                <th>END_ACT_ID_</th>
                <th>SUPER_PROCESS_INSTANCE_ID_</th>
                <th>DELETE_REASON_</th>
                <th>TENANT_ID_</th>
                <th>NAME_</th>
            </tr>

            @if(isset($data))
            @foreach($data['subprocessData'] as $subprocessDataValue)
            <tr>
                <td><a href= "{{ url('/procinstdetail') }}?id={{ 
                $subprocessDataValue -> ID_ }}&endtime={{ $subprocessDataValue -> 
                 END_TIME_ }}"> {{$subprocessDataValue -> ID_ }}</a></td>
                <td>{{ $subprocessDataValue->PROC_INST_ID_ }}</td>
                <td>{{ $subprocessDataValue->BUSINESS_KEY_ }}</td>
                <td>{{ $subprocessDataValue->PROC_DEF_ID_ }}</td>
                <td>{{ $subprocessDataValue->START_TIME_ }}</td>
                <td>{{ $subprocessDataValue->END_TIME_ }}</td>
                <td>{{ $subprocessDataValue->DURATION_ }}</td>
                <td>{{ $subprocessDataValue->START_USER_ID_ }}</td>
                <td>{{ $subprocessDataValue->START_ACT_ID_ }}</td>
                <td>{{ $subprocessDataValue->END_ACT_ID_ }}</td>
                <td>{{ $subprocessDataValue->SUPER_PROCESS_INSTANCE_ID_ }}</td>
                <td>{{ $subprocessDataValue->DELETE_REASON_ }}</td>
                <td>{{ $subprocessDataValue->TENANT_ID_ }}</td>
                <td>{{ $subprocessDataValue->NAME_ }}</td>
            </tr>
            @endforeach
            @endif
          </table>
        </body>

关于php - 将 mySQL 中的数据显示到 Laravel 中的 View 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155273/

相关文章:

php - 如何从 laravel 重启 apache2?

mysql - 为什么数据库中不存在该表

php - 项目外的 Laravel 文件系统

php - 仅验证正整数

php - Symfony 未捕获异常 - 'The process has been signaled with signal "9"

mysql - 如何使用 App Engine 从 eclipse 连接到 google cloud sql 实例?

php - 打印数组元素时 undefined offset

javascript - 仅使用 Javascript 将 Canvas 图像保存到服务器

php - 将参数从主线程传递到新线程

mysql - 标准化数据(1NF)仍然有意义吗? (表中有空字段有什么问题?)