一对多的雄辩关系不适用于不规则的词汇

时间:2018-04-11 20:27:09

标签: php laravel laravel-5 eloquent laravel-5.6

我有类别 - >子类别一对多关系(一个类别可以有很多子类别)但是我不能让它工作,我已经尝试了所有的东西,不知道接下来该做什么。

我的迁移和种子:

Categories:


Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->string('image_url');
            $table->timestamps();
        });


---------

DB::table('categories')->insert([[
            'id' => 1,
            'name' => 'Verbs',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => ''
        ],[
            'id' => 2,
            'name' => 'Nouns',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => ''
        ],[
            'id' => 3,
            'name' => 'Idioms',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => ''
        ],[
            'id' => 4,
            'name' => 'Adjectives',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => ''
        ]
        ]);


Subcategories:

Schema::create('subcategories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->string('image_url');
            $table->integer('category_id')->unsigned()->index;
            $table->timestamps();
        });

---------

DB::table('subcategories')->insert([[
            'id' => 1,
            'name' => 'Sport verbs',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => '',
            'category_id' => DB::table('categories')->where('name', 'Verbs')->pluck('id')->first()
        ],[
            'id' => 2,
            'name' => 'Cooking verbs',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => '',
            'category_id' => DB::table('categories')->where('name', 'Verbs')->pluck('id')->first()
        ],[
            'id' => 3,
            'name' => 'Business verbs',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => '',
            'category_id' => DB::table('categories')->where('name', 'Verbs')->pluck('id')->first()
        ],[
            'id' => 4,
            'name' => 'School verbs',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => '',
            'category_id' => DB::table('categories')->where('name', 'Verbs')->pluck('id')->first()
        ],[
            'id' => 5,
            'name' => 'Sport nouns',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => '',
            'category_id' => DB::table('categories')->where('name', 'Nouns')->pluck('id')->first()
        ],[
            'id' => 6,
            'name' => 'Sport idioms',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => '',
            'category_id' => DB::table('categories')->where('name', 'Idioms')->pluck('id')->first()
        ],[
            'id' => 7,
            'name' => 'Sport adjectives',
            'description' => 'Lorem ipsum dolor sit amet',
            'image_url' => '',
            'category_id' => DB::table('categories')->where('name', 'Adjectives')->pluck('id')->first()
        ],
        ]);

型号:

分类

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Subcategory;

class Category extends Model
{
    protected $table = 'categories';
    protected $fillable = ['name', 'description', 'image_url', 'id'];

    public function subcategories()
    {
        return $this->hasMany(Subcategory::class, 'category_id');
    }
}

子类别:

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Category;

class Subcategory extends Model
{
    protected $table = 'subcategories';
    protected $fillable = ['name', 'description', 'image_url', 'category_id', 'id'];

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }
}

在控制器中:

class CategoriesController extends Controller
{
    public function index($id)
    {
        $subcategories = Category::findOrFail($id)->subcategories();
        foreach ($subcategories as $subcategory)
            echo $subcategory->name . '<br>';
    }
}

路线:

Route::get('/category/{id}', 'CategoriesController@index');

所以它在网址website.test/category/1

但它没有给我任何回报。如果我只打印类别的名称(所以只使用没有子类别关系的类别)它可以工作。只是关系没有。我真的不知道我能做些什么。你有什么想法吗?

问候:)

1 个答案:

答案 0 :(得分:-1)

得到答案,要改变的事情在控制器中:

$subcategories = Category::with('subcategories')->findOrFail($id);

循环:

echo "<div>Category: {{$category->name}}</div>";
echo "<div>Subcategories:</div>";
foreach ($category->subcategories as $subcategory)
            echo $subcategory->name . '<br>';