【问题标题】:Laravel Relationship Issues : Laravel 5.4Laravel 关系问题:Laravel 5.4
【发布时间】:2017-05-10 14:49:19
【问题描述】:

我的应用程序中有 2 个表...用户大会

我在conventioners 表中有用户ID,我想从Users 表中访问他们的性别....

conventioners 表中有 10 个用户 ID,users 表中有 20 个用户...

请问如何访问用户表中的所有性别...

$conventioners->users()->gender

Conventioners 是 Conventioner 模型的一个实例,其中包含关系 **belongsToMany

非常感谢大家

这是我的惯例模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Conventioner extends Model
{
    /**
     * @var string
     */
    protected $table = 'conventioners';

    /**
     * @var array
     */
    protected $fillable = [
        'user_id','year','church_id','convention_id'
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function users()
    {
        return $this->hasMany('App\User');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function convention()
    {
        return $this->belongsTo('App\Convention');
    }
}

这是我的 ConventionController 方法,称为 Convention... 它检索当前约定的详细信息

public function convention($slug)
    {
        if(!$this->admin()) return redirect()->back();

        $convention = Convention::where('slug', $slug)->first();

        $participants = Conventioner::where('convention_id', $convention->id)->get();

        $conventioner = [];
        foreach($participants as $participant)
        {
            $thisUser = [];
            $thisUser['data'] = User::withTrashed()->where('id', $participant->user_id)->first();
            $thisUser['convention'] = $participant;
            array_push($conventioner, $thisUser);
        }

        var_dump($participants->users()->pluck('gender')->all());

        return view('dashboard/conventions/convention', [
            'convention' => $convention,
            'user' => Auth::user(),
            'conventioners' => $convention->conventioners(),
            'participants' => $conventioner
        ]);
    }

【问题讨论】:

  • 试试$conventioners-&gt;users-&gt;gender
  • 在此处发布您的模型会有所帮助
  • BelongsToMany 需要一个数据透视表。认为您可能对您的关系定义感到困惑。
  • @SapneshNaik 已尝试 $conventioners->users->gender 但它不起作用
  • @prime 数据透视表..?

标签: laravel laravel-5


【解决方案1】:

问题在于用户是一个集合,而不是您可以调用gender 的个人。如果您想要所有性别的列表,可以使用以下内容:

Conventioner::where('convention_id', $convention->id)->with('users')->get()
$conventioners->pluck('users')->pluck('gender')->all();

这将返回一个性别数组。你可以阅读更多关于pluckhere的信息。

pluck 方法检索给定键的所有值

【讨论】:

  • Collection.php 第 1564 行中的异常:此集合实例上不存在属性 [用户]。
  • Macroable.php 第 74 行中的 BadMethodCallException:方法用户不存在。
  • conventioners 不是 Laravel 集合吗?
  • $convention = Convention::where('slug', $slug)->first(); $conventioners = Conventioner::where('convention_id', $convention->id)->get();
  • 您能否将您的 Conventioner 模型用户关系添加到您的初始问题?
猜你喜欢
  • 2017-12-13
  • 2018-01-22
  • 2017-10-25
  • 2013-04-03
  • 2017-09-22
  • 2017-10-01
  • 2017-08-22
  • 1970-01-01
相关资源
最近更新 更多