eloquent 相关问题

Eloquent是Laravel自己的对象关系映射器。它被命名为“Eloquent”,因为它允许您使用雄辩和富有表现力的语法来处理数据库对象和关系。

Laravel user_id 没有被引用为外键

我正在尝试为我的项目中的配置文件创建地址。 一个用户有多个地址。 用户模型 公共函数地址(){ 返回 $this->hasMany(Address::class); } 地址...

回答 1 投票 0

Laravel 多态关系在 Laravel Nova 中不起作用

我有以下模型和关系。 应用程序\订阅者.php 我有以下模型和关系。 应用程序\订阅者.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Grosv\LaravelPasswordlessLogin\Traits\PasswordlessLogin; class Subscriber extends Authenticatable { use Notifiable, PasswordlessLogin; public function customFieldValues() { return $this->morphMany(CustomFieldValue::class, 'customFieldValueable'); } } 应用程序\CustomField.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use App\Models\Campaign; class CustomField extends Model { protected $fillable = [ 'name', 'key', 'type', 'field_options' ]; public function customFieldValues() { return $this->hasMany(CustomFieldValue::class); } } 应用程序\CustomFieldValues.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class CustomFieldValue extends Model { use HasFactory; protected $fillable = ['custom_field_id', 'value', 'custom_field_valueable_id', 'custom_field_valueable_type']; public function customField() { return $this->belongsTo(CustomField::class); } public function customFieldValueable() { // If you are using custom column names, specify them here. Otherwise, ensure they match the migration. return $this->morphTo(null, 'custom_field_valueable_type', 'custom_field_valueable_id'); } } 应用\Campaign.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use App\Models\CustomField; class Campaign extends Model { protected $dates = [ 'imported_at' ]; protected $casts = [ 'imported_at' => 'datetime', 'exported_at' => 'datetime' ]; protected $fillable = [ 'uuid', 'name', 'slug', 'client_id' ]; public function customFieldValues() { return $this->morphMany(CustomFieldValue::class, 'customFieldValueable'); } } 应用\Newsletter.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Newsletter extends Model { protected $fillable = [ 'uuid', 'name', 'brand_id', 'active', 'create_send_list_id', 'last_imported' ]; public function brand() { return $this->belongsTo(Brand::class); } public function subscribers() { return $this ->belongsToMany(Subscriber::class) ->withPivot('active') ->withTimestamps(); } public function customFieldValues() { return $this->morphMany(CustomFieldValue::class, 'customFieldValueable'); } } 我创建表来支持这一点的迁移看起来像这样, <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('custom_field_values', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('custom_field_id'); $table->string('value'); // Manually create the polymorphic columns $table->unsignedBigInteger('custom_field_valueable_id'); $table->string('custom_field_valueable_type'); // Manually specify a shorter index name $table->index(['custom_field_valueable_type', 'custom_field_valueable_id'], 'custom_field_valueable_index'); $table->timestamps(); $table->foreign('custom_field_id')->references('id')->on('custom_fields')->onDelete('cascade'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('custom_field_values'); } }; 但是,在 Nova 界面中,当我尝试查看订阅者时,出现以下错误, SQLSTATE[42S22]:未找到列:1054“where 子句”中的未知列“custom_field_values.customFieldValueable_type”(连接:mysql,SQL:从custom_field_values中选择*,其中custom_field_values。customFieldValueable_type = App\Models\Subscriber且 custom_field_values.customFieldValueable_id = 1016 且 custom_field_values.customFieldValueable_id 不为空,按 custom_field_values.id desc limit 6 offset 0) 如果有人可以向我解释我哪里出了问题,那就太好了。我对多态关系非常陌生。 laravel 查询正在寻找列 customFieldValueable_type 但您的数据库中有一个 custom_field_valueable_type 。 如果您重新使用标准命名约定,它应该可以正常工作。 错误似乎来自于函数中的App\CustomFieldValues.php: public function customFieldValueable() { // If you are using custom column names, specify them here. Otherwise, ensure they match the migration. return $this->morphTo(null, 'custom_field_valueable_type', 'custom_field_valueable_id'); } 正确的命名约定可以在这里在 laravel 文档中找到

回答 1 投票 0

如何使用 Eloquent 获取最多重复的值?

我在 Eloquent 中具有多对多关系(帖子和标签)。是否可以对数据透视表中的标签进行排序以获得 10 个最常用的标签?

回答 2 投票 0

Laravel 查询包含 `where isn't ( select ....)`

我有一个多对多,并尝试通过两个条件查询数据。 第一个验证表 station 中的 station_code,第二个验证表 Products 中的列名称中的类似条件 $

回答 1 投票 0

Laravel 模型在没有变化时更新

这是一个交叉帖子,因为我希望我能在这里得到一些有用的答案。链接到原始帖子:https://laracasts.com/discuss/channels/eloquent/model-updated-despite-no-changes-done 模特的

回答 1 投票 0

使用 eloquent 转义 LIKE SQL 查询中的通配符

我想阻止这个雄辩的查询中的标准 SQL 下划线匹配: $data = DB::table('项目') -> 不同的() ->选择('项目名称') ->where('item_name', 'LIKE', $term . '...

回答 2 投票 0

关系中的谁到特定行

针对这个问题,我降低了数据库的复杂度。 我有一张有订单的桌子 ID 姓名 还有一个包含子订单的表 ID 订单号 用户身份 一个订单可能包含多个子订单。一个亚目...

回答 1 投票 0

Laravel Eloquent:如何在数据透视表上使用逻辑分组

我的应用程序有用户和组。它们与数据透视表相关。数据透视表具有可为空的“连接”和“左”日期时间。 如何在数据透视表上使用逻辑分组...

回答 1 投票 0

在 foreach 循环内按开始时间对数据进行排序

我想对 foreach 循环内的开始时间数据进行排序。 这是我的控制器代码: 私有函数 getPatientList($search = null) { 返回预约::select('id', 'hospital_id', '

回答 1 投票 0

Laravel:使用带有 IN 子句的 whereRaw 查询时出错

(第一次提问) 我尝试在 laravel 中运行以下查询 $cats = ['x1', 'x2']; $pars = ['Par1' => 'd1', 'Par2' => $cats]; $结果 = Projecto::whereRaw('( (ESTADO = :Par1 )...

回答 1 投票 0

有很多通过,但包括中间表中的属性

使用 Laravel 文档中的示例: https://laravel.com/docs/10.x/eloquent-relationships#has-many-through 项目 id - 整数 名称 - 字符串 环境 id - 整数 项目...

回答 1 投票 0

如何修复 laravel 8 中的“尝试获取非对象的属性‘标题’”错误

背景: 我安装了新的 Laravel 版本 8.49.2,并从(遗留的 Laravel v5.8 项目)中移动了我的应用程序逻辑(控制器、路由、视图、中间件、模型、自定义配置),一切正常...

回答 2 投票 0

在 Laravel 中使用 ids 数组获取相关数据。有更好的方法吗?

在 CustomerDashboard 的 station_ids 列中,我存储了一个 id 数组。 我不知道如何在模型中为此添加一个 hasMany() 关系 $dashboards = CustomDashboard::get(); foreach($仪表板...

回答 1 投票 0

laravel withCount 仅显示 id 值,而不显示计数值

我正在尝试使用 Laravel 8 (Eloquent)从相关表中获取匹配记录的计数值。这个想法只是计算分配给每个用户的任务数量。 我正在尝试确定...

回答 1 投票 0

laravel 自动将整数转换为字符串

我有一个带有移动 API 的 Laravel 5.6 Web 应用程序。 问题是 Laravel 自动将所有整数和布尔值转换为字符串。 我想知道有什么办法可以解决吗? 有趣的是

回答 2 投票 0

如何为所有模型而不是单独实现雄辩的“保存”事件

根据laravel雄辩事件的文档,所有雄辩事件都是根据每个模型单独触发的,有没有办法使用“创建”事件或任何其他雄辩事件

回答 3 投票 0

Laravel 雄辩地根据表中的值获取记录

所以我有一个任务表,其中有一个时间字段和一个“分钟”字段。我只想根据时间和分钟字段获取记录,如下所示: $tasks = Task::whereTime('时间', ...

回答 1 投票 0

Laravel 禁止更新在另一个表中引用的行

说表A和表B(表B有一个外键a_id) 我希望能够更新 A 中的行,只要它不是 B 中的引用 我已经做到了,但我正在寻找更好的方法。 我的方法 $

回答 1 投票 0

如何在 Laravel 中使用 Eloquent 查询构建器来获取 Join 中相关记录的计数

我正在尝试使用 Laravel 8 (Eloquent)使用 count(..) 从相关表中获取匹配记录的计数值。这个想法只是计算分配给每个用户的任务数量。 我

回答 1 投票 0

Laravel - 如何获取关系计数大于列值的位置

我的模型设置如下: 一个项目有一个项目库 公共函数project_gallery(){ 返回 $this->hasOne(ProjectGallery::class); } 一个项目图库可以有许多图库图像

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.