douwei7203 2019-04-27 16:55
浏览 88
已采纳

Laravel - 访问关系数组

I try to access the relationship array but I am getting an error.

This this my Campaign Model :

class Campagne extends Model
{
    protected $table = 'Campagne';

    /**
     * The primary key for the model.
     * 
     * @var string
     */
    protected $primaryKey = 'Id';


    public $incrementing = false;


    public function Annonceur()
    {
        return $this->belongsTo('App\Annonceur', 'Annonceur');
    }

}

In my database i have the good foreign key and in my debugbar I have this object :

0 => Campagne {#574 ▼
  #table: "Campagne"
  #attributes: array:12 [▼
    "Id" => 7
    "Nom" => "ORANGE"
    "DateDebut" => null
    "DateFin" => null
    "Annonceur" => 25
    "Service" => 25
    "Description" => null
    "Active" => null
    "Visible" => 1
    "CommerceMaj" => null
    "created_at" => "2019-04-27 15:00:43"
    "updated_at" => "2019-04-27 15:00:43"
  ]
  #relations: array:1 [▼
    "Annonceur" => Annonceur {#610 ▼
      #table: "Annonceur"
      #primaryKey: "Id"
      #fillable: array:12 [▶]
      #connection: "mysql"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:17 [▶]
      #original: array:17 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
  ]

}

I have to access the "Annonceur" relation attribute but I have an error when I try :

$campagne->Annonceur->Name for exemple.

Thanks

  • 写回答

1条回答 默认 最新

  • douzhi9635 2019-04-27 17:01
    关注

    The relationship you have defined using

    return $this->belongsTo('App\Annonceur', 'Annonceur');

    is called Many to Many relationship.

    That means, you are telling to Laravel that your campagne has many Annonceur. as a result when you access $campagne->Annonceur it return a collection(for simplicity let's thing it is an array) of Annonceur instead of a single instance of Annonceur. So, either you can loop through all those Annonceur using a for loop as below:

    foreach($campagne->Annonceur as $annonceur){
      dd($annonceur->Name);
    }
    

    or to access only the first annonceur you can access as below:

    $campagne->Annonceur->first()->Name;

    If you change the relation to one to one means using

    $this->hasOne('App\Annonceur', 'Id', 'Annonceur');

    then you can access like: $campagne->Annonceur->Name; If your defined relationship is correct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥35 关于shodan搜索网络摄像头的各种方法详解
  • ¥15 求PHDA糖尿病并发症数据集,有偿
  • ¥15 为什么AVL fire DVI 界面里面的response Editor project 中的Summary result 点不了
  • ¥20 中标麒麟系统V4.0,linux3.10.0的内核,3A4000处理器,如何安装英伟达或AMD的显卡驱动,
  • ¥20 求文心中文心理分析系统(TextMind)
  • ¥15 chipyard环境搭建问题
  • ¥15 python,pyopenGL的OBV渲染无法设置透明