dsxmwin86342 2018-06-26 22:13
浏览 18

关系一对多它不起作用

https://imgur.com/a/ob9rjIz There are two tables one called user and another called user_relation_user My relation is an user to many user_relation_user and in my migration. I want to create 10 user with php artisan tinker so i run factory(App\User::class, 10)->create(); at the end i access to my database so do select * from users there are 10 users but in user_relation_user isn't 10 id or it's empty

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Administrator extends Model
    {

        protected $table = 'user_relation_user';
        protected $primaryKey  = 'id';
        protected $fillable = ['user_id'];


        public function users(){

            return $this->belongsTo(User::class,'user_id');
        }
    }



    <?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function administrator(){
        return $this->hasMany(Administrator::class,'user_id');
    }
}
//hasMany

My migration

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserRelationUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_relation_user', function (Blueprint $table) {

            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('user_id_admin')->unsigned();
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *foreign
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_relation_user');
    }
}
  • 写回答

1条回答 默认 最新

  • donglankui1263 2018-06-26 22:22
    关注

    The relation you define is correct,you just need to define a relation in user model as well.

    Example:

    Suppose you are developing a blog system where user can post blogs. then there will be two models user model and blog model

    In User model you have to define user relation as below:

    public function blogs()
    {
       return $this->hasmany(Blog::class);
    }
    

    Then in blogs model

    public function users()
    {
      return $this->belongsTo(User::class);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行