(2/2) ErrorException Trying to get property of non-object - php

In course table have department field where the value is number and when i get data form this table i don't want to show number.I want to show department name but i can not do it i want expert help.I have attached picture and codethis is course table.
Controller code :
public function index()
{
$allCourse = Course::paginate(10);
return view ('Admin.course.index',['allCourse'=> $allCourse]);
}
index.blade.php
#foreach($allCourse as $course)
<tr class="info">
<td class="success">{{$course->id}}</td>
<td class="success">{{$course->code}}</td>
<td class="success">{{$course->name}}</td>
<td class="success">{{$course->credit}}</td>
<td class="success">{{$course->description}}</td>
<td class="success">{{$course->department->$department}}</td>
<td class="success">{{$course->semester}}</td>
<td class="success">
<div class="btn btn-success">
<i class="fa fa-pencil" aria-hidden="true"></i>
</div>
<div class="btn btn-danger">
<a class="delete_link" href="{{ url('/Admin/course',[$course->id,'delete']) }}"><i class="fa fa-trash" aria-hidden="true"></i></a>
</div>
{{-- {!! Form::open(['url' => 'foo/bar']) !!}
<div>
{{ Form::button('<i class="fa fa-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger '] ) }}
</div>
{!! Form::close() !!}--}}
</td>
</tr>
#endforeach
</table>
{{ $allCourse->links() }}
</div>
</div>
</div>
#endsection
error page

Make sure you have Relation between course and department
You can find the documentation here
Secondly, you have a typo in your code. Change this to
<td class="success">{{$course->department->$department}}</td>
this
<td class="success">{{$course->department->department}}</td>
Hope this helps.

Eloquent just fetch everything inside your specified table. If you want department name (i assume you have department table), you need to define the relationship. Eloquent do have method for defining relationship.
For me, just use fluent 'join'.
Course::join('department_table','Course.department','=','department_table.department_id')->paginate(10);
Once you change your query to this, change from
{{$course->department->$department}}
to
{{$course->department_name}}
This is the easier way, but if you are willing to study, have a look here https://laravel.com/docs/5.5/eloquent-relationships

Related

Use of undefined constant category Laravel error

im working on laravel project and now its giving me this error:Use of undefined constant category - assumed 'category' (this will throw an Error in a future version of PHP)
and its pointing at lane 26
even though i declared the variable 'category' any ideas how to solve it ? thank you
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-body">
<table class="table table-hover">
<thead>
<th>
Category Name
</th>
<th>
Editing
</th>
<th>
Deleting
</th>
</thead>
<tbody>
#foreach($categories as $category)
<tr>
<td>{{$category->name}}</td>
<td>
<a href="{{ route(category.edit, ['id=>$category->id']) }}" class="btn btn-light btn-xs">
Edit
</a>
</td>
<td>
<a href="{{route(category.delete, ['id => $category->id'])}}" class="btn btn-danger btn-xs">
Delete
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#stop
Use this route variable in commas, It's worked for me.
<a href="{{ route('category.edit', ['id=>$category->id']) }}" class="btn btn-light btn-xs">
Edit
</a>
You need to change these below routes and i hope it will work for you!
<a href="{{ route('category.edit', $category->id) }}" class="btn btn-light
btn-xs">Edit</a>
Delete

Undefined property: Illuminate\Support\Collection::$id in Laravel 5.2

I have a confusion and I need a help to solve.
I am trying to add a record to a table that depends on the initial record, it is an event log table related to the initial registration of a call record.
As I try to do this it is with an input hidden, but it does not receive the id and it generates the following error (Undefined property:Illuminate\Support\Collection::$id).
This is part of the view with the record button of the incidences.
#foreach ($data as $call)
<tr class="active">
<td align="center">{{ ++$i }}</td>
<td style="text-align: center">{{ $call->created_at->format('d - m - Y') }}</td>
<td>{{ $call->name }}</td>
<td>{{ $call->last_name }}</td>
<td align="center">
#if($call->type == 1)
<span class="label label-info">Saliente</span>
#else
<span class="label label-success">Entrante</span>
#endif
</td>
<td>{{ $call->phone }}</td>
<td>{{ $call->movil }}</td>
<td align="center">
<a class="btn btn-info btn-xs" href="{{ route('calls.show',$call->id) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Detalle de llamada"> <i class="material-icons">info_outline</i> </a>
#permission('role-edit')
<a class="btn btn-primary btn-xs" href="{{ route('calls.edit',$call->id) }}" data-toggle="tooltip" data-placement="top" title="Editar registro de llamada"> <i class="material-icons">create</i> </a>
#endpermission
<a class="btn btn-warning btn-xs" href="{{ route('comments.create', $call->id) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Registrar incidencia"> <i class="material-icons">event</i> </a>
{!! Form::open(['method' => 'DELETE','route' => ['calls.destroy', $call->id],'style'=>'display:inline']) !!}
#permission('role-delete')
<button type="submit" class="btn btn-danger btn-xs" data-toggle="tooltip" data-placement="top" title="Eliminar llamada"><i class="material-icons delete-white">delete</i></button>
#endpermission
{!! Form::close() !!}
</td>
</tr>
#endforeach
This is part of the view where the issue is logged, but I need to pass the id in the hidden field.
{!! Form::open(array('route' => 'comments.store','method' => 'POST')) !!}
<div class="col-md-12 col-xs-12">
<div class="input-group">
<div class="col-md-4 col-xs-4">
{!! Form::select('call_id', $calls, null, ['class' => 'form-control', 'placeholder' => 'Seleccionar cliente']) !!} <!--This is the select-->
{{ Form::hidden('call_id', $calls->id) }} <!--This is the hidden mode-->
</div>
<div class="col-md-8 col-xs-8">
{!! Form::text('name', null, array('placeholder' => 'Registrar incidencia','class' => 'form-control')) !!}
</div>
<span class="input-group-btn">
<button type="submit" class="btn btn-success btn-xs" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Guardar">
<i class="material-icons">save</i>
</button>
</span>
</div>
</div>
{!! Form::close() !!}
The error generated by the view is:
Undefined property: Illuminate\Support\Collection::$id
These are my methods for the controller (CommentController).
public function create()
{
$calls = Call::orderBy('id', 'asc')->lists('name', 'id');
return view('comments.create', compact('calls'));
}
public function store(Request $request)
{
//return $request->all();
$this->validate($request, [
'name' => 'required|unique:categories|max:255',
]);
$comments = Comment::create([
'name' => $request->get('name'),
'call_id' => $request->get('call_id'),
]);
return redirect()->route('comments.index')
->with('success','Comentario agregado correctamente!!!');
}
This is my route method.
Route::resource('comments','CommentController');
This is the call log view, when clicking the orange button calls the comment view to record an issue.
This is the view of record of incidents, here I have the dropdown but ideally instead of a dropdown can receive the ID of the record you select from the previous view.
Someone who can guide me, since I have used several methods and I have not been able to solve it.
Solve the confusion and method as follows:
The button to create the incidence in the view I defined it as follows:
#foreach ($data as $call)
<a class="btn btn-warning btn-xs" href="{{ route('comments.create', ['id' => $call->id]) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Registrar incidencia"> <i class="material-icons">event</i> </a>
#endforeach
In the create method of CommentController I structured it as follows:
public function create($id)
{
$calls = DB::table('calls')->find($id);
return view('comments.create', compact('calls'));
}
The store method of CommentController I structured it as follows:
public function store(Request $request)
{
//return $request->all();
$rules = [
'call_id' => 'required',
'comments_name' => 'required',
];
$messages = [
'call_id.required' => 'Debe seleccionar un código de llamada',
'comments_name.required' => 'Debe ingresar incidencia',
];
$this->validate($request, $rules, $messages);
$comments = Comment::create([
'comments_name' => $request->get('comments_name'),
'call_id' => $request->get('call_id'),
]);
return redirect()->route('calls.index')
->with('success','Incidencia agregada correctamente!!!');
}
The labels in the view create incidents are as follows:
{!! Form::open(array('route' => 'comments.store','method' => 'POST')) !!}
<div class="col-md-12 col-xs-12">
<div class="input-group">
<div class="col-md-12 col-xs-12">
{!! Form::text('comments_name', null, array('placeholder' => 'Nombres','class' => 'form-control')) !!}
{{ Form::hidden('call_id', $calls->id) }}
</div>
<span class="input-group-btn">
<a class="btn btn-warning btn-xs" href="{{ route('calls.index') }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Retornar">
<i class="material-icons">arrow_back</i>
</a>
<button type="submit" class="btn btn-success btn-xs" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Guardar">
<i class="material-icons">save</i>
</button>
</span>
</div>
</div>
{!! Form::close() !!}
And I defined the following route in the routes file:
Route::get('comments/create/{id}', [
'middleware' => 'auth',
'as' => 'comments.create',
'uses' => 'CommentController#create'
]);
This way you can create a call log with a list of many incidents.
If this method should be improved please give me the recommendations, but it was the way in which I could do it.
Thanks #RobFonseca and #CarlosAdames for your comments and help.
The lists method returns a collection instance. You need to convert the Collection into a plain array using the all method.
In your controller try this:
$calls = Call::orderBy('id', 'asc')->lists('name', 'id')->all();
You can read more about it in the following link The lists Method
Let me know if your problem is solved.

How to put HTML inside a <td> in Laravel Blade?

I want to put Bootstrap Icon inside of my blade..But the problem is i couldn't get the actual output..Means here is the sample code:
#if (($row->is_management) === 1)
<td> <span class="glyphicon glyphicon-ok"></span> </td>
#else
<td><span class="glyphicon glyphicon-remove"></span></td>
#endif
Means if status is 1 then green checked will come and in another condition red cross.
Can anyone suggest me what the actual mistake here?
It will work on bootstrap css with glyphicons fonts. Please make sure that u have bootstrap css called and have glyphicons fonts
#if (($row->is_management) == 1)
<td> <span class="glyphicon glyphicon-ok"></span> </td>
#else
<td><span class="glyphicon glyphicon-remove"></span></td>
#endif
Try this, use == instead of ===
Updated Code:
#if (($row->is_management) == 1)
{{ <td> <span class="glyphicon glyphicon-ok"></span> </td> }}
#else
{{ <td><span class="glyphicon glyphicon-remove"></span></td> }}
#endif

Laravel web app displaying incorrect database entries

I am trying to troubleshoot a web app that is not functioning 100% correctly that was built by a former employee. I have a novice understanding of PHP.
This is a relatively simple app that displays past exam files, but is locked down to the public. It is working correctly for the most part except for a weird thing happening with sample answer files. Not every exam has a sample answer file. The ones that don't have one should just be blank. However what is happening is that some exam files that do not have a corresponding answer file are displaying the button for exam files for other exams.
You can see from this image, that the first exam from Spring 1997 shows a blank space for sample answer which is correct. the next three exams are also correct in that they are showing the correct answer file, however, the last three exams do not actually have sample answer files in the database but are showing the sample answer for Spring 2004, like they are repeating the last file name that is in a specific sequence.
What I think is happening is that there is nothing in the code specifically saying to render a blank space if there is no corresponding answer file, but I don't know if this is actually the case. This is the code from index.blade.php:
<div class="row">
<h4>Browse past exams</h4>
<form class="form-inline">
<div class="form-group">
<label class="col-sm-2">Course</label>
<div class="col-sm-4">
{{ Form::select('course', $courses, Input::get('course', ''), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2">Faculty</label>
<div class="col-sm-4">
{{ Form::select('faculty', $faculties, Input::get('faculty', ''), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
</div>
</div>
{{ Form::close() }}
</form>
#if(PastExamsPublicController::isAdmin())
<div class="col-md-10 col-md-offset-1" style="margin-top: 25px; margin-bottom: 25px;">
<div class="btn-group" role="group">
<a href="{{ route('past-exams.utlaw.admin.exams.create') }}" class="btn btn-primary">
Add exam
</a>
<a href="{{ route('past-exams.utlaw.admin.faculty.index') }}" class="btn btn-primary">
Manage faculty
</a>
<a href="{{ route('past-exams.utlaw.admin.course.index') }}" class="btn btn-primary">
Manage courses
</a>
</div>
</div>
#endif
<hr>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-1 col-xs-12">
<table class="table table-hover" id="past-exams-table" class="sort">
<thead>
<tr>
<th class='sort-default'>Course</th>
<th>Session/year</th>
<th>Faculty</th>
<th class='no-sort'>Exam</th>
<th class='no-sort'>Sample Answers</th>
</tr>
</thead>
<tbody>
#foreach($exams as $exam)
<tr>
<td>{{ $exam->course->name }}</td>
<td data-sort="{{ $exam->year . $exam->session->name }}" >
{{ $exam->session->name . '/' . $exam->year }}
</td>
<td>{{ $exam->faculty->last_name . ', ' . $exam->faculty->first_name }}</td>
<?php
foreach($exam->files as $file) {
if($file->type == 'a') {
$answers = $file->filename;
}
else{
$examf = $file->filename;
}
}
?>
<td>
<a href="{{ asset('files/' . $examf) }}" class="btn btn-warning">
<span class="glyphicon glyphicon-download" aria-hidden="true"></span> Exam
</a>
</td>
<td>
#if(isset($answers))
<a href="{{ asset('files/' . $answers) }}" class="btn btn-primary">
<span class="glyphicon glyphicon-download" aria-hidden="true"></span> Sample Answers
</a>
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
Thank you in advance for any guidance in helping point me to the genesis of the error.
Your problem exists here:
foreach($exam->files as $file) {
if($file->type == 'a') {
$answers = $file->filename;
}
else{
$examf = $file->filename;
}
}
This is contained inside a foreach loop so gets run for each exam. If the exam type is "a", the $answers variable gets set, but there is nothing that clears it if it is not set. Would recommend initializing it to null before the loop:
$answers = null;
$examf = null;
foreach($exam->files as $file) {
if($file->type == 'a') {
$answers = $file->filename;
}
else{
$examf = $file->filename;
}
}
EDIT: As pointed out by #mopo922, you will want to do the same with $examf, which I added above.

Laravel 5.2 - Getting a pivot table additional attribute

I'm using Laravel to track Gear in Wearhouses. For that I have 2 tables: gears and wearhouses, and another pivot table: gear_wearhouse.
The relationship between the two tables is a many-to-many relationship, and in my pivot table, in addition to the gear_id and wearhouse_id columns, I have a qty (quantity) column.
Now I loaded all the wearhouses that store a certain gear item ($whs = $gear->wearhouses), and I want to get the quantity (qty field) from the pivot table.
Here is my code:
#foreach ($gear->wearhouses as $wh)
<tr>
<td>
{{ $wh->title }}
</td>
<td>
{{ $wh->manager->name }}
</td>
<td>
{{ $wh->pivot->qty }}
</td>
<td>
<a href="{{ url('/admin/organization/gear/edit/' . $wh->id) }}" class="btn btn-warning btn-xs">
<i class="fa fa-pencil-square-o"></i> עריכה
</a>
<a data-toggle="modal" href="#delete-{{ $wh->id }}" class="btn btn-danger btn-xs">
<i class="fa fa-trash-o"></i> מחיקה
</a>
</td>
</tr>
#endforeach
$wh->pivot->qty returns nothing.
How can I do that the easiest way?
$warehouse = Warehouse::find(1);
$gears = $warehouse->gears;
foreach($gears as $gear)
{
$qty = $gear->pivot->qty;
}
or
$warehouse = Warehouse::find(1);
$gear = $warehouse->gears()->first();
$qty = $gear->pivot->qty;

Categories