我的应用程序中有2个表...用户约定
我在传统者表中有用户ID,我想从Users表中访问他们的性别....
我在常规表中有10个用户ID,用户表中有20个用户...
请如何在用户表中访问所有性别...
$conventioners->users()->gender
Conventioner是Conventioner Model的一个实例,它包含一个关系** 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
]);
}
答案 0 :(得分:0)
问题是用户是一个集合,而不是您可以呼叫gender
的个人。如果您想要所有性别的列表,您可以使用以下内容:
Conventioner::where('convention_id', $convention->id)->with('users')->get()
$conventioners->pluck('users')->pluck('gender')->all();
这将返回一系列性别。您可以详细了解pluck
here。
pluck方法检索给定键的所有值