PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 26, 2022

[FIXED] in laravel How to display values from two different model in single view

 January 26, 2022     laravel, laravel-5, laravel-5.7, php     No comments   

Issue

I have created a single view file and I have multiple models. I what to show multiple model value in the view.

I want to display student details using "STUDENT MODEL" along with show center_code using "CENTER MODEL".

I tried but it's showing some error.

Property [center_code] does not exist on this collection instance. (View: C:\resources\views\Center\registretion_form_hardcopy.blade.ph

1st model student

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    protected $table = 'student_lists';


    protected $fillable = ['student_name', 'student_registration_id', 'date_of_join', 'student_phone_no',];


}

2nd model student

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Center extends Model
{
    protected $table = 'centers';
    protected $fillable = ['center_code','center_name'];    
}

}

Route

 Route::get('registretion_form_hardcopy{id}', 'StudentController@registretion_form_hardcopy');

Controller

public function registretion_form_hardcopy($id)
{
  $hardcopy = Student::where('delete_status','NOT DELETED')->where('center_accepting_online_application','ACCEPTED')->where('center_approved','APPROVED')->where('center_code','=',Auth::user()->center_code)->find($id);

  $center_details_hardcopy = Center::where('center_code','=',Auth::user()->center_code)->get();

  $pdf = PDF::loadView('Center.registretion_form_hardcopy', compact('hardcopy','center_details_hardcopy'))->setPaper('a4', 'portrait');

  $fileName = $hardcopy->student_registration_id;
  return $pdf->stream($fileName . '.pdf');
}

View

<!DOCTYPE html>
<html>
<head>
    <!-- <title>0</title> -->


    <style>
        *{margin: 0; padding: 0;}
        body{ font-size:20px; }
        .banner{position: relative; width: 90%; margin: 0 auto; }
        .banner img{width: 100%}
        .heading{color: red; position: absolute; top: 50%; width: 100%; text-align: center; font-size:3rem;text-shadow: 5px 5px 10px #000000; }
        .x{color: pink; position: absolute; top: 50%; width: 100%; text-align:  }


           @page {
      size: 8.5in 11in;
      margin: .5in;
    }
    #bgimg {
      position: fixed;
      left: .0in;
      top: -.1in;
      width: 8.5in;
      height: 11in;
      z-index: -999

    }

    p {
  padding-left: 100px;
font-style: initial;
}

    b {
  padding-left: 100px;
font-style: initial;
}
  #header{
    position: relative;
}
#logo{
    position: absolute;


    right: 30px;
}

.left{
float:left;
}
.right{
float:right;
}


div.r {
  line-height: 90%;
}


    </style>
</head>
<body>
    <div class="container">

        <div class="banner">
            <img id="bgimg" src="center student hardcopy.jpg" alt="img" >
        </div>


  <font size="-1"><p style="font-family:sans-serif; "><b4 style="font-family:sans-serif;padding-left:170px;">{{$hardcopy->student_registration_id}}</b4></p></font>
<br>
  <font size="-1"><p style="font-family:sans-serif; "><b4 style="font-family:sans-serif;padding-left:170px;">{{$hardcopy->student_name}}</b4></p></font>
<br>


              <br> 
<br>


             <font size="-1">

                <div  class="r"><b3 style="font-family:sans-serif;padding-left:525px; text-align:right">{{$center_details_hardcopy->center_code}}<div></div></b3></font>       
</div><br>   
</body>
</html>

Any help is Appreciated.


Solution

You're getting multiple collections in controller so use first() instead of get()

change in Controller

$center_details_hardcopy = Center::where('center_code','=',Auth::user()->center_code)->first();

Change in blade file

{{$center_details_hardcopy->center_code ?? ''}}


Answered By - Dilip Hirapara
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing