ErrorException未定义的属性stdClass :: $ Start

时间:2018-07-07 19:10:22

标签: laravel laravel-5

public class MeetingCenter { ... [JsonProperty(PropertyName = "id")] public string MeetingCenterId { get; set; } ... } 表中获取数据时遇到错误。我正在尝试使用goals循环显示所有表数据。但是由于未知的属性错误,我无法执行此操作。我还在学习laravel,所以请帮忙。 这是我的控制器代码。

foreach

然后输入我的index.blade.php代码

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use App\goal;
use DB;

class GoalController extends Controller
{
    public function postAddGoal(Request $request){
        $this->validate($request, [
            'goal'=>'required',
            'checklist'=>'required'
        ]);
        $goal_name=$request['goal'];
        $checklist=$request['checklist'];

        $goal = new goal();
        $goal->goal=$goal_name;
        $goal->checklist=$checklist;
        $goal->status='PENDING';
        $goal->start='1002-1-1';
        $goal->finish='1002-1-1';
        $goal->remarks="NULL";
        $goal->score="0";
        $goal->save();

        redirect()->route('seegoals');
    }

    public function getGoals(){
        $goals = DB::select(' select * from goals ');
        if(empty($goals)){
            $goals=0;
        }
        return view('/index', ['goals'=>$goals] );
    }
}

我得到一个例外:

  

(2/2)ErrorException未定义的属性:stdClass :: $ Start(查看:   /opt/lampp/htdocs/ToDo/resources/views/index.blade.php)

我的代码可能是什么问题?

1 个答案:

答案 0 :(得分:2)

首字母存在误导。将Start更改为start

<th colspan="2">{{$goal->start}}</th>

为了使控制器的良好实践将代码更改为

$goals = DB::select('select `checklist`, `status`, `start`, `finish`, `score`, `remarks` from goals');

您必须仅在数据库中获得所需的信息
还要删除这部分

if (empty($goals)) {
    $goals = 0;
}

当目标为空时不会出错