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 R语言爬虫的时候元素和园代码不一样怎么解决呀
  • ¥15 VS2022多项目启动有问题
  • ¥15 SQL删除添加数据后序号不连续问题。
  • ¥15 首次运行OmniEvent运行报错
  • ¥15 有没有人知道这个问题怎么解决
  • ¥15 comsol电力电缆载流量仿真
  • ¥15 webSocket可以接TCP socket接口吗
  • ¥60 mpi并行出错,CFD++计算
  • ¥15 c#:vsto,powerpoint的外接程序中换主题颜色
  • ¥15 状态机/汽车转向灯/Sateflow