我正在开发laravel 5.2。我想显示那些属于此时打开的特定组的成员。实际上,我收到了我存储在我的数据库中的所有成员,但是,我只想访问或只显示属于我当前正在访问的特定成员的那些成员。我收到一个错误:方法组不存在。如下所示:
我的控制器:
public function members($id){
$dashes=Grouptable::findorFail($id);
$members=Member::all();
return view('members' , ['dashes'=>$dashes,'members'=>$members]);
}
public function dashboard($id){
$dashes=Grouptable::findorFail($id);
return view('dashboard' , ['dashes'=>$dashes]);
}
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
我的观点:
<body>
<div class="row">
<div class="col-lg-3 col-lg-offset-1">
<img src="images/ImgResponsive_Placeholder.png"
class="img-circle img- responsive" alt="Placeholder image"> </div>
<div class="col-lg-7">
<h1 style="color:black;">{{ $dashes->name }}</h1></div>
<br />
</div>
<div class="row">
<div class="col-lg-3">
<button class="btn btn-success" onclick="myFunction()">
Add Members + </button>
<div>
<form id="demo" style="display:none;" method="post"
action="{{ route('addmember') }}">
<input class="form-control" type="text" name="addmember">
<button class="btn btn-primary" type="submit">Add</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
<div class="col-lg-7 col-lg-offset-0">
<div class="panel panel-default">
<div id="grp" class="panel-heading">
<h3 id="grouptitle" class="panel-title">Group Members</h3>
</div>
<div id="zx" class="panel-content">
<div class="row">
@foreach($members as $member)
<section class="col-md-6">
<div class="row">
<section class="col-md-offset-1 col-md-3 col-xs-offset-1
col-xs-4">
<img id="imagesize" src="images/g.jpg" class="img-circle"/>
</section>
<section class="col-md-offset-1 col-md-7 col-xs-7">
<section class="col-md-12">
<h5 id="friendname">{{$member->members}}</h5>
</section>
</section>
</div>
</section>
</div>
</section>
@endforeach
</div>
<div id="mn" class="panel-footer"><a id="seemr1"
href="#.html">See More</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
我的路线:
Route::get('/members/{id}',[
'uses'=>'GroupController@members',
'as'=>'members'
]);
Route::get('/dashboard/{id}',[
'uses'=>'GroupController@dashboard',
'as'=>'dashboard'
]);
Route::post('/memeber/add',[
'uses'=>'GroupController@addmembers',
'as'=>'addmember'
]);
我的模态:
Grouptable:
public function members(){
return $this->hasMany('App\Member');
}
Member:
public function groups(){
return $this->hasMany('App\Grouptable');
}
答案 0 :(得分:0)
你定义关系的方式是错误的。我认为这是OneToMany关系。像A Group有很多用户。然后你必须像这样定义它
<强>模型强>
Grouptable:
public function members(){
return $this->hasMany('App\Member','group_id'); // group_id represents the name of the foreign key. It is not neccessary if your foreign key is Grouptable_id, because Laravel automatically guess it unless the foreign key name is explicitly provided
}
Member:
public function groups(){
return $this->belongsTo('App\Grouptable','group_id');
}
要检索特定群组的所有成员,请说groupid 1
:
$members = \App\Gouptable::find(1)->members;
<强> CONTROLLER 强>
你的addmembers()
函数似乎有些错误:
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member); //what is this groups()?
//You need to do something like this
$groupid = $request['groupid']; // send groupid as parameter from form
Grouptable::find($groupid)->members()->save($member);
return redirect()->back();
}