【问题标题】:Building a menu retrieving data using model relationship使用模型关系构建菜单检索数据
【发布时间】:2014-04-26 00:35:27
【问题描述】:

我正在尝试使用模型关系构建菜单。菜单项是类别和产品。我想要一个类别列表,其中每一行类别都列出了他们自己的产品。恐怕我错过了整个逻辑。

我的第一个模型:

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

} 

'categorie' 表字段是:

('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>

我怎样才能用优雅的“雄辩的方式”来翻译这个?

【问题讨论】:

    标签: php laravel model relationship


    【解决方案1】:

    尝试替换:

    @foreach($prodotti as $prodotto)
    

    与:

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

    如果它有效,请告诉我。

    此外,由于您使用的是 Eloquent 关系,因此您只需要

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

    编辑:也尝试改成这个:

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

    【讨论】:

    • 不幸的是它不起作用。替换为 '@foreach($categorie->prodotti as $prodotto)' 错误是:Undefined property: Illuminate\Database\Eloquent\Collection::$prodotti (View: /var/www
    • @Asmodai-也没有工作,但我添加了一个编辑,看看是否有帮助,谢谢
    • 那是我要建议你的最后一次编辑,因为它是根据雄辩的关系解决的。似乎问题出在关系中的本地外键分配中。尝试我上次的编辑,看看它是否会有所作为。
    • 没有任何改变。添加了迁移以获取更多信息,可能会有所帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多