skip to Main Content

Hi hope you are having a good day ! ,I have a Laravel application where I’m using a custom primary key for my Ticket model. In the store method of my TicketController, I generate a random string for the ticketID column using uniqid(). When I check the database that im using phpMyAdmin, I can see that the ticketID values are correctly generated and stored.

but, when I try to display or work with the ticketID in my views, only a small part of the generated ID is being shown specially the numbers . For example, if the generated ticketID is "6045e16d8db80" picture 1,
in my views or when trying to access it, I only see "6045".picture 2

Here’s the relevant code in my TicketController:

`public function store(Request $request)
    {
        $ticket = new Ticket();
        $ticket->ticketID = uniqid();
        $ticket->sujet = $request->input('sujet');
        $ticket->description = $request->input('description');
        $ticket->status = 0; // Set default status
        $ticket->Priorite = $request->input('priorite');
        $ticket->user_id = Auth::id(); // Associate the ticket with the authenticated user
        $ticket->save();    
        return redirect()->route('dashboard')->with('success', 'Ticket created successfully');
    }`

And here’s how I’m trying to display the ticketID in my view:

` @foreach($tickets as $ticket)
        <tr class="bg-white border-b hover:bg-gray-100 text-black">
            <td class="px-6 py-4">{{ $ticket->ticketID }}</td>
@endforeach`

I’m not sure why only part of the ticketID is being displayed. Any insights or suggestions on how to fix this issue would be greatly appreciated. Thank you!

and i already checked everything model schema my database too and the other colums work fine the problem is just in ticketid,

2

Answers


  1. It’s because the Primary key has been an INTEGER field by default the day since they wrote MySQL 😉

    You should add $keyType and $incrementing const in your mode for this.

    class Ticket extends Model
    {
        public $incrementing = false;
    
        protected $keyType = 'string';
    
        protected $primaryKey = 'ticketID';
    
    }
    

    Finally, run

    php artisan config:clear
    

    Take a look at naming conversation (DB column ticketID should be ticket_id, Not a must. But good to follow)

    Login or Signup to reply.
  2. Primary key always cast to an integer, so update your model with the required datatype for primary key

    protected $keyType = 'string';
    

    Otherwise, try getOriginal() method, just replace the below code

    @foreach($tickets as $ticket)
            <tr class="bg-white border-b hover:bg-gray-100 text-black">
                <td class="px-6 py-4">{{ $ticket->getOriginal('ticketID') }}</td>
    @endforeach
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search