Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Manzoorahmed's avatar

Generate certificate

Hi i'm tryin to generate dynamic certificates using intervention image package i make a Registration controller How can I get username Dynamically on certificate from database

public function certificate(Request $request ) {

    $image = Image::make(public_path('frontend/img/CME CERTIFICATE .jpg'));
    $name="Vishal Gupta";
    imagettftext($image,50,0,365,420,$color,$font,$_POST['name']);
    $image->text('Ahmed', 4050, 2250, function($font) {
        $font->file(public_path('fonts/AGENCYR.TTF'));
        $font->size(500);
        $font->color('#EB2B33');
        $font->align('center');
        $font->valign('bottom');
        $font->angle(0);
    });

    $image->save(public_path('frontend/img/username .jpg'));
    Session::flash('success_message', 'Certificate Generate Successfully!');
    return back();

// return view('frontend.certificate'); }

0 likes
88 replies
martinbean's avatar

@manzoorahmed You’d probably be better off creating a certificate using DOMPDF, like Cashier does for receipts. Then you could “design” your certificate as a Blade template that’s rendered to a PDF.

1 like
Manzoorahmed's avatar

@martinbean Thank You if it is possible that i used image path in DOMPDF, and just username get from database

Sinnbeck's avatar

@Manzoorahmed why do you want to use an image? Creating a html page with laravel is generally a lot easier

Manzoorahmed's avatar

@Sinnbeck Thank You please share any example that i generate dynamically username on certificate from database

Manzoorahmed's avatar

@Sinnbeck public function generate_certificate(Request $request) { $registrations['registrations'] = Registration::find(2);

    if ($request->has('download')) {
        $pdf = PDF::loadView('admin.registrations.generate_certificate',$registrations);
        $pdf->setPaper('A4', 'landscape');
        return $pdf->stream('generate_certificate.pdf');

    }
    return view('admin.registrations.generate_certificate');

// $data['registrations'] = Registration::find(2); // $pdf = Pdf::loadView('generate_certificate', $data); // return $pdf->download('generate_certificate.pdf'); }

i have create this function but it will return the whole user record on certificate i need separate certificate for each user @foreach ($registrations as $registration) {{ $registration->first_name }} @endforeach

Sinnbeck's avatar

@Manzoorahmed You need to make multiple certificates? Then you need to write code that creates them on disk, and adds them to a zip file. You cannot return multiple files as a single download

Manzoorahmed's avatar

@Sinnbeck i just want to show unique name on certificate user name is showing on certificate but it get all user record on one certificate i just need unique name on certificate according to user registration i place this code in my blade file but it will retrieved the whole user record that exist in database {{ $registration->name }}

Sinnbeck's avatar

@Manzoorahmed How can it get all users on 1 certificate if you are using User::find() ? Can you show some code?

Manzoorahmed's avatar

@Sinnbeck public function generate_certificate(Request $request) { $registrations = DB::table("registrations")->get(); view()->share('registrations', $registrations);

    if ($request->has('download')) {
        $pdf = PDF::loadView('admin.registrations.generate_certificate');
        $pdf->setPaper('A4', 'landscape');
        return $pdf->stream('generate_certificate.pdf');

    }
    return view('admin.registrations.generate_certificate');

// $data['registrations'] = Registration::findOrFail($id); // $pdf = Pdf::loadView('generate_certificate', $data); // return $pdf->download('generate_certificate.pdf'); }

Manzoorahmed's avatar

@Sinnbeck before i used this one but it will not work $data['registrations'] = Registration::findOrFail(); $pdf = Pdf::loadView('generate_certificate', $data); return $pdf->download('generate_certificate.pdf');

Sinnbeck's avatar

@Manzoorahmed Not sure what to do with "it will not work". And you are only generating 1 certificate here I assume? So pass in whatever data you need to the view. Start by designing the page without PDF.. This is a basic blade view

Manzoorahmed's avatar

@Sinnbeck i design certificate its work but username that i get from databse alluser record show in one certificate i need a unique certificate for each user

martinbean's avatar

@manzoorahmed Can you format your code properly? It’s hard to read when some lines are formatted and others aren’t.

Manzoorahmed's avatar

@martinbean i Want to display only single value in blade but it show multiple row value on one certificate how can i get only single value in blade

Manzoorahmed's avatar

@Sinnbeck

public function generate_certificate(Request $request) { $registrations = DB::table('registrations')->find(3); // $registrations = DB::table("registrations")->get(); view()->share('registrations', $registrations);

    if ($request->has('download')) {
        $pdf = PDF::loadView('admin.registrations.generate_certificate');
        $pdf->setPaper('A4', 'landscape');
        return $pdf->stream('generate_certificate.pdf');

    }
    return view('admin.registrations.generate_certificate');
Manzoorahmed's avatar

@Sinnbeck Thank you for your support i just want to display single record value on each certificate but it show multiple username on each certificate

Manzoorahmed's avatar

@Sinnbeck I used this one but it will not work Could You please check it

$registrations['registrations'] = Registration::find(2); $pdf = Pdf::loadView('pdf.generate_certificate', $registrations); return $pdf->download('generate_certificate.pdf');

Sinnbeck's avatar

@Manzoorahmed That is because you arent formatting it correctly. Add ``` on the line before and after the code (not on the same line)

Manzoorahmed's avatar

@Sinnbeck

<div class="cert-container print-m-0">
   <div id="content2" class="cert">
       <img

           src= "{{ asset('frontend/img/CME CERTIFICATE (1).png')}}"
           class="cert-bg"
           alt=""
       />
       @foreach ($registrations as $registration)
           <div class="cert-content">
               <h1 class="other-font">Certificate of Completion</h1>
               <span style="font-size: 40px;">{{ $registration->first_name }}</span>
               <br/><br/><br/><br/>
               <span class="other-font"
               ><i><b>has completed the</b></i></span
               >
               <br/>
               <span style="font-size: 40px;"><b>Advance Wound Care Course</b></span>
               <br/>
               <small>(Component 2)</small>
               <br/><br/><br/><br/>

           </div>
       @endforeach
   </div>

</div>
Manzoorahmed's avatar

@Sinnbeck I have shared b0th controller and blade code when You get a chance please have a look

public function generate_certificate(Request $request)
   {
       {
           $registrations['registrations'] = Registration::find(2);
           $pdf = Pdf::loadView('admin.registrations.generate_certificate', $registrations);
           return $pdf->download('generate_certificate.pdf');


       }
Sinnbeck's avatar

@Manzoorahmed Well you are doing a foreach look, and thereby adding more than one..

@foreach ($registrations as $registration)

In the controller

$data['registration'] = Registration::find(2);
$pdf = Pdf::loadView('admin.registrations.generate_certificate', $data);

and blade (not foreach!)

           <div class="cert-content">
               <h1 class="other-font">Certificate of Completion</h1>
               <span style="font-size: 40px;">{{ $registration->first_name }}</span>
               <br/><br/><br/><br/>
               <span class="other-font"
               ><i><b>has completed the</b></i></span
               >
               <br/>
               <span style="font-size: 40px;"><b>Advance Wound Care Course</b></span>
               <br/>
               <small>(Component 2)</small>
               <br/><br/><br/><br/>

           </div>
Manzoorahmed's avatar

@Sinnbeck i removed foreacch loop but it will show error Trying to get property 'first_name' of non-object (View: C:\xampp\htdocs\new-temp\resources\views\admin\registrations\generate_certificate.blade.php)

Manzoorahmed's avatar

@Sinnbeck This one is my controller i store data in registrationcontroller and i create a certificate generate function in this controller

public function store(Request $request)
   {
//        dd($request);
       $this->validate($request, [
//            'name' => 'required|unique:registrations,name|max:255',
           'email' => 'required|unique:registrations,email|max:255',
           'course' => 'required|unique:registrations,course|max:255',
           'first_name' => 'required|unique:registrations,first_name|max:255',
           'last_name' => 'required|unique:registrations,last_name|max:255',
//            'reference' => 'required|unique:registrations,reference|max:255',
           'date' => 'required:registrations,date|max:255',
           'genders' => ['required', 'in:male,female'],
           'cc_email' => 'required|unique:registrations,cc_email|max:255',
           'phone_no' => 'required|unique:registrations,phone_no|numeric|max:255',
           'id_passport' => 'required|unique:registrations,id_passport|max:255',
           'professional_board' => 'required|unique:registrations,professional_board|max:255',
           'facility' => 'required|unique:registrations,facility|max:255',
           'experience' => 'required|unique:registrations,experience|max:255',
           'country' => 'required:registrations,country|max:255',
           'image' => 'required|image|mimes:jpeg,png,jpg'
       ]);
       $input = $request->all();
       $registration = new Registration();
//        $registration->name = $input['name'];
       $registration->email = $input['email'];
       $registration->course = $input['course'];
       $registration->first_name = $input['first_name'];
       $registration->last_name = $input['last_name'];
       $registration->genders = $input['genders'];
       $registration->reference = date("Y") . rand(100, 999);
       $registration->date = $input['date'];
       $registration->phone_no = $input['phone_no'];
       $registration->id_passport = $input['id_passport'];
       $registration->professional_board = $input['professional_board'];
       $registration->cc_email = $input['cc_email'];
       $registration->facility = $input['facility'];
       $registration->experience = $input['experience'];
       $registration->country = $input['country'];
//        $registration->slug = $this->createSlug($request->input('name'));
       if ($request->hasFile('image')) {
           if ($request->file('image')->isValid()) {
               $this->validate($request, [
                   'image' => '|image|mimes:jpeg,png,jpg'
               ]);
               $file = $request->file('image');
               $destinationPath = "uploads/images";
               $extension = $file->getClientOriginalExtension();
               $fileName = $file->getClientOriginalName();
               $fileName = rand() . $fileName;
               //renaming image
               $request->file('image')->move($destinationPath, $fileName);
               $registration->image = $fileName;
               $registration->save();

           }

       }
       $registration->save();
       Session::flash('success_message', 'Great! Registrations has been saved successfully!');
       return redirect()->back();
   }
Manzoorahmed's avatar

@Sinnbeck

public function generate_certificate(Request $request)
    {
        {
            $registrations['registration'] = Registration::find(2);
            $pdf = Pdf::loadView('admin.registrations.generate_certificate', $registrations);
            return $pdf->stream('generate_certificate.pdf');
            
        }

    }
Manzoorahmed's avatar

@Sinnbeck its not showing any output

 <div class="cert-content">
            <h1 class="other-font">Certificate of Completion</h1>
            <span style="font-size: 40px;">{{ $registrations->first_name }}</span>
            @dd($registrations)
            <br/><br/><br/><br/>
            <span class="other-font"
            ><i><b>has completed the</b></i></span
            >
            <br/>
            <span style="font-size: 40px;"><b>Advance Wound Care Course</b></span>
            <br/>
            <small>(Component 2)</small>
            <br/><br/><br/><br/>

        </div>
Manzoorahmed's avatar

@Sinnbeck Undefined variable: registrations (View: C:\xampp\htdocs\new-temp\resources\views\admin\registrations\generate_certificate.blade.php)

Sinnbeck's avatar

@Manzoorahmed is it called registration or registrations? My examples use registrations, but you seem to use both at random?

1 like
Manzoorahmed's avatar

@Sinnbeck please check this one

$registrations['registration'] = Registration::find(2);
       $pdf = Pdf::loadView('admin.registrations.generate_certificate', $registrations);


       if ($request->has('download')) {
           $pdf->setPaper('A4', 'landscape');
           return $pdf->stream('pdfview.pdf');

       }
       return view('admin.registrations.generate_certificate');

   }
Manzoorahmed's avatar

@Sinnbeck if i used $registration it will show this error

ErrorException
Trying to get property 'first_name' of non-object (View: C:\xampp\htdocs\new-temp\resources\views\admin\registrations\generate_certificate.blade.php)
http://127.0.0.1:8000/admin/generate_certificate
Sinnbeck's avatar

@Manzoorahmed I think my syntax might have confused you. Use this syntax if you better understand it

$pdf = Pdf::loadView('admin.registrations.generate_certificate', ['registration' => Registration::find(2));

And add @dd() before $registration->first_name. Notice I wrote registration not registrations

Manzoorahmed's avatar

@Sinnbeck yesteday i shared a controller function that i used for registration stored in db its working fine please have a look

Sinnbeck's avatar

@Manzoorahmed ok don't know how to help you then. Start by making a completely normal view, and once that is working look into pdf. Passing a variable is basic laravel :)

Manzoorahmed's avatar

@Sinnbeck I'm facing this issue

ErrorException

Trying to get property 'first_name' of non-object (View: C:\xampp\htdocs\new-temp\resources\views\admin\registrations\generate_certificate.blade.php)

Manzoorahmed's avatar

@Sinnbeck Now i'm facing this issue I dod some changes in blade

Using $this when not in object context

<div class="cert-content">
           <h1 class="other-font">Certificate of Completion</h1>

           <span style="font-size: 40px;"> {{ $registration['first_name'] }}</span>
           <br/><br/><br/><br/>
           <span class="other-font"
           ><i><b>has completed the</b></i></span
           >
           <br/>
           <span style="font-size: 40px;"><b>Advance Wound Care Course</b></span>
           <br/>
           <small>(Component 2)</small>
           <br/><br/><br/><br/>

       </div>
Lara_Love's avatar

This code is quite thick, my friend. What is the problem with this code that Mr @Sinnbeck wrote?

Lara_Love's avatar

You save variable information such as name, surname, etc. through the form in the database and this information is placed on the pre-designed certificate and the user downloads her certificate.

Manzoorahmed's avatar

@LoverToHelp yes i create controller and store user information now i want to use that variable to generate certificate for user but i'm facing issue in my blade i did some changes in code and now this error will come

Using $this when not in object context
Lara_Love's avatar

no add it controller above your public Function . First, see what this word is for and define it consistently

Manzoorahmed's avatar

@LoverToHelp like that but it show error

private static $this =" '';
public function generate_certificate(Request $request)
   {

       $pdf = Pdf::loadView('admin.registrations.generate_certificate', ['registration' => Registration::find(1)]);


       if ($request->has('download')) {
           $pdf->setPaper('A4', 'landscape');
           return $pdf->stream('pdfview.pdf');

       }
       return view('admin.registrations.generate_certificate');

   }
Manzoorahmed's avatar

@LoverToHelp this error should came in model please have a look Registration model code

Using $this when not in object context

App\Models\Registration::find
C:\xampp\htdocs\new-temp\app\Models\Registration.php:55

//    protected $hidden = [

//        'email',

//        'remember_token',

//    ];

//    /**

//     * The attributes that should be cast.

//     *

//     * @var array<string, string>

//     */

//    protected $casts = [

//        'email_verified_at' => 'datetime',

//    ];





   public function collection()

   {

       return Registration::all();

   }

   public function find()

   {

       return $this->belongsTo(Registration::class);

   }



   public function Event()

   {

       return $this->belongsTo(Event::class);

   }

}
Tray2's avatar

@Manzoorahmed Don't listen to @lovertohelp, he has no clue what he is doing here.

$this is a reserverd variable name, that is the current instance of the object. It should never be defined as he told showed you.

Lara_Love's avatar

@Tray2 It is better in life to learn to solve problems in a beautiful way and away from destroying others. Not everyone was a professor from the beginning.

Manzoorahmed's avatar

@LoverToHelp this one is controller code


public function store(Request $request)
   {
//        dd($request);
       $this->validate($request, [
//            'name' => 'required|unique:registrations,name|max:255',
           'email' => 'required|unique:registrations,email|max:255',
           'course' => 'required|unique:registrations,course|max:255',
           'first_name' => 'required|unique:registrations,first_name|max:255',
           'last_name' => 'required|unique:registrations,last_name|max:255',
//            'reference' => 'required|unique:registrations,reference|max:255',
           'date' => 'required:registrations,date|max:255',
           'genders' => ['required', 'in:male,female'],
           'cc_email' => 'required|unique:registrations,cc_email|max:255',
           'phone_no' => 'required|unique:registrations,phone_no|numeric|max:255',
           'id_passport' => 'required|unique:registrations,id_passport|max:255',
           'professional_board' => 'required|unique:registrations,professional_board|max:255',
           'facility' => 'required|unique:registrations,facility|max:255',
           'experience' => 'required|unique:registrations,experience|max:255',
           'country' => 'required:registrations,country|max:255',
           'image' => 'required|image|mimes:jpeg,png,jpg'
       ]);
       $input = $request->all();
       $registration = new Registration();
//        $registration->name = $input['name'];
       $registration->email = $input['email'];
       $registration->course = $input['course'];
       $registration->first_name = $input['first_name'];
       $registration->last_name = $input['last_name'];
       $registration->genders = $input['genders'];
       $registration->reference = date("Y") . rand(100, 999);
       $registration->date = $input['date'];
       $registration->phone_no = $input['phone_no'];
       $registration->id_passport = $input['id_passport'];
       $registration->professional_board = $input['professional_board'];
       $registration->cc_email = $input['cc_email'];
       $registration->facility = $input['facility'];
       $registration->experience = $input['experience'];
       $registration->country = $input['country'];
//        $registration->slug = $this->createSlug($request->input('name'));
       if ($request->hasFile('image')) {
           if ($request->file('image')->isValid()) {
               $this->validate($request, [
                   'image' => '|image|mimes:jpeg,png,jpg'
               ]);
               $file = $request->file('image');
               $destinationPath = "uploads/images";
               $extension = $file->getClientOriginalExtension();
               $fileName = $file->getClientOriginalName();
               $fileName = rand() . $fileName;
               //renaming image
               $request->file('image')->move($destinationPath, $fileName);
               $registration->image = $fileName;
               $registration->save();

           }

       }
       $registration->save();
       Session::flash('success_message', 'Great! Registrations has been saved successfully!');
       return redirect()->back();
   }

public function generate_certificate(Request $request)
   {

       $pdf = Pdf::loadView('admin.registrations.generate_certificate', ['registration' => Registration::find(1)]);


       if ($request->has('download')) {
           $pdf->setPaper('A4', 'landscape');
           return $pdf->stream('pdfview.pdf');

       }
       return view('admin.registrations.generate_certificate');

   }
Manzoorahmed's avatar

@LoverToHelp

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Registration extends Model
{
   protected $fillable = [
//        'id',
       'reference',
       'email',
       'course',
       'first_name',
       'last_name',
       'genders',
       'image',
       'date',
       'cc_email',
       'country',
       'phone_no',
       'id_passport',
       'professional_board',
       'facility',

   ];

//    /**
//     * The attributes that should be hidden for serialization.
//     *
//     * @var array<int, string>
//     */
//    protected $hidden = [
//        'email',
//        'remember_token',
//    ];
//    /**
//     * The attributes that should be cast.
//     *
//     * @var array<string, string>
//     */
//    protected $casts = [
//        'email_verified_at' => 'datetime',
//    ];


   public function collection()
   {
       return Registration::all();
   }
   public function find()
   {
       return $this->belongsTo(Registration::class);
   }

   public function Event()
   {
       return $this->belongsTo(Event::class);
   }
}



Manzoorahmed's avatar

@Sinnbeck sir i checked its working the problem is in blade view when i used foreach loop then it show multiple data but when i removed foreach loop be because i need single value so it will not work it show error

Lara_Love's avatar
public function collection()
  {
      return Registration::all();
  }

is this in model is correct work ?

Lara_Love's avatar

And where is it used? I could not see the connection problem. If it works properly without it, why should you write it?

Lara_Love's avatar
Manzoorahmed's avatar

@LoverToHelp sir as i shared before blade code when i used foreach loop it will work and return multiple username on single certificate that exists in database but i need separate certificate for each user so i retrieve a single record for each certificate so i removed foreach loop and jus used like that{{$registration - >first-name}} but it shoe error

Lara_Love's avatar

@Manzoorahmed Our team made it before. But I think that in the worst case, you should create the same thing with Photoshop and upload its pdf or image to the user. easy way ;)

Lara_Love's avatar

@Manzoorahmed its better . if you want take from database you should make html certificate and then use from database

<select>select user</select> for store in database

then

@foreach($user as $item)
{{ $item->name}}
@endforeach 
Lara_Love's avatar

Do not focus on solvable problems. Website design is not the work of one person. It requires a ui ux team.

Manzoorahmed's avatar
Manzoorahmed
OP
Best Answer
Level 1

Solve just add this code

$registration = DB::table('registrations')->find($id);
     
return view('admin.registrations.generate_certificate', compact('registration'));

Please or to participate in this conversation.