首页 > 解决方案 > 定义与同一模型的关系(自连接)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>

标签: phplaravelbackend

解决方案


self您可以使用此类自联接的关键字来引用模型本身。所以你的关系代码会是这样的。

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

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

推荐阅读