自定义主键值未正确显示。

huangapple go评论81阅读模式
英文:

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-&gt;ticketID = uniqid();
        $ticket-&gt;sujet = $request-&gt;input(&#39;sujet&#39;);
        $ticket-&gt;description = $request-&gt;input(&#39;description&#39;);
        $ticket-&gt;status = 0; // Set default status
        $ticket-&gt;Priorite = $request-&gt;input(&#39;priorite&#39;);
        $ticket-&gt;user_id = Auth::id(); // Associate the ticket with the authenticated user
        $ticket-&gt;save();    
        return redirect()-&gt;route(&#39;dashboard&#39;)-&gt;with(&#39;success&#39;, &#39;Ticket created successfully&#39;);
    }`

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

` @foreach($tickets as $ticket)
        &lt;tr class=&quot;bg-white border-b hover:bg-gray-100 text-black&quot;&gt;
            &lt;td class=&quot;px-6 py-4&quot;&gt;{{ $ticket-&gt;ticketID }}&lt;/td&gt;
@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 = &#39;string&#39;;

    protected $primaryKey = &#39;ticketID&#39;;

}

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 = &#39;string&#39;;

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

@foreach($tickets as $ticket)
        &lt;tr class=&quot;bg-white border-b hover:bg-gray-100 text-black&quot;&gt;
            &lt;td class=&quot;px-6 py-4&quot;&gt;{{ $ticket-&gt;getOriginal(&#39;ticketID&#39;) }}&lt;/td&gt;
@endforeach

huangapple
  • 本文由 发表于 2023年6月5日 14:00:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定