簡體   English   中英

嘗試讀取 null 上的屬性“名稱”

[英]Attempt to read property “name” on null

類別 Model

class Category extends Model
{
use HasFactory;

protected $fillable= ['name','number'];

 public function news(){

    return $this->hasMany(News::class);
}

新聞 Model

class News extends Model
{
use HasFactory;

protected $fillable= ['cat_id','title','photo','description','author_id','status'];

 public function category(){
        return $this->belongsTo(Category::class);
    }

  public function author(){
  return $this->belongsTo(Author::class);
    }

作者 Model

class Author extends Model
{
use HasFactory;

protected $fillable= ['name','status'];

public function news(){
    return $this->hasMany(News::class);
}

3個模型之間存在關系。 我想在news-list.blade.php中顯示類別名稱而不是 category_id 。 我收到這個錯誤。

這是我的 controller function

  public function news_index(){
    $news= News::with('category')->get();
    return view('News.list',compact('news'));
}

這是我的刀片頁面。 當我輸入$new->category->name而不是cat_id時出現錯誤。

@foreach($news as $new)
            <tr>
                <th scope="row">{{$loop->iteration}}</th>
                <td> {{$new->category->name}}</td>
                <td>  {{$new->title}}</td>
                <td> @if($new->photo)
                        <a href="{{url('storage/images/'.$new->photo)}}" target="_blank" class="btn btn-sm btn-secondary">Görüntüle</a></td>
                @endif
                </td>
                <td>  {{$new->author_id}}</td>
                <td>  {{$new->description}}</td>
                <td>  {{$new->status}}</td>

                <td>
                    <a class="btn btn-danger" onclick="return confirm('Silmek istediğinize emin 
       misiniz?')" href="{{route('news_delete', $new->id)}}"><i class="fa fa-trash"></i></a>
                    <a class="btn btn-primary" href="{{route('news_update',$new->id)}}"><i class="fa 
      fa-pen"></i></a>

                </td>
            </tr>
        @endforeach
 

這是我的新聞遷移表

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

您需要在定義類別關系時明確定義類別外鍵。 因為你的分類外鍵不是category_id; 它是 cat_id。

例如,您需要在 News model 中定義類別關系,如下所示:

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

我想你需要重寫你的 news_index() function。

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

我希望這會奏效。 您沒有為 $news 變量分配任何內容並將其傳遞給查看。 $new 是 null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM