加入Laravel表并根据记录ID显示结果

时间:2019-02-05 00:16:04

标签: html-table jointable

我希望加入2个表以显示Lavavel项目上的映射结果

这是我的表演。刀片

       <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>

这是我的Member.php文件

<?php
namespace App;
use Carbon\Carbon;

use Illuminate\Database\Eloquent\Model;

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');
    }

}

这是我的MemberController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

use App\Member;
use App\ActiveKids;
use Carbon\Carbon;

class MembersController extends Controller
{
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
 */

就像在我的索引中一样,想要从rankmappings表中显示成员排名。

索引视图工作正常,显示视图是导致我出现问题的视图。

当前我的结果是空白字段

0 个答案:

没有答案