我的Laravel项目中已有一个Join,并且有一个成员加入其Rank
我有一个Rankmapping表和一个Member Detail表。 在“成员详细信息”中,“等级”字段是Rankmapping表的ID。
在下面的代码中,我有一个完美的MemberController @ index页面基本连接。
但是我无法在MemberController @ show页面上使加入工作 我希望在此显示等级,因为随着等级的提升,成员将需要使用该等级来编辑该等级。
HTML页面(MemberController @ show)
@section('content')
<div class="container-fluid">
<div class="row">
<div class = "col-sm-12">
<div class = "card">
<div class="card-header card-header-icon card-header-rose">
<h4 class="card-title font-weight-bold">Member Details</h4>
</div>
<div class="card-body">
<table class="table">
<tr>
<th>First Name:</th>
<td style="border-top: 1px #ddd solid">{{$member->first_name}}</td>
<th>Last Name:</th>
<td style="border-top: 1px #ddd solid">{{$member->last_name}}</td>
<th>Rank:</th>
<td style="border-top: 1px #ddd solid">{{$member->rank}}</td>
</tr>
<tr>
<th>Age:</th>
<td>{{$member->age}} years</td>
<th>Date of Joining:</th>
<td>{{date("d/m/Y",strtotime($member->date_joined))}}</td>
<th>Service:</td>
<td>{{number_format((float)$member->service,2)}} years</td>
</tr>
</table>
</div>
</div>
</div>
</div>
HTML页面(MembersController @ Index)
@section('content')
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header card-header-icon card-header-rose">
<h4 class="card-title text-center">Members</h4>
</div>
<div class="card-body">
<div class="pull-right new-button">
<a href="{{action('MembersController@create')}}" class="btn btn-primary" title="Add Member"><i
class="fa fa-plus fa-2x"></i> Add Member</a>
</div>
<div class="table-responsive">
<table class="table">
<thead class="text-primary">
<th></th>
<th class="text-center">Membership Number</th>
<th class="text-center">Name</th>
<th class="text-center">Rank</th>
</thead>
<tbody>
@foreach($member as $m)
<tr>
<td class="text-center">
<a href="{{action('MembersController@show', $m->id)}}" title="View" class="btn btn-success"><i class="fa fa-info"></i></a>
</td>
<td class="text-center">{{$m->membership_number}}</td>
<td class="text-center">{{$m->last_name}}, {{$m->first_name}}</td>
<td class="text-center">{{$m->memberrank}}</td>
</tr>
@endforeach
</tbody>
<tfooter>
<tr>
</tr>
</tfooter>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
这是我的Member.php文件
class Member extends Model
{
//
protected $fillable = [
'id', 'membership_number', 'first_name', 'last_name', 'rank', 'date_joined', 'date_birth', 'active'
];
public function getAgeAttribute()
{
return Carbon::parse($this->attributes['date_birth'])->age;
}
public function getServiceAttribute()
{
$now = Carbon::now();
$service = Carbon::parse(date('Y-m-d',strtotime($this->date_joined)))->DiffInYears($now);
return $service;
}
public function ActiveKids()
{
return $this->hasMany('App\ActiveKids', 'member_id', 'id');
}
public function MemberRank()
{
return $this->hasOne('App\Rankmapping', 'rank', 'members_rank');
}
}
这是我的MemberController
class MembersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$member = DB::table('members')
->join('rankmappings', 'members.rank', '=', 'rankmappings.id')
->select('members.*', 'rankmappings.rank as memberrank')
->orderBy('last_name')
->get();
return view('members.index', compact('member'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view('members.add');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$validateData = Validator::make($request->all(), [
'membership' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'doj' => 'required|date',
'dob' => 'required|date',
]);
if ($validateData->fails())
{
return Redirect::back()->withErrors($validateData)->withInput();
}
//Create Member
$e = new Member();
$e->membership_number = $request->get('membership');
$e->first_name = $request->get('firstname');
$e->last_name = $request->get('lastname');
$e->rank = "";
$e->date_joined = $request->get('doj');
$e->date_birth = $request->get('dob');
$e->active= "Y";
$e->save();
return redirect(action('MembersController@index'))->with('success', 'Member Added');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
$member = Member::find($id);
if ($member !=null)
{
return view('members.show', compact('member', 'service', 'mrank'));
}
return redirect(action('MemberController@index'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
答案 0 :(得分:0)
除了使用join()
之外,您还可以使用Model本身进行查询,以便能够像with()
方法那样使用口才好。有了它,您可以使用with('MemberRank')
包含等级关系,也可以直接调用该关系。
代码看起来像这样:
use App\Member; // Important! Import your model
public function index()
{
$member = Member::with('MemberRank')
->orderBy('last_name')
->get();
return view('members.index', compact('member'));
}
public function show($id)
{
$member = Member::findOrFail($id);
return view('members.show', compact('member'));
}
然后在视图中将其命名为:
<td class="text-center">{{$member->MemberRank->rank}}</td>
提示:使用Eloquent进行查询时,您甚至可以不使用with()
或load()
来调用关系,因为它将使查询自动获取。但要注意:如果为列表的每个成员调用它,它将进行与该列表具有成员的查询一样多的查询。
解决方法是使用Eager Loading,在调用数据之前将其加载到变量中。您可以在这里阅读有关内容:Eloquent: Relationships/#Eager Loading