Search code examples
laravelmany-to-many

laravel, displaying data from another table


I am trying to solve following problem: I have 3 tables : passports ,statuses and passport_statuses, (passport_statuses table is the pivot table formed because of many to many relationship between passports and statuses. The model are like below: passport model

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

}

Statuses model

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

So I was able to save the passport status in the pivot table. Now I a trying to display the the latest status of that passport in the index page. index page snapshot

saving passport details

my index view

@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

Controller

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

Solution

  • You can add a function to your model that gets the latest status, and then you can use that function inside your view. This new function is not a new relationship, it is just a standard function.

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

    In your view:

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