所以我有无法解决的问题,我的解决方法如下
用户可以喜欢工作或其他用户,所以我写了一个多态关系one-to-many
,其中用户可以喜欢很多工作或其他用户。
这是表的定义
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('favoritable_id');
$table->unsignedBigInteger('user_id');
$table->string('favoritable_type');
$table->foreign('user_id')
->references('id')
->on('default_users')
->onDelete('cascade');
$table->timestamps();
});
}
一个问题是,每当我尝试喜欢给定的工作时,我总是会收到此错误,
具有id的工作可以正常工作,但是第二个工作不起作用
运行正常
DefaultUser::first()->favoriteJobs()->save(Job::first())
但是这个
DefaultUser::first()->favoriteJobs()->save(Job::find(2))
给出以下错误
Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint
fails (`dwarozh_jobs`.`favorites`, CONSTRAINT `favorites_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `default_users` (`id`) ON DELETE CASCADE) (SQL: inser
t into `favorites` (`favoritable_id`, `favoritable_type`, `user_id`) values (1, App/Models/Job, 2))'
CanFavorite.php
<?php
namespace App\Concerns;
use App\Models\Job;
trait CanFavoriteJobs {
public function favoriteJobs()
{
return $this->morphedByMany(Job::class, 'favoritable', 'favorites', 'favoritable_id', 'user_id');
}
}
谢谢您的回答。
场景:用户可以收藏其他用户和/或工作。
favorites
表格迁移
class CreateFavoritesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->primary(['user_id', 'favoritable_id', 'favoritable_type']);
$table->foreignId('user_id')->constrained();
$table->unsignedBigInteger('favoritable_id');
$table->string('favoritable_type');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('favorites');
}
}
最喜欢的口才模型-获得一些关系功能
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Favorite extends Model
{
//use HasFactory;
public function favoritables()
{
return $this->morphTo('favoritable');
}
}
特性提供标记唱片收藏夹和收藏夹关系的功能。
namespace App;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
trait Favoritable
{
/**
* Get all favorites associated with the model.
*/
public function favorites(): MorphToMany
{
return $this->morphToMany(User::class, 'favoritable', 'favorites')->withTimestamps();
}
/**
* Mark the model record as favorite
*/
public function favorite(?User $user = null): void
{
$user = $user ?: auth()->user();
$this->favorites()->attach($user);
}
}
使用户以及工作模型都使用该特征
use App\Favoritable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Model
{
use HasFactory, Notifiable, Favoritable;
//$fillable, $hidden, $casts etc
public function favoritesForUsers()
{
return $this->hasMany(Favorite::class, 'user_id', 'id')->where('favoritable_type', 'App\Models\User');
}
public function favoritesForJobs()
{
return $this->hasMany(Job::class, 'user_id', 'id')->where('favoritable_type', 'App\Models\Job');
}
}
工作(口才)模型
namespace App\Models;
use App\Favoritable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Job extends Model
{
use HasFactory, Favoritable;
//Model class definition
}
现在将已认证用户-登录用户标记为作业或用户为收藏夹
//Mark the first Job as favorited by currently logged in user
$job = Job::first();
$job->favorite();
//Mark the User with an id of 45 favorited by currently logged in user
$user = User::find(45);
$user->favorite();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句