简体   繁体   English

多对多关系“此集合实例上不存在属性[moedas]”

[英]Relation Many to Many 'Property [moedas] does not exist on this collection instance '

I would like to get moeda (coin in portuguese) data from user object. 我想从user对象中获取moeda (葡萄牙语的硬币)数据。 But I'm getting an error saying that the property moedas does not exist. 但是我收到一个错误消息,说属性moedas不存在。 I have already created all the requirements pivot table, models,controllers and view. 我已经创建了所有需求透视表,模型,控制器和视图。

My models: 我的模特:

Moeda 前田

namespace leilao;

use Illuminate\Database\Eloquent\Model;

class Moeda extends Model
{
   public function users(){
         return $this->belongsToMany('leilao\User','moeda_user');
   }
}

User 用户

<?php

namespace leilao;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use leilao\Moeda;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
    public function moedas(){
        return $this->belongsToMany('leilao\Moeda','moeda_user');
    }
}

My Controller 我的控制器

<?php

namespace leilao\Http\Controllers;

use Illuminate\Http\Request;
use leilao\User;
use leilao\Moeda;
class UsuarioController extends Controller
{
 $userCollection=User::with('moedas')->get();  
public function lista(){

    return view('lista')->with('users', $userCollection);
}

}

My View 我的观点

<table class="table table-striped table-bordered table-hover">
<?php foreach ($users->moedas as $c): ?>
<tr>
   td><?= $c->currencia ?></td>
   <td><?= $c->valor ?></td>

</tr>
<?php endforeach ?>
</table>

Pivot Table 数据透视表

The migration up method was done as follow: 迁移方法如下:

public function up()
{
      Schema::create('moeda_user', function(Blueprint $table)
{
  $table->increments('id');
  $table->integer('moeda_id')->unsigned()->nullable(); //unsingned não permite valores negativos
  $table->foreign('moeda_id')->references('id')
        ->on('moedas')->onDelete('cascade');

  $table->integer('user_id')->unsigned()->nullable();
  $table->foreign('user_id')->references('id')
        ->on('users')->onDelete('cascade');

  $table->timestamps();
});    }

You've got mismatched capitalization between your class name and the relationship's first parameter: 您的类名和关系的第一个参数之间的大小写不匹配:

// uppercase leilao\Moeda
public function moedas(){
    return $this->belongsToMany('leilao\Moeda','moeda_user');
}

// lowercase
class moeda extends Model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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