Reputation: 27
I ran into an error while fetching data from my goals
table. I'm trying to display all the tables data using foreach
loop. But I can't due to unknown property error. I'm still learning laravel so kindly help.
Here's my controller code.
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] );
}
}
Then my index.blade.php code
@foreach($goals as $goal)
<tr>
<th colspan="3">{{ $goal->goal }}</th>
<th colspan="7">{{$goal->checklist}}</th>
<th colspan="2">{{$goal->status}}</th>
<th colspan="2">{{$goal->Start}}</th>
<th colspan="2">{{$goal->finish}}</th>
<th colspan="2">{{$goal->score}}</th>
<th colspan="7">{{$goal->remarks}}</th>
</tr>
@endforeach
I get an exception:
(2/2) ErrorException Undefined property: stdClass::$Start (View: /opt/lampp/htdocs/ToDo/resources/views/index.blade.php)
What could be the problem with my code?
Upvotes: 0
Views: 7315
Reputation: 8618
There is misleading of first letter. Change Start
to start
<th colspan="2">{{$goal->start}}</th>
And for good practice in controller change code to
$goals = DB::select('select `checklist`, `status`, `start`, `finish`, `score`, `remarks` from goals');
You must be get in db only needed info
also delete this part
if (empty($goals)) {
$goals = 0;
}
For not get error when goals is empty
Upvotes: 2