构建使用模型关系检索数据的菜单


Building a menu retrieving data using model relationship

我正在尝试使用模型关系构建菜单。菜单项是类别和产品。我想有一个类别的清单,每一行的类别列出自己的产品。恐怕我没有理解整个逻辑。

我的第一个模型:
class Categoria extends Eloquent {
    protected $table = 'categorie';
    public function prodotti()
    {
        return $this->hasMany('Prodotto','categoria_id');
    }
}

第二个模型:

class Prodotto extends Eloquent {
    protected $table = 'prodotti';
        public function categoria()
        {
            return $this->belongsTo('Categoria', 'categoria_id');
        }
} 

' category '表字段如下:

('id', 'nome', 'descrizione'...)

'prodotti'表字段:

('id', 'categoria_id', 'codice'...)

我的控制器是:

class SiteController extends BaseController {

    public function menuHome()
    {
        $categorie = Categoria::where('attivo', '=', '1')->orderBy('nome','asc')->get();
        $prodotti = Categoria::find(1)->prodotti()->get();
        return View::make('index')
        ->with('categorie',$categorie)
        ->with('prodotti',$prodotti);
    }
}

迁移:

1)

class CreateCategorie extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categorie', function($table)
        {
            $table->increments('id')->unsigned();
            $table->string('nome', 255);
            $table->text('descrizione')->nullable();
            $table->boolean('attivo')->default(false);
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('categorie');
    }
}

2)

class CreateProdotti extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('prodotti', function($table)
        {
            $table->increments('id')->unsigned();
            $table->integer('categoria_id')->unsigned();
            $table->foreign('categoria_id')->references('id')->on('categorie');
            $table->string('codice', 255)->nullable();
            $table->string('produttore', 255);
            $table->string('modello', 255);
            $table->text('descrizione')->nullable();
            $table->string('tipo', 255)->nullable();
            $table->string('lungmax', 255)->nullable();
            $table->string('peso', 255)->nullable();
            $table->string('certificazioni', 255)->nullable();
            $table->string('prove', 255)->nullable();
            $table->boolean('venduto')->default(false);
            $table->boolean('attivo')->default(true);
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('prodotti');
    }
}

我正试图从这个视图中构建整体:

<!-- TEST -->
 <div class="well">
  @foreach($categorie as $categoria)
  <ul>
    <li>{{ $categoria->nome }}</li>
      <ul>
       @foreach($prodotti as $prodotto)
        <li>{{ $prodotto->codice }}</li>
       @endforeach
      </ul>
    </ul>
  @endforeach
 </div>
<!-- /TEST -->

这显然行不通。

我这样改变了视图,它工作得很好:

<div class="well">
  @foreach($categorie as $categoria)
    <ul>
      <li>{{ $categoria->nome }}</li>
      <ul>
      @foreach(Prodotto::where('categoria_id', '=', $categoria->id)->get() as $prodotto)
        <li><a href="#">{{ $prodotto->codice }}</a></li>
      @endforeach
      </ul>
    </ul>
  @endforeach
</div>

我如何用优雅的"雄辩的方式"翻译这句话?

尝试替换:

@foreach($prodotti as $prodotto)

With:

@foreach($categorie->prodotti as $prodotto)

让我知道它是否有效。

由于你使用的是雄辩关系,你只需要

return View::make('index')
    ->with('categorie',$categorie); 

而不触及Prodotti模型:

 public function menuHome()
{
    $categorie = Categoria::where('attivo', '=', '1')->orderBy('nome','asc')->get();
    return View::make('index')
    ->with('categorie',$categorie);
}

EDIT:试着也改成这个:

       public function prodotti()
{
    return $this->hasMany('Prodotto', 'categoria_id', 'id');
}
 public function categoria()
    {
        return $this->belongsTo('Categoria', 'categoria_id','id');
    }