Ich benötige einen Download-Verlauf für jeden Benutzer, der unser System nutzt (im Grunde ein System, mit dem Benutzer Social-Media-Beiträge herunterladen können). Ich habe ein „DownloadHistory“-Modell erstellt, das die Felder ID, timestamp, download_history und user_id enthält. Wenn ich einen Benutzer erstelle, sollte automatisch eine Instanz von DownloadHistory erstellt und die Benutzertabelle aktualisiert werden, damit das Feld download_history_id die neu erstellte DownloadHistory-ID enthält, wie in meinem RegisterController angezeigt:
$downloadHistory = DownloadHistory::create([
'user_id' => $user->id
]);
DB::table('users')->where('id', $user->id)->update(['download_history_id' => $downloadHistory->id]);
Das Problem ist: Ich erhalte eine Fehlermeldung, die für mich keinen Sinn ergibt: Der Grund dafür, dass es für mich keinen Sinn ergibt, ist, dass ich nie
download_history erstellt habe, aber ich habe download_history erstellt. Was ist das also? ? ? Mein DownloadHistory-Modell ist:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class DownloadHistory extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
];
protected $casts = [
'downloads' => 'array',
];
public function user()
{
return $this->belongsTo(User::class);
}
}
Meine Migration zum Erstellen der Tabelle ist:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDownloadHistoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('download_history', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->json('history');
$table->integer('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('download_history');
}
} 2 antwortet
在模型中添加 protected $table = 'download_history';
'array',
];
public function user()
{
return $this->belongsTo(User::class);
}
} 在 Laravel 中,表名称假定为复数。
参考:https://laravel.com/docs/9.x /eloquent#表名
要修复此问题,请在模型中使用 $table 属性。
protected $table = 'download_history';
Hot-Tools-Tags
Hot Questions
Hot Tools
Sammlung der Laufzeitbibliothek vc9-vc14 (32+64 Bit) (Link unten)
Laden Sie die Sammlung der Laufzeitbibliotheken herunter, die für die Installation von phpStudy erforderlich sind
VC9 32-Bit
VC9 32-Bit-Laufzeitbibliothek für die integrierte Installationsumgebung von phpstudy
Vollversion der PHP-Programmierer-Toolbox
Programmer Toolbox v1.0 PHP Integrierte Umgebung
VC11 32-Bit
VC11 32-Bit-Laufzeitbibliothek für die integrierte Installationsumgebung von phpstudy
SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen





