尝试读取 null 上的属性“名称”

Emr*_*pcı 8 php laravel eloquent laravel-8

类别型号

\n
class Category extends Model\n{\nuse HasFactory;\n\nprotected $fillable= ['name','number'];\n\n public function news(){\n\n    return $this->hasMany(News::class);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

新闻模型

\n
class News extends Model\n{\nuse HasFactory;\n\nprotected $fillable= ['cat_id','title','photo','description','author_id','status'];\n\n public function category(){\n        return $this->belongsTo(Category::class);\n    }\n\n  public function author(){\n  return $this->belongsTo(Author::class);\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

作者模型

\n
class Author extends Model\n{\nuse HasFactory;\n\nprotected $fillable= ['name','status'];\n\npublic function news(){\n    return $this->hasMany(News::class);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

3个模型之间存在关系。我想在 中显示类别名称而不是category_id news-list.blade.php。我收到此错误。

\n

这是我的控制器功能

\n
  public function news_index(){\n    $news= News::with('category')->get();\n    return view('News.list',compact('news'));\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的刀片页面。$new->category->name当我输入而不是 时出现错误cat_id

\n
@foreach($news as $new)\n            <tr>\n                <th scope="row">{{$loop->iteration}}</th>\n                <td> {{$new->category->name}}</td>\n                <td>  {{$new->title}}</td>\n                <td> @if($new->photo)\n                        <a href="{{url('storage/images/'.$new->photo)}}" target="_blank" class="btn btn-sm btn-secondary">G\xc3\xb6r\xc3\xbcnt\xc3\xbcle</a></td>\n                @endif\n                </td>\n                <td>  {{$new->author_id}}</td>\n                <td>  {{$new->description}}</td>\n                <td>  {{$new->status}}</td>\n\n                <td>\n                    <a class="btn btn-danger" onclick="return confirm('Silmek istedi\xc4\x9finize emin \n       misiniz?')" href="{{route('news_delete', $new->id)}}"><i class="fa fa-trash"></i></a>\n                    <a class="btn btn-primary" href="{{route('news_update',$new->id)}}"><i class="fa \n      fa-pen"></i></a>\n\n                </td>\n            </tr>\n        @endforeach\n \n
Run Code Online (Sandbox Code Playgroud)\n

这是我的新闻迁移表

\n
public function up()\n    {\n        Schema::create('news', function (Blueprint $table) {\n            $table->id();\n            $table->unsignedBigInteger('cat_id');\n            $table->string('title');\n            $table->string('photo');\n            $table->longText('description');\n            $table->unsignedBigInteger('author_id');\n            $table->boolean('status')->default('1');\n$table->foreign('cat_id')->references('id')->on('categories')->onDelete('cascade');\n            $table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');\n            $table->timestamps();\n        });\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

mrd*_*dev 9

您需要在定义类别关系时显式定义类别外键。因为你的类别外键不是category_id;这是cat_id。

例如,您需要在新闻模型中定义类别关系,如下所示:

public function category()
{
    return $this->belongsTo(Category::class, 'cat_id');
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我遇到了同样的问题,我通过以下方式解决了它:

$new->category->name ?? None'
Run Code Online (Sandbox Code Playgroud)

  • 在 PHP 8.0 及更高版本中,您可以使用 Null 安全运算符 `$new-&gt;category?-&gt;name` (2认同)

xrk*_*lix 1

我想你需要重写 news_index() 函数。

public function news_index(){
    $news = News::with(array('category'=>function($query){
            $query->select('id','name');
        }))->get();
    return view('News.list',compact('news'));
}
Run Code Online (Sandbox Code Playgroud)

我希望这会起作用。您没有为 $news 变量分配任何内容并将其传递给视图。$new 为空。