mardi 15 novembre 2016

set relations in evailable full table in laravel

i create an migration as below :

  public function up()
{
    //
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('parent')->nullable()->unsigned();;

        $table->string('category_name');
        $table->text('category_lable');
        $table->timestamps();
});
    Schema::table('categories', function (Blueprint $table) {

        $table->foreign('parent')
            ->references('id')->on('categories')
            ->onDelete('cascade')
            ->onUpdate('cascade');
    }) ;
}

and that migration is seeded by this way :

   Category::create([
        'id' => 1,
        'parent' => null,
        'category_name' => "root"
    ]);

    Category::create([
            'id' => 2,
            'parent' => 1,
            'category_name' => "something"
         ]);

    Category::create([
        'id' => 3,
        'parent' => 2,
            'category_name' => "something"
            ]);
    Category::create([
        'id' => 4,
        'parent' => 2,
            'category_name' => "something"
            ]);

    and etc ...

also this is my model that is self Referenced :

class Category extends Model
{
public function getchildren() {
    return $this->has_many('App\Category' , 'parent');
}
public function getparent()   {
    return $this->belongs_to('App\Category' , 'parent');
}

}

in the tinker when i try to get some node parent or children , an error accured.

error :

BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::getparent()'

or

BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::getchildren()'

where is my wrong ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire