php - 定义与同一模型的关系(自连接)Laravel - 找不到类'App\Models\App\Models\Contact_cats' laravel 8
问题描述
我想创建一个联系我们页面,它有很多类别,所以我将它命名为contact_cats,它的类别包括子类别,所以我只想用一个表来制作它,所以我创建了一个名为 parent_id 的列
那么为什么这个错误
找不到类“App\Models\App\Models\Contact_cats”?我已经建立了关系
任何解决方案?
这是我的桌子
<?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');
}
}
这是我的 Contact_catsController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Contact_Cats;
use Illuminate\Http\Request;
class Contact_catsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.contact.index')->with('contacts' , Contact_Cats::all()->where('parent_id' , null))
->with('parents' , Contact_Cats::all()->where('parent_id' ,'!=',null));
}
这是我的 Contact_Cats 模型
<?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(App\Models\Contact_Cats::class , 'parent_id');
}
public function parents(){
return $this->belongsTo(App\Models\Contact_cats::class , 'id');
}
}
这是我的刀片文件,用于显示所有信息
<div class="container">
<div class="row">
<div class="col-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">contact with all informations</h3><br>
@if (Session::has('success'))
<div class="success alert-success">
<h3>{{Session::get('success')}}</h3>
</div>
@endif
<a href="{{route('contact.create')}}" class="btn btn-primary">ADD</a>
</div><!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>contact name</th>
<th>category names</th>
<th>options</th>
</tr>
</thead>
<tbody>
<?php $i = 0; ?>
@foreach ($contacts as $contact)
<tr>
<td>{{$i++}}</td>
<td>{{$contact->name_ar}}</td>
@foreach ($contact->parents as $item)
<td>{{$item->name_ar}}</td>
@endforeach
<td>
<a href="{{route('contact.edit',$contact->id)}}" class="btn btn-primary">EDIT</a>
<a href="{{route('contact.destroy',$contact->id)}}" class="btn btn-danger">DELETE</a>
</td>
</tr>
@endforeach
</table>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div>
</div>
</div>
解决方案
self
您可以使用此类自联接的关键字来引用模型本身。所以你的关系代码会是这样的。
public function children(){
return $this->hasMany(self::class , 'parent_id');
}
public function parents(){
return $this->belongsTo(self::class , 'id');
}
推荐阅读
- python-3.x - 将 pandas to_numeric 转换为“忽略”错误无法转换字符串
- flask - 如何动态地将值传递给授权值
- python - 如何从熊猫过滤的电影数据中绘制折线图
- jenkins - Jenkins groovy 错误 java.lang.IllegalArgumentException: Expected named arguments but got [{returnStatus=true} ...]
- c# - C# 对象引用未设置为对象的实例。(空引用异常)
- python - 转换火花数据框列
- c# - 为什么我不能让触摸输入(Unity with C#)工作?
- java - Java数组排序,定义索引后不会停止输入数字
- javascript - 如果承诺永远不会兑现,是否会产生负面影响?
- java - java - 如何在Java 11中获取JVM在运行时加载的jar的名称