Trying to get property of non-object in Laravel 5.1 - php

Firstly it works fine But I don't know how to come this error in this page but this problem can be solve when i changed the code in view {{$training->first()->sectionsCount}} but my specification has been incorrect.
My complete error is:-
ErrorException in Training.php line 51: Trying to get property of non-object (View:resources/views/Training/index.blade.php)
My code in model is:-
public function sectionsCountRelation()
{
return $this->hasOne('App\Schedule')->selectRaw('training_id, count(*) as count')->groupBy('training_id')->where('training_end_date','<',carbon::now());
}
public function getSectionsCountAttribute()
{
return $this->sectionsCountRelation->count;<!--This is line 51 -->
}
In controller is
public function index()
{
$training = Training::with('sectionsCountRelation')->get();
return view('Training.index',compact('training'));
}
In View:-
#foreach ($training as $training)
<tr>
<td>{{$i}}</td>
<td>{{ $training->category }}</td>
<td>{{ $training->topic }}</td>
<td>{{$training->sectionsCount}}</td>
<td>Update</td>
<td>Schedule</td>
<td>
{!! Form::open(['method' => 'DELETE', 'route'=>['training.destroy', $training->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
<?php $i++;?>
{!! Form::close() !!}
</td>
</tr>
#endforeach

Looks like you have some Trainings that have no Schedule assigned. That means, $this->sectionsCountRelation returns null and you can't access ->count on it. Try checking for null:
public function getSectionsCountAttribute()
{
return $this->sectionsCountRelation === null ? 0 : $this->sectionsCountRelation->count;
}

Related

Data doesn't display

Need some help here, I'm using Laravel app, my problem is my data wont display on the table. I tried some ways to display my data but it doesn't work.
Here's my code.
<tbody>
#if (count($expenses) > 0)
#foreach ($expenses as $expense)
<tr data-entry-id="{{ $expense->id }}">
<td field-key='expense_category'>{{ $expense->expense_category->name or '' }}</td>
<td field-key='entry_date'>{{ $expense->entry_date }}</td>
<td field-key='amount'>{{ $expense->amount }}</td>
<td field-key='created_by'>{{ $expense->created_by->name or '' }}</td>
<td>
#can('view')
#lang('quickadmin.qa_view')
#endcan
#can('edit')
#lang('quickadmin.qa_edit')
#endcan
#can('delete')
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.qa_are_you_sure")."');",
'route' => ['expenses.destroy', $expense->id])) !!}
{!! Form::submit(trans('quickadmin.qa_delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
#endcan
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="9">#lang('quickadmin.qa_no_entries_in_table')</td>
</tr>
#endif
</tbody>
here is my expenseController.
but i have never touched this code, i always work on the table form.
class ExpensesController extends Controller
{
/**
* Display a listing of Expense.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
if (! Gate::allows('expense_access')) {
return abort(401);
}
if ($filterBy = Input::get('filter')) {
if ($filterBy == 'all') {
Session::put('Expense.filter', 'all');
} elseif ($filterBy == 'my') {
Session::put('Expense.filter', 'my');
}
}
$expenses = Expense::all();
return view('admin.expenses.index', compact('expenses'));
}
}
try to find are you getting data from DB or not ....
$expenses = Expense::all();
dd($expenses);
It will show your data collection.
As I Think you are not getting any data from here.

Laravel Parameter passing error from controller to view

I want to pass the parameter $questions to view, but it gives the following error:
ErrorException (E_ERROR)
Undefined variable: questions (View: C:\Users\Krishan\Documents\GitHub\GroupProject\lcurve\resources\views\quizz\questions\index.blade.php)
This is my controller index function part:
public function index()
{
$questions = Question::all();
return view('quizz/questions.index', compact('questions'));
}
This is a part Of my view:
<tbody>
#if (count($questions_options) > 0)
#foreach ($questions_options as $questions_option)
<tr data-entry-id="{{ $questions_option->id }}">
<td></td>
<td>{{ $questions_option->question->question_text or '' }}</td>
<td>{{ $questions_option->option }}</td>
<td>{{ $questions_option->correct == 1 ? 'Yes' : 'No' }}</td>
<td>
View-->
<!--Edit-->
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.are_you_sure")."');",
'route' => ['questions_options.destroy', $questions_option->id])) !!}
{!! Form::submit(trans('quickadmin.delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endif
</tbody>
enter image description here
where is questions_options coming from? You are passing questions. So your for loop should be
#if (count($questions) > 0)
#foreach ($questions as $question)
//rest of your code
#endforeach
#endif
and your return view part can be return view(quizz.questions.index, compact('questions'))
Firstly, the error message you have mention should be shown. The error message should be:
Undefined variable: questions_options (View:C:\Users\Krishan\Do........
Because you are passing questions to view but you are accessing question_options in view. So, it should say question_options in undefined in view.
Besides, do you know you can avoid this count check? You can use laravel's forelse tag here a below:
#forelse($questions as $question)
//Your table goes here
#empty
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endforelse

Passing data relation to view laravel 5.2

I finish to make relation in my model, but i dont know to passing data relation to view, hope you can help me.
Model Siswa
public function Absen()
{
return $this->hasMany(Absen::class);
}
Model Absen
public function Siswa()
{
$this->belongsTo(Siswa::class);
}
Absen Table
Siswa Table
AbsenController#index
public function index()
{
$absen = Absen::where('level', '=', 'Siswa')->get();
return view('absen.index')->with('data', $absen);
}
index.blade.php
#foreach($data as $index => $value)
<tr>
<td>{{ $index+1 }}</td>
<td>{{ $value->nama }}</td>
<td>{{ $value->keterangan }}</td>
<td>
{!! Form::open(['route' => ['siswa.destroy', $value->id],
'method' => 'DELETE']) !!}
{{ Form::submit('Hapus', ['class' => 'btn btn-danger']) }}
<a href="{{ route('siswa.edit', $value->id) }}" class="btn
btn-warning">Edit</a>
{!! Form::close() !!}
</td>
</tr>
#endforeach
The problem is the $value->nama not showing in my view, but i done to create relation in table siswa and table absen.Thank
nama is not an attribute of Absen class, yet your value is an Absen class. Though you have defined the relationship between Absen and Siswa, attributes of Siswa are still not directly inherited to Absens. You can first get the Siswa of the Absen and then get the attributes of the Siswa.
So what you have to do is:
Call $value->Siswa->nama instead of $value->nama, and you should have your nama displayed.

Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails

what is my mistake?
my controller :
public function index()
{
$employees = Employee::all();
return view('employee.view_all_employee_details', compact('employees'));
}
My Employee Model function :
public function basicdetails()
{
return $this->hasOne('App/EmployeeAdditionalDetail' , 'emp_id' , 'emp_id');
}
My EmployeeAdditionalDetail model :
public function employeeDetails()
{
return $this->belongsTo('App\Employee' , 'emp_id' , 'emp_id');
}
My View
#if(count($employees) > 0)
#foreach($employees as $employee)
<tr>
<td> {{ $employee->first_name }} </td>
<td> {{ $employee->manager_id }} </td>
<td>
{{ $employee->basicdetails->personal_email }} </p> **Error showing here**
</td>
</tr>
#endforeach
#else
{!! "<tr><td>No Recod Found</td></tr>" !!}
#endif
My Error:
ErrorException in
C:\xampp\htdocs\socialhub\app\Http\Controllers\EmployeeController.php
line 22: Undefined property:
Illuminate\Database\Eloquent\Collection::$basicdetails
I suspect the issue is with the third parameter of $this->hasOne(..), it should be 'local_key', normally this would be 'id'.
Try removing the third parameter, or put the correct local_key:
public function basicdetails()
{
return $this->hasOne('App/EmployeeAdditionalDetail' , 'emp_id');
}

Undefined property: Illuminate\Database\Eloquent\Collection::$id

I don't know exactly why I'm getting this error as indicated by title. I am trying to write the code fro deleting records. This is the code below.
<TABLE >
#foreach($users as $user)
<TR><TD>{{ $user->id }}</TD><TD>{{ $user->firstname }}</TD><TD><div id="divContainer"><div class="theDiv"><form action="/users/{{ $users->id }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }}<button class="css-deletebutton">DELETE</button></form></div></div></TD><TD>{!! Form::submit('DEACTIVATE', ['class'=>'css-statusbutton']) !!}</TD></TR>
#endforeach
<!--{!! $users->render() !!}-->
</TABLE>
This is the route:
Route::resource('users', 'UserController');
This is the controller method:
public function show($id)
{
$users = User::find($id);
return view('userpages.show')->with('users', $users);
}
This is the view to be displayed for deletion of any record.
#section('body')
{!! Form::open([ 'method' => 'delete', 'route' => ['users.destroy', $users->id]]) !!}
<TABLE>
<TR><TD>{{ $users->firstname }}</TD><TD>{{ $users->lastname }}</TD><TD>{{ $users->email }}</TD><TD>{{ $users->username }}</TD><TD>{!! Form::submit('DELETE', ['class'=>'css-deletebutton']) !!}</TD></TR>
</TABLE>
{!! Form::close() !!}
#stop
This is the error message I'm getting:
ErrorException in acf1a7ad2174bd2b743200b1a50b4c9f line 14:
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\Users\ken4ward\Documents\xampp\htdocs\schoolproject\resources\views\userpages\index.blade.php)
I can see you are referring to $users->id within the foreach loop in the first code snippet - that's what's causing the error you're getting. $users is the variable that holds the collection and $user is the variable that you should use to access user's data, including user's ID.

Categories