title: "Trying to get property 'name' of non-object
body: <p>I had a Laravel script installed
  Now I want to go to the services list page, but with the "Trying to get the property 'name' of non-object" encountered.
  please help me</p>
  
  <p>My error:
  Trying to get property 'name' of non-object (View: /mylocalhost/core/resources/views/admin/service.blade.php)</p>
  
  <p>in line 36:
  category->name); ?></p>
  
  <p>service.blade.php code:</p>
  
  <pre><code>@section('page_name', 'Services')
      &lt;a class="btn btn-outline-success btn-lg" href="#" data-toggle="modal" data-target="#addService"&gt;&lt;i
                  class="fa fa-plus-circle"&gt;&lt;/i&gt; Add Service&lt;/a&gt;
  @endsection
  @section('body')
      &lt;ul class="orders-ul"&gt;
          &lt;li class="text-color"&gt;&lt;a class="btn btn-outline-secondary" href="{{ route('api.service') }}" target="_blank"&gt;API Services List&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;div class="row"&gt;
          @include('admin.layouts.flash')
          &lt;div class="col-md-12"&gt;
              &lt;div class="tile"&gt;
                  &lt;h3 class="tile-title"&gt;Services&lt;/h3&gt;
                  &lt;table class="table table-hover"&gt;
                      &lt;thead&gt;
                      &lt;tr&gt;
                          &lt;th&gt;Serial&lt;/th&gt;
                          &lt;th&gt;Name&lt;/th&gt;
                          &lt;th&gt;Category&lt;/th&gt;
                          &lt;th&gt;Price per 1K&lt;/th&gt;
                          &lt;th&gt;Min&lt;/th&gt;
                          &lt;th&gt;Max&lt;/th&gt;
                          &lt;th&gt;Order Response&lt;/th&gt;
                          &lt;th&gt;Status&lt;/th&gt;
                          &lt;th&gt;Action&lt;/th&gt;
                      &lt;/tr&gt;
                      &lt;/thead&gt;
                      &lt;tbody&gt;
                      @foreach($items as $key =&gt; $item)
                          &lt;tr&gt;
                              &lt;td&gt;{{ $key+1 }}&lt;/td&gt;
                              &lt;td&gt;{{ $item-&gt;name }}&lt;/td&gt;
                              &lt;td&gt;{{ $item-&gt;category-&gt;name }}&lt;/td&gt;
                              &lt;td&gt;{{ $item-&gt;price_per_k }}&lt;/td&gt;
                              &lt;td&gt;{{ $item-&gt;min }}&lt;/td&gt;
                              &lt;td&gt;{{ $item-&gt;max }}&lt;/td&gt;
                              &lt;td&gt;{{ $item-&gt;service_id == 0? 'Manual' : 'API' }}&lt;/td&gt;
                              &lt;td&gt;
                                  {!! $item-&gt;status == 0 ? '&lt;b style="color:#d35400"&gt;Inactive&lt;/b&gt;' : '&lt;b style="color:#16a085"&gt;Active&lt;/b&gt;' !!}
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;button class="btn btn-outline-info service_edit_btn"
                                          data-toggle="modal"
                                          data-target="#editService"
                                          data-route="{{ route('service.update', $item-&gt;id) }}"
                                          data-category="{{ $item-&gt;category_id }}"
                                          data-name="{{ $item-&gt;name }}"
                                          data-price="{{ $item-&gt;price_per_k }}"
                                          data-min="{{ $item-&gt;min }}"
                                          data-max="{{ $item-&gt;max }}"
                                          data-service_id="{{ $item-&gt;service_id }}"
                                          data-details="{{ $item-&gt;details }}"
                                          data-status="{{ $item-&gt;status }}"&gt;
                                      &lt;i class="fa fa-edit`enter code here`"&gt;&lt;/i&gt;&lt;/button&gt;
                                  &lt;button class="btn btn-outline-danger service_dlt_btn"
                                          data-toggle="modal"
                                          data-target="#delService"
                                          data-route="{{ route('service.delete', $item-&gt;id) }}"&gt;
                                      &lt;i class="fa fa-trash"&gt;&lt;/i&gt;&lt;/button&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      @endforeach
                      &lt;/tbody&gt;
                  &lt;/table&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="modal fade" id="addService" tabindex="-1" role="basic" aria-hidden="true"&gt;
          &lt;div class="modal-dialog modal-lg"&gt;
              &lt;div class="modal-content"&gt;
                  &lt;div class="modal-header"&gt;
                      &lt;h4&gt;&lt;b&gt;Add Service&lt;/b&gt;&lt;/h4&gt;
                  &lt;/div&gt;
                  &lt;div class="modal-body"&gt;
                      &lt;div class="portlet light bordered"&gt;
                          &lt;div class="portlet-body form"&gt;
                              &lt;form role="form" action="{{ route('service.store') }}" method="post"&gt;
                                  @csrf
                                  &lt;div class="form-body"&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Category&lt;/b&gt;&lt;/label&gt;
                                          &lt;select name="category_id" class="form-control form-control-lg"&gt;
                                              @foreach($categories as $category)
                                                  &lt;option value="{{ $category-&gt;id }}"&gt;{{ $category-&gt;name }}&lt;/option&gt;
                                                  @endforeach
                                          &lt;/select&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/label&gt;
                                          &lt;input type="text" name="name" class="form-control form-control-lg"&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Price per 1k&lt;/b&gt;&lt;/label&gt;
                                          &lt;div class="input-group"&gt;
                                          &lt;input type="text" name="price_per_k" class="form-control form-control-lg"&gt;
                                          &lt;div class="input-group-append"&gt;
                                              &lt;span class="input-group-text"&gt;{{ $general-&gt;currency_symbol }}&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class="row"&gt;
                                          &lt;div class="form-group col-md-6"&gt;
                                              &lt;label for=""&gt;&lt;b&gt;Min&lt;/b&gt;&lt;/label&gt;
                                              &lt;input type="text" name="min" class="form-control form-control-lg"&gt;
                                          &lt;/div&gt;
                                          &lt;div class="form-group col-md-6"&gt;
                                              &lt;label for=""&gt;&lt;b&gt;Max&lt;/b&gt;&lt;/label&gt;
                                              &lt;input type="text" name="max" class="form-control form-control-lg"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Details&lt;/b&gt;&lt;/label&gt;
                                          &lt;textarea class="form-control" name="details" rows="8"&gt;&lt;/textarea&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;service Id (If order process through API)&lt;/b&gt;&lt;/label&gt;
                                          &lt;input type="text" name="service_id" class="form-control form-control-lg"&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Status&lt;/b&gt;&lt;/label&gt;
                                          &lt;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"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;div class="tile-footer"&gt;
                                      &lt;button class="btn btn-primary btn-block btn-lg" type="submit"&gt;&lt;i
                                                  class="fa fa-fw fa-lg fa-check-circle"&gt;&lt;/i&gt;Save
                                      &lt;/button&gt;
                                  &lt;/div&gt;
                              &lt;/form&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="modal-footer"&gt;
                      &lt;button type="button" class="btn btn-danger" data-dismiss="modal"&gt;Cancel&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;!-- /.modal-content --&gt;
          &lt;/div&gt;
          &lt;!-- /.modal-dialog --&gt;
      &lt;/div&gt;
      &lt;div class="modal fade" id="editService" tabindex="-1" role="basic" aria-hidden="true"&gt;
          &lt;div class="modal-dialog modal-lg"&gt;
              &lt;div class="modal-content"&gt;
                  &lt;div class="modal-header"&gt;
                      &lt;h4&gt;&lt;b&gt;Edit Service&lt;/b&gt;&lt;/h4&gt;
                  &lt;/div&gt;
                  &lt;div class="modal-body"&gt;
                      &lt;div class="portlet light bordered"&gt;
                          &lt;div class="portlet-body form"&gt;
                              &lt;form role="form" action="" method="post"
                                    id="serviceEditdForm"&gt;
                                  @csrf
                                  @method('put')
                                  &lt;div class="form-body"&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Category&lt;/b&gt;&lt;/label&gt;
                                          &lt;select name="category_id" id="category_id" class="form-control form-control-lg"&gt;
                                              @foreach($categories as $category)
                                                  &lt;option value="{{ $category-&gt;id }}"&gt;{{ $category-&gt;name }}&lt;/option&gt;
                                              @endforeach
                                          &lt;/select&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/label&gt;
                                          &lt;input type="text" name="name" id="name" class="form-control form-control-lg"&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Price per 1k&lt;/b&gt;&lt;/label&gt;
                                          &lt;div class="input-group"&gt;
                                              &lt;input type="text" name="price_per_k" id="price_per_k" class="form-control form-control-lg"&gt;
                                              &lt;div class="input-group-append"&gt;
                                                  &lt;span class="input-group-text"&gt;{{ $general-&gt;currency_symbol }}&lt;/span&gt;
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class="row"&gt;
                                          &lt;div class="form-group col-md-6"&gt;
                                              &lt;label for=""&gt;&lt;b&gt;Min&lt;/b&gt;&lt;/label&gt;
                                              &lt;input type="text" name="min" id="min" class="form-control form-control-lg"&gt;
                                          &lt;/div&gt;
                                          &lt;div class="form-group col-md-6"&gt;
                                              &lt;label for=""&gt;&lt;b&gt;Max&lt;/b&gt;&lt;/label&gt;
                                              &lt;input type="text" name="max" id="max" class="form-control form-control-lg"&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Details&lt;/b&gt;&lt;/label&gt;
                                          &lt;textarea class="form-control" name="details" id="details" rows="8"&gt;&lt;/textarea&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;service Id (If order process through API)&lt;/b&gt;&lt;/label&gt;
                                          &lt;input type="text" name="service_id" id="service_id" class="form-control form-control-lg"&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label for=""&gt;&lt;b&gt;Status&lt;/b&gt;&lt;/label&gt;
                                          &lt;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"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;div class="tile-footer"&gt;
                                      &lt;button class="btn btn-primary btn-block btn-lg" type="submit"&gt;&lt;i
                                                  class="fa fa-fw fa-lg fa-check-circle"&gt;&lt;/i&gt;Save
                                      &lt;/button&gt;
                                  &lt;/div&gt;
                              &lt;/form&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="modal-footer"&gt;
                      &lt;button type="button" class="btn btn-danger" data-dismiss="modal"&gt;Cancel&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;!-- /.modal-content --&gt;
          &lt;/div&gt;
          &lt;!-- /.modal-dialog --&gt;
      &lt;/div&gt;
      &lt;div class="modal fade" id="delService"&gt;
          &lt;div class="modal-dialog modal-dialog-centered"&gt;
              &lt;div class="modal-content"&gt;
                  &lt;div class="modal-body"&gt;
                      &lt;form action="" method="post" id="serviceDelForm"&gt;
                          @csrf
                          @method('delete')
                          &lt;h3 class="d-flex justify-content-center"&gt;Are you want to delete this Service?&lt;/h3&gt;
                          &lt;div class="tile-footer d-flex justify-content-center"&gt;
                              &lt;button class="btn btn-primary" type="submit"&gt;&lt;i class="fa fa-fw fa-lg fa-check-circle"&gt;&lt;/i&gt;Confirm
                              &lt;/button&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                              &lt;a class="btn btn-danger" href="#" data-dismiss="modal"&gt;&lt;i
                                          class="fa fa-fw fa-lg fa-times-circle"&gt;&lt;/i&gt;Cancel&lt;/a&gt;
                          &lt;/div&gt;
                      &lt;/form&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  @endsection
  @section('scripts')
      &lt;script type="text/javascript"&gt;
          $(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);
              });
          });
      &lt;/script&gt;
      @endsection
  </code></pre>
  
  <p>And service list controller in Admin controller:</p>
  
  <pre><code>public function service(){
      $categories = Category::orderBy('name')-&gt;get();
      $items = Service::orderBy('category_id')-&gt;paginate(10);
      return view('admin.service', compact('items', 'categories'));
  }
  
  public function serviceStore(Request $request){
      $this-&gt;validate($request, [
          'category_id' =&gt; 'required',
          'name' =&gt; 'required|unique:services,name',
          'price_per_k' =&gt; 'required',
          'min' =&gt; 'required',
          'max' =&gt; 'required',
          'details' =&gt; 'required',
      ], [
          'category_id.required' =&gt; 'Category name is required',
          'price_per_k.required' =&gt; 'price per 1k is required',
      ]);
      $excp = $request-&gt;except('_token', 'status', 'service_id');
      $status = $request-&gt;status == 1? 1:0;
      if(isset($request-&gt;service_id)){
          $service_id = $request-&gt;service_id;
      }else{
          $service_id = 0;
      }
      $sev = Service::create($excp + ['status' =&gt; $status, 'service_id' =&gt; $service_id]);
      $users = User::all();
      foreach ($users as $user){
          $servicePrice = new ServicePrice();
          $servicePrice-&gt;category_id = $sev-&gt;category_id;
          $servicePrice-&gt;service_id = $sev-&gt;id;
          $servicePrice-&gt;user_id = $user-&gt;id;
          $servicePrice-&gt;price = $sev-&gt;price_per_k;
          $servicePrice-&gt;save();
      }
      session()-&gt;flash('success', 'Service Stored successfully');
      return back();
  }
  
  public function serviceUpdate(Request $request, $id){
      $this-&gt;validate($request, [
          'category_id' =&gt; 'required',
          'name' =&gt; 'required',
          'price_per_k' =&gt; 'required',
          'min' =&gt; 'required',
          'max' =&gt; 'required',
          'details' =&gt; 'required',
      ], [
          'category_id.required' =&gt; 'Category name is required',
          'price_per_k.required' =&gt; 'price per 1k is required',
      ]);
      $excp = $request-&gt;except('_token', 'status', 'service_id');
      $status = $request-&gt;status == 1? 1:0;
      if(isset($request-&gt;service_id)){
          $service_id = $request-&gt;service_id;
      }else{
          $service_id = 0;
      }
      Service::findOrFail($id)-&gt;update($excp + ['status' =&gt; $status, 'service_id' =&gt; $service_id]);
      session()-&gt;flash('success', 'Service Updated successfully');
      return back();
  }
  
  public function serviceDelete($id){
     Service::findOrFail($id)-&gt;delete();
      session()-&gt;flash('success', 'Service deleted successfully');
      return back();
  }
  </code></pre>