mysql - laravel中字符和数字的数据类型

标签 mysql laravel

我想在数据库中存储具有主键的列的字符和数字。

我像这样在laravel中通过查询创建了表

  public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            //   $table->increments('id');
            $table->engine = 'InnoDB';

            $table->string('roll_no');
            $table->string('name');
            $table->string('father_name');
            $table->string('address');
            $table->string('cnic')->unique();
            $table->string('phone_no');
            $table->string('father_phone_no');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('dept_id');
            $table->rememberToken();
            $table->timestamps();

            $table->primary('roll_no');
            $table->foreign('dept_id')->references('dept_id')->on('departments')->onDelete('cascade');
        });
    }

并设置我的列数据类型字符串。这显示数据库中的 varchar 看起来不错。当我在表格中输入值时,数据存储得非常像 12abc 但是当我从数据库中获取数据时它只显示 12 没有字符。 而函数是

   public function showStudents()
    {
        $allStudents = Student::all();
//        $user = Auth::user();
        return view ('admin.students.showStudents', compact('allStudents'));
    }

循环是

  @foreach($allStudents as $student)
                                <tr>
                                    <th scope="row">{{ $student->roll_no }}</th>
                                    <td><a href="{{ action('AdminController@singleStudent', [$student->roll_no])}}">{{ $student->name }}</a></td>
                                    <td>{{ $student->address }}</td>
                                    <td>{{ $student->phone_no }}</td>
                                    <td>{{ $student->email }}</td>
                                    <td>{{ $student->dept_id }}</td>
                                    <td><a href="{{ action('AdminController@editStudents', [$student->roll_no])}}" class="edit">Edit</a>  <a href="{{ action('AdminController@deleteStudent', [$student->roll_no])}}" class="delete">Delete</a> </td>
                                </tr>
                            @endforeach``

我应该怎么做才能获得正确和完整的数据。提前致谢。

最佳答案

只需将此行粘贴到您的模型中。这样模式就可以在没有自动增量字段的情况下处理它。

public $incrementing = false;

关于mysql - laravel中字符和数字的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915968/

相关文章:

php - Laravel 6 PHPUnit 测试 - 找不到驱动程序(SQL : PRAGMA foreign_keys = ON;))

php - 基于组的自动增量

php - 按正确顺序列出 2 个用户之间的对话

Laravel 8.x,3 个模型和多对多关系

php - 搜索 Laravel 后记住下拉选择选择

php - PHP 列出类别和子类别的逻辑

php - 在数据库中自动创建数据

mysql - 如何更新发送日期早于聊天开始的聊天消息?

PHP MYSQL 多个下拉菜单选择更改值

php - 显示选择数据时出现问题