首页 > 解决方案 > 每个纪念品都会生成一个序列号

问题描述

我需要为每个纪念品生成一个序列号(在每一列中,不同的序列号)例如,纪念品 1 的序列号将是“AK0001”。我希望“AK”永久(无法更改)在那里。但对于“0001”则不然。它可以随机变化

我使用 laravel 5.4 及以下,adminlte,heidisql

我只是在纪念品的每一列中添加了另一行序列号

纪念品控制器.php

public function create()
    {
        return view('admin.souvenir.create');
    }

public function store(Request $request)
    {

        $souvenir = new Souvenir;
        $souvenir->name = $request->name;
        $souvenir->dateReceived = $request->dateReceived;
        $souvenir->description = $request->description;
        $souvenir->receivedFrom = $request->receivedFrom;
        $souvenir->location = $request->location;
        $souvenir->status = $request->status;
        $souvenir->save();

        if( !$souvenir->save() ){
            return redirect()
                ->route('admin.souvenir.index')
                ->with('error', "Error creating souvenir");
        }
        return redirect()
            ->route('admin.souvenir.index')
            ->with('success', "Souvenir created successfully!");     
    }

纪念品表迁移.php

class CreateSouvenirTable extends Migration
{
    public function up()
    {
        Schema::create('souvenir', function( Blueprint $table )
        {    
            $table->increments('id');
            $table->string('serNumber');
            $table->string('name', 100);
            $table->date('dateReceived');
            $table->text('description');
            $table->string('receivedFrom', 100);
            $table->string('location', 100);
            $table->string('status', 100);
        });
    }

创建.blade.php

<div class="form-group">
<div class="row">
<label class="col-md-3">Series Number</label>
<div class="col-md-6">
        <input type="text" name="serNumber" class="form-control">
                    </div>
                    <div class="clearfix"></div>
                </div>
</div>

意见 - index.blade.php

    <section class="content">
        <div class="container-fluid">
            <p>
                <a href="{{ route('admin.souvenir.create') }}" class="btn btn-primary">Add New Souvenir</a>
            </p>
            <table class="table table-bordered table-striped">
                <tr>
                    <th>ID</th>
                    <th>Series Number</th>
                    <th>Name</th>
                    <th>Date</th>
                    <th>Location</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
                @foreach($souvenir as $s)
                    <tr>
                        <td>{{ $s->id }}</td>
                        <td>{{ $s->serNumber }}</td>
                        <td>{{ $s->name }}</td>
                        <td>{{ $s->dateReceived }}</td>
                        <td>{{ $s->location }}</td>
                        <td>{{ $s->status }}</td>  
                        <td>
                            <a href="{{ route('admin.souvenir.edit', $s->id) }}" class="btn btn-primary btn-block">Edit </a>
                            <a href="{{ route('admin.souvenir.show', $s->id) }}" class="btn btn-success btn-block"> View </a>
                            <a href="javascript:void(0)" onclick="$(this).parent().find('form').submit()" class="btn btn-danger btn-block">Delete</a>
                            <form action="{{ route('admin.souvenir.destroy', $s->id) }}" method="post">
                                <input type="hidden" name="_method" value="DELETE">
                                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            </form>
                        </td>
                    </tr>
                @endforeach
            </table>
        </div>
    </section>

@endsection

标签: phpsqllaravel

解决方案


如果您想更改最后四个区域,请尝试使用

$fourdigitrandom = rand(1000,9999); 

然后与AK连接

$string=  "AK" .$fourdigitrandom;

推荐阅读