laravel,显示另一个表中的数据

时间:2016-01-13 07:01:07

标签: laravel many-to-many

我正在尝试解决以下问题: 我有3个表:护照,状态和passport_statuses,(passport_statuses表是由于护照和状态之间的多对多关系而形成的数据透视表。模型如下所示: 护照模式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Passport extends Model
{
    protected $fillable=[
'Full_Name',
'Date_of_Birth',
'Passport_Number',
'comments',
'Delivered_to_owner'
    ];


public function status()
{

    return $this->belongsToMany('App\Statuses',"passport_statuses","passport_id","statuses_id")->withTimestamps();

}

public function LatestStatus($query)
{

    //dd($this->status[0]);
    $allStatus=$this->status()->orderBy('updated_at')->first();
    dd($allStatus);
    //return $allStatus[0]->Status_Name;
    //dd($allStatus[0]->attributes["Status_Name"]);
    //dd($this->status()->get());
    //return $allStatus[0]->attributes["Status_Name"];
    //dd($allStatus);
}

}

状态模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Statuses extends Model
{
 protected $fillable=[
'Status_Name'

];

public function passport()
{
    return $this->belongsToMany('App\Passport')->withTimestamps();
}
}

passportstatus模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PassportStatus extends Model
{
protected $fillable=[
'passport_id',
'statuses_id'
];
}

所以我能够将护照状态保存在数据透视表中。现在我试图在索引页面中显示该护照的最新状态。 index page snapshot

saving passport details

我的索引视图

@extends('admin.adminmaster')
@section('content')
 <h2>Passport list </h2>
<a class="btn btn-primary" href="passport/create" role="button">+ Add New Passport</a>
<hr>
<table style="width:100%">
<tr>
    <th>Name</th>
    <th>Passport Number</th>
    <th>Status</th>
</tr>
 @foreach($passports as $passport)
 <tr>
<td><a href="{{action('PassportController@show',[$passport->id])}}"><h4>    {{$passport->Full_Name}}</h4></a></td>

<td>{{$passport->Passport_Number}}</td>

{{--<td>{{dd($passport->status())}}</td>--}}
<td><a class="btn btn-warning" href="{{action('PassportController@edit',                     [$passport->id])}}" role="button">Edit</a>
   <td><a class="btn btn-danger" href="{{action('PassportController@destroy',[$passport->id])}}" role="button"><span class="glyphicon glyphicon-trash"></span> Delete</a>
</td>
   </tr>
     @endforeach
    </table>
    @endsection

控制器

 public function index()
  {
     $passports=Passport::latest()->get();
    return view('admin.passport.index')->with('passports',$passports);
  }
public function create()
  {
        $statuses=Statuses::lists('Status_Name','id');
    return view('admin.passport.create')->with('statuses',$statuses);
  }
 public function store(Request $request)
  {
$passport=Passport::create($request->all());


$passport->status()->attach($request->input('Status_Name'));
  return redirect('admin/passport');
}
 public function show($id)
{
     $passports=Passport::findorFail($id);

    return view('admin.passport.show')->with('passports',$passports);
}

1 个答案:

答案 0 :(得分:0)

您可以向模型添加一个获取最新状态的函数,然后您可以在视图中使用该函数。这个新功能不是新关系,它只是一个标准功能。

class Passport extends Model
{
    public function status() {
        return $this->belongsToMany('App\Statuses',"passport_statuses","passport_id","statuses_id")->withTimestamps();
    }

    // this is not a relationship. just a standard method on the model.
    public function latestStatus() {
        return $this->status()->latest()->first();
    }
}

在您看来:

<td>{{ $passport->latestStatus()->Status_Name }}</td>