【问题标题】:Why I am getting Laravel error no such table while using php artisan tinker为什么我在使用 php artisan tinker 时收到 Laravel 错误 no such table
【发布时间】: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


【解决方案1】:

我不知道您是否在数据库中创建了user_profiles 表,但您可以在修补程序中执行以下操作:

$profile = new \App\UserProfiles();

如果模型已经创建

【讨论】:

  • 但是如果我写 $profile-&gt;user 来检查它是否显示为空
  • 你应该已经定义了两个模型的关系
  • 如果它对您有所帮助,请点赞或接受它,这样如果其他人有相同的查询,它也对他们有用
  • 你能帮我解决其他问题吗?正如我单击保存和用户 $profile->user 后显示的 null 一样。一旦我得到答案,我会赞成并接受它,因为在此之后它仍然给出错误
  • 你有字段 $profile->user 吗?还是有关系?
【解决方案2】:

您必须迁移表(如果您还没有创建表,让我们创建一个。):

php artisan migrate

重复你的代码后:

$profile = new \App\Profile();

$profile->title='Cool Title';

$profile->description='Description';

$profile->user_id = 1;

$profile->save();

如果您使用 Laravel 8,请使用:

    $profile = new App\Models\Profile();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 2020-11-01
    • 2016-04-20
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多