我安装了Laravel脚本 现在,我想转到服务列表页面,但是遇到了“尝试获取非对象的属性'名称'”。 请帮助我
我的错误: 试图获取非对象的属性“名称”(查看:/mylocalhost/core/resources/views/admin/service.blade.php)
在第36行中的: 类别->名称); ?>
service.blade.php代码:
@section('page_name', 'Services')
<a class="btn btn-outline-success btn-lg" href="#" data-toggle="modal" data-target="#addService"><i
class="fa fa-plus-circle"></i> Add Service</a>
@endsection
@section('body')
<ul class="orders-ul">
<li class="text-color"><a class="btn btn-outline-secondary" href="{{ route('api.service') }}" target="_blank">API Services List</a></li>
</ul>
<div class="row">
@include('admin.layouts.flash')
<div class="col-md-12">
<div class="tile">
<h3 class="tile-title">Services</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Name</th>
<th>Category</th>
<th>Price per 1K</th>
<th>Min</th>
<th>Max</th>
<th>Order Response</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($items as $key => $item)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->category->name }}</td>
<td>{{ $item->price_per_k }}</td>
<td>{{ $item->min }}</td>
<td>{{ $item->max }}</td>
<td>{{ $item->service_id == 0? 'Manual' : 'API' }}</td>
<td>
{!! $item->status == 0 ? '<b style="color:#d35400">Inactive</b>' : '<b style="color:#16a085">Active</b>' !!}
</td>
<td>
<button class="btn btn-outline-info service_edit_btn"
data-toggle="modal"
data-target="#editService"
data-route="{{ route('service.update', $item->id) }}"
data-category="{{ $item->category_id }}"
data-name="{{ $item->name }}"
data-price="{{ $item->price_per_k }}"
data-min="{{ $item->min }}"
data-max="{{ $item->max }}"
data-service_id="{{ $item->service_id }}"
data-details="{{ $item->details }}"
data-status="{{ $item->status }}">
<i class="fa fa-edit`enter code here`"></i></button>
<button class="btn btn-outline-danger service_dlt_btn"
data-toggle="modal"
data-target="#delService"
data-route="{{ route('service.delete', $item->id) }}">
<i class="fa fa-trash"></i></button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
<div class="modal fade" id="addService" tabindex="-1" role="basic" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4><b>Add Service</b></h4>
</div>
<div class="modal-body">
<div class="portlet light bordered">
<div class="portlet-body form">
<form role="form" action="{{ route('service.store') }}" method="post">
@csrf
<div class="form-body">
<div class="form-group">
<label for=""><b>Category</b></label>
<select name="category_id" class="form-control form-control-lg">
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for=""><b>Name</b></label>
<input type="text" name="name" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Price per 1k</b></label>
<div class="input-group">
<input type="text" name="price_per_k" class="form-control form-control-lg">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for=""><b>Min</b></label>
<input type="text" name="min" class="form-control form-control-lg">
</div>
<div class="form-group col-md-6">
<label for=""><b>Max</b></label>
<input type="text" name="max" class="form-control form-control-lg">
</div>
</div>
<div class="form-group">
<label for=""><b>Details</b></label>
<textarea class="form-control" name="details" rows="8"></textarea>
</div>
<div class="form-group">
<label for=""><b>service Id (If order process through API)</b></label>
<input type="text" name="service_id" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Status</b></label>
<input data-toggle="toggle" data-onstyle="success" data-offstyle="danger"
data-on="Active" data-off="Inactive" data-width="100%" type="checkbox" name="status" value="1">
</div>
</div>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<div class="modal fade" id="editService" tabindex="-1" role="basic" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4><b>Edit Service</b></h4>
</div>
<div class="modal-body">
<div class="portlet light bordered">
<div class="portlet-body form">
<form role="form" action="" method="post"
id="serviceEditdForm">
@csrf
@method('put')
<div class="form-body">
<div class="form-group">
<label for=""><b>Category</b></label>
<select name="category_id" id="category_id" class="form-control form-control-lg">
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for=""><b>Name</b></label>
<input type="text" name="name" id="name" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Price per 1k</b></label>
<div class="input-group">
<input type="text" name="price_per_k" id="price_per_k" class="form-control form-control-lg">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for=""><b>Min</b></label>
<input type="text" name="min" id="min" class="form-control form-control-lg">
</div>
<div class="form-group col-md-6">
<label for=""><b>Max</b></label>
<input type="text" name="max" id="max" class="form-control form-control-lg">
</div>
</div>
<div class="form-group">
<label for=""><b>Details</b></label>
<textarea class="form-control" name="details" id="details" rows="8"></textarea>
</div>
<div class="form-group">
<label for=""><b>service Id (If order process through API)</b></label>
<input type="text" name="service_id" id="service_id" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Status</b></label>
<input data-toggle="toggle" data-onstyle="success" data-offstyle="danger"
data-on="Active" data-off="Inactive" data-width="100%" type="checkbox" name="status" id="status" value="1">
</div>
</div>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<div class="modal fade" id="delService">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
<form action="" method="post" id="serviceDelForm">
@csrf
@method('delete')
<h3 class="d-flex justify-content-center">Are you want to delete this Service?</h3>
<div class="tile-footer d-flex justify-content-center">
<button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-lg fa-check-circle"></i>Confirm
</button>
<a class="btn btn-danger" href="#" data-dismiss="modal"><i
class="fa fa-fw fa-lg fa-times-circle"></i>Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script type="text/javascript">
$(document).ready(function () {
$(document).on('click', '.service_edit_btn', function () {
var route = $(this).data('route');
var category = $(this).data('category');
var name = $(this).data('name');
var price = $(this).data('price');
var min = $(this).data('min');
var max = $(this).data('max');
var service_id = $(this).data('service_id');
var details = $(this).data('details');
var status = $(this).data('status');
$('#category_id').val(category).attr('selected', 'selected');
$('#name').val(name);
$('#price_per_k').val(price);
$('#min').val(min);
$('#max').val(max);
$('#service_id').val(service_id);
$('#details').val(details);
$('#serviceEditdForm').attr('action',route);
if(status == 1){
$('#status').bootstrapToggle('on');
}else{
$('#status').bootstrapToggle('off');
}
});
$(document).on('click', '.service_dlt_btn', function () {
var route = $(this).data('route');
$('#serviceDelForm').attr('action', route);
});
});
</script>
@endsection
以及管理控制器中的服务列表控制器:
public function service(){
$categories = Category::orderBy('name')->get();
$items = Service::orderBy('category_id')->paginate(10);
return view('admin.service', compact('items', 'categories'));
}
public function serviceStore(Request $request){
$this->validate($request, [
'category_id' => 'required',
'name' => 'required|unique:services,name',
'price_per_k' => 'required',
'min' => 'required',
'max' => 'required',
'details' => 'required',
], [
'category_id.required' => 'Category name is required',
'price_per_k.required' => 'price per 1k is required',
]);
$excp = $request->except('_token', 'status', 'service_id');
$status = $request->status == 1? 1:0;
if(isset($request->service_id)){
$service_id = $request->service_id;
}else{
$service_id = 0;
}
$sev = Service::create($excp + ['status' => $status, 'service_id' => $service_id]);
$users = User::all();
foreach ($users as $user){
$servicePrice = new ServicePrice();
$servicePrice->category_id = $sev->category_id;
$servicePrice->service_id = $sev->id;
$servicePrice->user_id = $user->id;
$servicePrice->price = $sev->price_per_k;
$servicePrice->save();
}
session()->flash('success', 'Service Stored successfully');
return back();
}
public function serviceUpdate(Request $request, $id){
$this->validate($request, [
'category_id' => 'required',
'name' => 'required',
'price_per_k' => 'required',
'min' => 'required',
'max' => 'required',
'details' => 'required',
], [
'category_id.required' => 'Category name is required',
'price_per_k.required' => 'price per 1k is required',
]);
$excp = $request->except('_token', 'status', 'service_id');
$status = $request->status == 1? 1:0;
if(isset($request->service_id)){
$service_id = $request->service_id;
}else{
$service_id = 0;
}
Service::findOrFail($id)->update($excp + ['status' => $status, 'service_id' => $service_id]);
session()->flash('success', 'Service Updated successfully');
return back();
}
public function serviceDelete($id){
Service::findOrFail($id)->delete();
session()->flash('success', 'Service deleted successfully');
return back();
}
答案 0 :(得分:2)
这意味着您的商品没有类别,因此您可以执行以下操作。
<td>
@if(!empty($item->category)) {{ $item->category->name }} @endif
</td>
答案 1 :(得分:0)
使用php7解决此问题的另一种方法是使用null coalescing operator,其中:
如果存在并且不为NULL,则返回其第一个操作数;否则,它将返回其第二个操作数。
<td>{{ $item->category->name ?? '' }}</td>