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

时间:2016-06-20 06:36:16

标签: 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``

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

2 个答案:

答案 0 :(得分:1)

在您的模型上,您应该告诉它您的primary_key不是自动增量字段。

protected $primaryKey = 'roll_no';
public $incrementing = false;

应该避免强制转换为整数。

答案 1 :(得分:1)

只需将此行粘贴到您的模型即可。因此模态可以在没有自动增量字段的情况下处理它。

public $incrementing = false;