简体   繁体   中英

One-To-Many Eloquent relation isn't working with irregular words

I've got categories->subcategories one to many relation (one category can have many subcategories) but I can't get this to work, I've tried basically everything and don't know what to do next.

My migrations and seeds:

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()
        ],
        ]);

Models:

Category:

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

Subcategory:

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

And in controller:

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

Route:

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

So it's on url website.test/category/1

but it returns me nothing. If I just print name of category (so just work with category without its subcategories relations) it works. Just relations don't. I really have no idea already what can I do. Do you have any ideas?

Regards :)

Got answer, things to change were in controller:

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

And loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM