在 Laravel 11 应用程序中,我有
app/Models/News.php
模型引用:
class News extends Model implements Feedable
{
protected $primaryKey = 'id';
public $timestamps = false;
protected $table = 'news';
protected $casts = [ 'created_at' => 'datetime', 'updated_at' => 'datetime', 'published_at' => 'datetime' ];
protected static function boot()
{
parent::boot();
if(App::isLocal()) {
Model::shouldBeStrict(); // ERROR AFTER I ADDED THIS LINE
}
static::deleting(function ($news) {
if (Storage::exists($news->image)) {
Storage::delete($news->image);
}
});
}
public function userReactions(): HasMany
{
return $this->hasMany(Reaction::class, 'model_id', 'id');
}
并使用 userReactions 运行请求:
$this->newsList = News
::orderBy($orderByField, $orderByFieldDirection)
->orderBy($orderByField2, $orderByFieldDirection2)
->with('creator:id,name,slug')
->with('newsCategory:id,name,uploaded_file')
->with('userReactions')
->with('tags')
->getByPublished(NewsPublishedEnum::PUBLISHED)
->getByCompanyId($this->filterCompanyId)
->getByIsHomepage($this->onlyIsHomepage)
->getByTitle($this->searchTitle, true)
->select('id', 'news_category_id', 'title', 'content', 'slug', 'image', 'published', 'created_at', 'creator_id')
->paginate(ConfigValueEnum::get(ConfigValueEnum::NEWS_PER_PAGE))
->through(function ($newsItem) {
$newsItem->image = (new CheckModelImage())->get($newsItem->image, get_class($newsItem));
return $newsItem;
});
如果我在模型启动方法中添加带有
Model::shouldBeStrict();
的行,则会出现错误:
The attribute [id] either does not exist or was not retrieved for model [App\Models\Reaction].
表反应定义为:
Schema::create('reactions', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->nullable()->references('id')->on('users')->onDelete('CASCADE');
$table->morphs('model');
$table->unsignedTinyInteger('action');
$table->timestamp('created_at')->useCurrent();
$table->unique(['model_type', 'model_id', 'user_id', 'action'], 'reactions_4fields_unique');
$table->index(['model_type', 'model_id', 'action', 'created_at'], 'reactions_4fields_index');
$table->index(['created_at', 'model_type', 'model_id'], 'reactions_3fields_index');
如果没有添加shouldBeStrict,我就没有这样的错误,一切正常:
在应用程序/模型/Reaction.php中:
<?php
namespace App\Models;
use App\Enums\ReactionActionEnum;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\App;
use Spatie\Tags\HasTags;
class Reaction extends Model
{
protected $table = 'reactions';
public $timestamps = false;
protected $primaryKey = 'id';
protected static function boot()
{
parent::boot();
// if(App::isLocal()) {
// Model::shouldBeStrict();
// }
}
public function scopeGetByModelId($query, string $modelId = null)
{
if ( ! empty($modelId)) {
$query->where($this->table . '.model_id', $modelId);
}
return $query;
}
public function scopeGetByModelType($query, string $modelType = null)
{
if ( ! empty($modelType)) {
$query->where($this->table . '.model_type', $modelType);
}
return $query;
}
public function news(): BelongsTo
{
return $this->belongsTo(News::class, 'model_id', 'id');
}
出了什么问题?
您必须将
$fillable
或 $guadrd
属性添加到您的模型中。