【发布时间】:2023-03-03 00:51:01
【问题描述】:
我有一个子联系人,我想创建一个编辑页面来编辑与 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');
}
}
这是我的 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());
}
这是我的编辑页面
<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>
这是我的 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');
}
}
【问题讨论】:
标签: laravel eloquent eloquent-relationship