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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题