英文:
Custom primary key value not being displayed correctly
问题
嗨,希望你今天过得愉快!我有一个Laravel应用程序,在这个应用程序中,我为我的Ticket模型使用了自定义主键。在我的TicketController的store方法中,我使用uniqid()生成了一个随机字符串作为ticketID列的值。当我检查我使用phpMyAdmin的数据库时,我可以看到ticketID的值被正确生成和存储。
但是,当我尝试在视图中显示或处理ticketID时,只有生成的ID的一小部分被显示,特别是数字部分。例如,如果生成的ticketID是"6045e16d8db80",在我的视图中或尝试访问它时,我只看到"6045"。
这是我的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; // 设置默认状态
$ticket->Priorite = $request->input('priorite');
$ticket->user_id = Auth::id(); // 将票关联到经过身份验证的用户
$ticket->save();
return redirect()->route('dashboard')->with('success', 'Ticket created successfully');
}
这是我尝试在视图中显示ticketID的方式:
@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>
</tr>
@endforeach
我不确定为什么只显示了ticketID的一部分。对于如何解决这个问题的任何见解或建议将不胜感激。谢谢!
我已经检查了所有的模型模式以及我的数据库,其他列都正常工作,问题只出现在ticketID上。
英文:
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,
答案1
得分: 1
因为主键默认是INTEGER
字段,从编写MySQL开始的那一天起
你应该在你的模型中添加$keyType
和$incrementing
常量。
class Ticket extends Model
{
public $incrementing = false;
protected $keyType = 'string';
protected $primaryKey = 'ticketID';
}
最后,运行
php artisan config:clear
看一看命名规范(数据库列
ticketID
应该是ticket_id
,不是必须的,但最好遵循)
英文:
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)
答案2
得分: 1
主键始终转换为整数,因此请使用所需的主键数据类型更新您的模型。
protected $keyType = 'string';
否则,尝试使用 getOriginal()
方法,只需替换下面的代码:
@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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论