Muhammad Arslan Muhammad Arslan - 5 months ago 11
MySQL Question

data type for character and numbers in laravel

I want to store character and numbers for column have primary key in database.

I created the table through query in laravel like this

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');
});
}


and set my column datatype string. which shows varchar in database looking fine. When i enter values in form the data stored perfectly like 12abc but when i fetch data from database it shows only 12 without characters.
And the function is

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


and loop is

@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``


What should i do to get proper and complete data. Thanks in advance.

Answer

Just paste this line to your model. So that modal can handle it without auto increment field.

public $incrementing = false;