Property [parent_id] does not exist on this collection instance. Laravel 8

314 views Asked by At

i have a sub contact and i want to make an edit page to edit the sub contacts that related to the id of the contact name i created the edit page but it tells me Property [parent_id] does not exist on this collection instance. the problem is i want to show on the edit page , the sub category names relations in {{$sub->parent_id}}

ANY SOLUTIONS??

and here is my table

<?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');
    }
}

here is my Sub_atController.php

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    return view('admin.contact.edit')
    ->with('categories' , Contact_Cats::all()->where('parent_id' , null))
    ->with('contact' , Contact_Cats::find($id))
    ->with('sub' , Contact_Cats::find($id)->where('parent_id' , '!=' , null)->get());
}

and here is my edit page

<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>

and here is my Contact_Cats.php Model

<?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');
    }

        
}
1

There are 1 answers

2
Kamlesh Paul On

use first()

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

to

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

Then you can do $sub->parent_id like this