Laravel 此集合实例上不存在属性[parent_id]。拉维尔8

Laravel 此集合实例上不存在属性[parent_id]。拉维尔8,laravel,eloquent,eloquent-relationship,Laravel,Eloquent,Eloquent Relationship,我有一个子联系人,我想制作一个编辑页面来编辑与id相关的子联系人 联系人姓名的名称 我创建了编辑页面 但它告诉我 此集合实例上不存在属性[parent_id]。 问题是我想在编辑页面上显示子类别名称关系 在{{$sub->parent_id}中 有什么解决办法吗 这是我的桌子 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminat

我有一个子联系人,我想制作一个编辑页面来编辑与id相关的子联系人 联系人姓名的名称 我创建了编辑页面 但它告诉我 此集合实例上不存在属性[parent_id]。 问题是我想在编辑页面上显示子类别名称关系 在{{$sub->parent_id}中

有什么解决办法吗

这是我的桌子

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateContactcatsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contact__cats', function (Blueprint $table) {
            $table->id();
            $table->string('name_ar');
            $table->string('name_en');
            $table->unsignedBigInteger('parent_id')->nullable();
            $table->foreign('parent_id')->references('id')->on('contact__cats');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contact__cats');
    }
}
这是我的编辑页面

<div class="container">
    <div class="row">
        <div class="col-12">
            
              <!-- general form elements -->
              <div class="box box-primary">
                <div class="box-header with-border">
                  <h3 class="box-title">Edit The Specific Slider</h3>
                </div><!-- /.box-header -->
                <!-- form start -->
                <form role="form" action="{{route('contact.update' , $contact->id)}}" method="POST">
                  @csrf
                  @method('patch')
                  <div class="box-body">
                    
                    <div class="form-group">
                      <label for="exampleInputEmail1">edit arabic name</label>
                      <input type="text" class="form-control" name="name_ar" id="exampleInputEmail1" value="{{$contact->name_ar}}">
                    </div>
                    
                    <div class="form-group">
                      <label for="exampleInputEmail1">edit english name</label>
                      <input type="text" class="form-control" name="name_en" id="exampleInputEmail1" value="{{$contact->name_en}}">
                    </div>
                    
                    
                    <div class="form-group">
                      <label for="exampleInputFile">edit the sub Category Name</label><br>
                      <ul>
                          <li>{{$sub->parent_id}}</li>  
                      </ul>
                  </div><!-- /.box-body -->

                  <div class="box-footer">
                    <button type="submit" class="btn btn-primary">Submit</button>
                  </div>
                </form>
              </div><!-- /.box -->
        </div>
    </div>
</div>

编辑特定的滑块
@csrf
@方法('补丁')
编辑阿拉伯名称
编辑英文名称
编辑子类别名称
  • {{$sub->parent_id}
提交
这是我的Contact_Cats.php模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact_Cats extends Model
{
    use HasFactory;

    protected $fillable = ['name_ar','name_en','parent_id'];

    public function children(){ 
        return $this->hasMany(Contact_Cats::class , 'parent_id');
    }

    public function parents(){
        return $this->belongsTo(Contact_cats::class , 'id');
    }

        
}
使用
first()

->with('sub',Contact_Cats::find($id)->where('parent_id','!=',null)->get();

->with('sub',Contact_Cats::find($id)->where('parent_id','!=',null)->first();

然后你可以这样做
$sub->parent\u id

谢谢,这很有效,但是如果我想显示所有子类别(parent\u id)及其与姓名的关系,你能根据它更新你的问题吗,以便我能理解
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact_Cats extends Model
{
    use HasFactory;

    protected $fillable = ['name_ar','name_en','parent_id'];

    public function children(){ 
        return $this->hasMany(Contact_Cats::class , 'parent_id');
    }

    public function parents(){
        return $this->belongsTo(Contact_cats::class , 'id');
    }

        
}