【问题标题】:Laravel error : Trying to get property of non-object in show.blade.phpLaravel 错误:试图在 show.blade.php 中获取非对象的属性
【发布时间】:2017-12-15 14:37:04
【问题描述】:

我的代码在这里,如果你想要任何其他部分的代码,我也会提供那段代码

 <div class="panel-heading ">Organization Profile</div>
  <table class="table table-striped">
    <thead>
     <tr>
      <th>Attributes</th>
      <th>Values</th>
     </tr>
    </thead>
    <tbody>
     <tr>
      <td>Org Name</td>
      <td>{{$org->name}}</td>
     </tr>
     <tr>
      <td>Owner Name</td>
      <td>{{$org->owner}}</td>
     </tr>
   </tbody>
  </table>
 </div>

【问题讨论】:

  • 我是初学者,请高手指点!
  • 控制器中的代码是什么?
  • 显然 $org 不是你想要的
  • $org 为空,找出原因
  • 只有控制器才能知道

标签: php laravel-5.3


【解决方案1】:

Trying to get property of non-object 问题通常出现在 Laravel 中,当您返回数组但尝试在刀片文件中使用对象或根本不传递正确的对象字符串时。

我从 cmets 注意到您在函数中传递的是 $orgs,而不是 $org

public function create() { 
    return view('Admin.organizations.create',compact('orgs')); 
}

更新您的刀片文件,如下所示:

<div class="panel-heading ">Organization Profile</div>
<table class="table table-striped">
<thead>
 <tr>
  <th>Attributes</th>
  <th>Values</th>
 </tr>
</thead>
<tbody>
 <tr>
  <td>Org Name</td>
  <td>{{$orgs->name}}</td>
 </tr>
 <tr>
  <td>Owner Name</td>
  <td>{{$orgs->owner}}</td>
 </tr>

【讨论】:

    【解决方案2】:

    考虑到所有的 cmets,您的控制器中可能应该有这样的东西:

    public function show($id)
    {
     $org = Organization::find($id);
     // $data = ['org', $id];
     return view('Admin.organizations.show')->with(compact('org'));
    }
    

    这是你的刀片视图:

    <div class="panel-heading ">Organization Profile</div>
     @if (empty($org))
        <h1>Organization is empty</h1>
     @else 
        <table class="table table-striped">
           <thead>
            <tr>
             <th>Attributes</th>
             <th>Values</th>
            </tr>
           </thead>
           <tbody>
            <tr>
             <td>Org Name</td>
             <td>{{$org->name}}</td>
            </tr>
            <tr>
             <td>Owner Name</td>
             <td>{{$org->owner}}</td>
            </tr>
          </tbody>
         </table>
     @endif
    </div>
    

    当然,不知道组织模型中的内容会使我们无法知道所有者和名称是否作为模型的属性存在。

    【讨论】:

      猜你喜欢
      • 2018-08-03
      • 1970-01-01
      • 2016-06-13
      • 2018-06-02
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多