【发布时间】:2020-12-13 02:08:29
【问题描述】:
更新:正如有人在 cmets 和答案中建议的那样,它有效。但是当我在保存后尝试在最后写$profile->user 时,它显示为空。有人能猜出原因吗?
我正在使用 laravel 7。我不断收到错误:Illuminate/Database/QueryException with message 'SQLSTATE[HY000]: General error: 1 no such table: profiles (SQL: insert into "profiles" ("title", "description", "user_id", "updated_at", "created_at") values (Cool Title, Description, 1, 2020-08-24 11:58:39, 2020-08-24 11:58:39))'
当我尝试使用这些命令时:
php artisan tinker
Psy Shell v0.10.4 (PHP 7.4.9 — cli) by Justin Hileman
>>> $profile = new \App\Profile();
=> App\Profile {#3184}
>>> $profile->title='Cool Title';
=> "Cool Title"
>>> $profile->description='Description';
=> "Description"
>>> $profile->user_id = 1;
=> 1
>>> $profile->save();
保存后我得到那个错误。
RegisterController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'username' => ['required', 'string', 'max:255', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
2020_08_24_112130_create_user_profiles_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_profiles', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_profiles');
}
}
用户.php
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
function index(Request $req) {
return $req->input();
}
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'email', 'password',
];
/**
* 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',
];
public function userprofile() {
return $this->hasOne(UserProfiles::class);
}
}
如果需要更多代码,请告诉我,我会更新我的问题。这是我在开头提到的唯一错误。
【问题讨论】:
-
表叫user_profiles?如果是这样,您应该更改您的配置文件模型以引用此表添加受保护的 $table = 'user_profile';到你的模型
-
它正在寻找
profiles表,但你创建了user_profiles表,所以你可以通过添加protected $table = 'user_profile'来强制laravel 查找profiles表;在App\Profile模型中添加到您的模型 -
@Abdel-azizhassan 你能告诉我在哪里添加这个以及在什么类?
-
查看您的
\App\Profile课程。默认情况下,Laravel 将使用类的名称并向其添加s以查找表。所以Profile正在寻找表profiles。如果这不是表的名称,则必须按照 Kamlesh Paul 的说明指定它。 -
正如@aynber 所说,您将查看 Profile 类并在可填充数组之前添加此行 protected $table = 'user_profiles';
标签: php mysql sql database laravel