Laravel 查询构建器总是提取空数据
Laravel query builder always fetches null data
我对 Laravel 很陌生,尤其是从数据库中检索内容时。在阅读了他们关于查询构建器的文档后,我找到了一个部分,其中显示了示例代码来执行我 need:
If you don't need an entire row, you may extract a single value from a
record using the value method. This method will return the value of
the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
所以,我更改了 tables 和我需要的值:
$loginID = DB::table('logins')->where('email', $email)->value('id');
我目前正在处理注册,当用户注册时,RegisteredUserController.php 运行。但是,它因错误而崩溃:
QLSTATE[HY000]: General error: 1364 Field 'loginID' doesn't have a
default value (SQL: insert into users
(name
, address
, irdNum
,
phone
, updated_at
, created_at
) values (Tessa Jewels, 107a Allum
Street, 222222, 0275502217, 2021-08-21 05:28:37, 2021-08-21 05:28:37))
查看 Stack Overflow 上的其他帖子后,似乎是当 table 不可为 null 而您向其传递 null 值时会发生该错误。
下面是我的 类:
用户迁移文件
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->string('name', 45);
$table->mediumText('address');
$table->string('irdNum');
$table->integer('phone');
$table->bigInteger('loginID')->unsigned()->index();
$table->timestamps();
});
Schema::table('users', function($table) {
$table->foreign('loginID')->references('id')->on('logins')
->onUpdate('cascade')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
登录迁移文件
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLoginsTable extends Migration
{
public function up()
{
Schema::create('logins', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('logins');
}
}
登录模式
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Login extends Authenticatable
{
use HasFactory,Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email',
'password'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
}
用户模型
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'address',
'irdNum',
'phone',
'logInID'
];
}
注册用户控制器
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\Login;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
use Illuminate\Support\Facades\DB;
class RegisteredUserController extends Controller
{
/**
* Display the registration view.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('auth.register');
}
/**
* Handle an incoming registration request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'address' => 'required|string|max:500',
'irdNum' => 'required|string|max:12',
'phone' => 'required|string|max:16',
'email' => 'required|string|email|max:255|unique:logins',
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$login = Login::create([
'email' => $request->email,
'password' => Hash::make($request->password)
]);
event(new Registered($login));
$email = $request->email;
$loginID = DB::table('logins')->where('email', $email)->value('id');
if (is_null($loginID)) {
$loginID = 123;
}
$user = User::create([
'name' => $request->name,
'address' => $request->address,
'irdNum' => $request->irdNum,
'phone' => $request->phone,
'loginID' => $loginID
]);
$user->save();
Auth::login($login);
return redirect(RouteServiceProvider::HOME);
}
}
在这个项目中,登录模型被用来代替常规方式 Laravel 进行身份验证。我知道将其保留为身份验证的默认值会容易得多,但我也想了解事情是如何工作的,这样做教会了我很多东西。
此外,它可以完美地将内容保存到我的数据库中。例如,如果我摆脱了用户代码,只有创建登录名然后注册,我就可以立即通过 PhpMyAdmin 在我的数据库中看到该条目。似乎只是在检索任何内容时遇到问题。
问题说明
$email = $request->email;
$loginID = DB::table('logins')->where('email', $email)->value('id');
请求是从表单发送的,其中一个输入是电子邮件。因此,用户填写他们的详细信息,它会生成一个登录条目,保存他们的电子邮件和密码。之后,我提取它是哪个条目(通过在数据库中搜索与请求电子邮件匹配的登录 table 条目)并检索其 ID。然后在创建用户模型时使用该 ID:
$user = User::create([
'name' => $request->name,
'address' => $request->address,
'irdNum' => $request->irdNum,
'phone' => $request->phone,
'loginID' => $loginID
]);
$user->save();
但是它抱怨 'loginID' => $loginID
行,那是错误弹出的时候。由于我们创建了登录 table 并使用完全相同的变量 ($request->email;
) 将其成功保存到数据库中,因此它应该 return ID 而不是 returns NULL。
我的开发环境设置了 Docker 运行 4 个容器:app(php)、db(mysql)、PhpMyAdmin 和 webserver(nginx) if有帮助。
事实证明 $loginID 具有正确的值,所以我不太确定为什么在尝试使用它创建用户模型时出现错误。我按照评论中的建议通过 dd($loginID);
找到了价值。
在用户模型 $fillable 中,您将 logInID 更改为 loginID
尝试使用此查询:
$loginID = DB::table('logins')->where('email', $email)->first('id');
我对 Laravel 很陌生,尤其是从数据库中检索内容时。在阅读了他们关于查询构建器的文档后,我找到了一个部分,其中显示了示例代码来执行我 need:
If you don't need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
所以,我更改了 tables 和我需要的值:
$loginID = DB::table('logins')->where('email', $email)->value('id');
我目前正在处理注册,当用户注册时,RegisteredUserController.php 运行。但是,它因错误而崩溃:
QLSTATE[HY000]: General error: 1364 Field 'loginID' doesn't have a default value (SQL: insert into
users
(name
,address
,irdNum
,phone
,updated_at
,created_at
) values (Tessa Jewels, 107a Allum Street, 222222, 0275502217, 2021-08-21 05:28:37, 2021-08-21 05:28:37))
查看 Stack Overflow 上的其他帖子后,似乎是当 table 不可为 null 而您向其传递 null 值时会发生该错误。
下面是我的 类:
用户迁移文件
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->string('name', 45);
$table->mediumText('address');
$table->string('irdNum');
$table->integer('phone');
$table->bigInteger('loginID')->unsigned()->index();
$table->timestamps();
});
Schema::table('users', function($table) {
$table->foreign('loginID')->references('id')->on('logins')
->onUpdate('cascade')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
登录迁移文件
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLoginsTable extends Migration
{
public function up()
{
Schema::create('logins', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('logins');
}
}
登录模式
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Login extends Authenticatable
{
use HasFactory,Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email',
'password'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
}
用户模型
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'address',
'irdNum',
'phone',
'logInID'
];
}
注册用户控制器
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\Login;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
use Illuminate\Support\Facades\DB;
class RegisteredUserController extends Controller
{
/**
* Display the registration view.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('auth.register');
}
/**
* Handle an incoming registration request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'address' => 'required|string|max:500',
'irdNum' => 'required|string|max:12',
'phone' => 'required|string|max:16',
'email' => 'required|string|email|max:255|unique:logins',
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$login = Login::create([
'email' => $request->email,
'password' => Hash::make($request->password)
]);
event(new Registered($login));
$email = $request->email;
$loginID = DB::table('logins')->where('email', $email)->value('id');
if (is_null($loginID)) {
$loginID = 123;
}
$user = User::create([
'name' => $request->name,
'address' => $request->address,
'irdNum' => $request->irdNum,
'phone' => $request->phone,
'loginID' => $loginID
]);
$user->save();
Auth::login($login);
return redirect(RouteServiceProvider::HOME);
}
}
在这个项目中,登录模型被用来代替常规方式 Laravel 进行身份验证。我知道将其保留为身份验证的默认值会容易得多,但我也想了解事情是如何工作的,这样做教会了我很多东西。
此外,它可以完美地将内容保存到我的数据库中。例如,如果我摆脱了用户代码,只有创建登录名然后注册,我就可以立即通过 PhpMyAdmin 在我的数据库中看到该条目。似乎只是在检索任何内容时遇到问题。
问题说明
$email = $request->email;
$loginID = DB::table('logins')->where('email', $email)->value('id');
请求是从表单发送的,其中一个输入是电子邮件。因此,用户填写他们的详细信息,它会生成一个登录条目,保存他们的电子邮件和密码。之后,我提取它是哪个条目(通过在数据库中搜索与请求电子邮件匹配的登录 table 条目)并检索其 ID。然后在创建用户模型时使用该 ID:
$user = User::create([
'name' => $request->name,
'address' => $request->address,
'irdNum' => $request->irdNum,
'phone' => $request->phone,
'loginID' => $loginID
]);
$user->save();
但是它抱怨 'loginID' => $loginID
行,那是错误弹出的时候。由于我们创建了登录 table 并使用完全相同的变量 ($request->email;
) 将其成功保存到数据库中,因此它应该 return ID 而不是 returns NULL。
我的开发环境设置了 Docker 运行 4 个容器:app(php)、db(mysql)、PhpMyAdmin 和 webserver(nginx) if有帮助。
事实证明 $loginID 具有正确的值,所以我不太确定为什么在尝试使用它创建用户模型时出现错误。我按照评论中的建议通过 dd($loginID);
找到了价值。
在用户模型 $fillable 中,您将 logInID 更改为 loginID
尝试使用此查询:
$loginID = DB::table('logins')->where('email', $email)->first('id');