简体   繁体   中英

Laravel - Data is not Fetch for some table

I try to display data in a form. Some table are successfully fetch but some table is not. I try to 'dd' and it turns out the connection and table name is not there.

But it can fetch to whole table like this:

在此处输入图像描述

When I click edit button, no data is not fetch in the form like this:

在此处输入图像描述

and I check the URL, it is correct:

在此处输入图像描述

My controller:

public function edit(StudentApplication $student_applications)
{
    dd($student_applications);
    return view('student_application.edit', compact('student_applications'));
}

My Model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class StudentApplication extends Model

{
use Notifiable;

 /**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

}

My View

<label class="form-control-label" for="student_name">{{ __('Student Name') }}</label>
<input type="text" name="student_name" id="student_name" class="form-control{{ $errors->has('student_name') ? ' is-invalid' : '' }}" placeholder="{{ __('Student Name') }}" value="{{ old('student_name', $student_applications->student_name) }}"  required>

My dd result:

在此处输入图像描述

You can see that the connection, table and attributes are null and no data.

I did the exact same code with other module, and it works like this:

在此处输入图像描述

My Migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentApplicationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('student_applications', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('student_name');
            $table->bigInteger('student_ic_number');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('address');
            $table->string('guardian_name');
            $table->string('guardian_ic_number');
            $table->string('phone_number');
            $table->string('relationship');
            $table->rememberToken();
            $table->timestamps();
        });
    }

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('student_applications');
}

}

My Route: I put two route here, the user route is working like I show in the picture above, the studentapplication route is not working when I try to edit.

Route::resource('user', 'UserController', ['except' => ['show']]);
Route::resource('studentapplication', 'StudentApplicationController', ['except' => ['show']]);

Do you guys have a solution for my problem?

Your route looks incorrect.

Try this instead:

Route::resource('student-applications', 'StudentApplicationController', ['except' => ['show']]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM