John Howard

1645 经验值

我创建了三个数据库
posts
users
post_classes

posts表里有user_id 和 post_class_id 字段

PostClass的模型是这样的:

    public function posts()
    {
        return $this->hasMany(Post::class);
    }

模型在定义的时候为什么需要这样设置外键才能查询?

class Post extends Model
{
    public function postclass()
        {
            return $this->belongsTo(PostClass::class,'post_class_id','id');
        }
}

而用户的就不需要,可以直接这样:

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

快捷键设置里,右边点搜索,按Shift+ mac + / ,搜到你的这个快捷键,然后取消Help Search 的设置就可以了

哦,好的,非常感谢,我自己搭建的环境,手动把引擎改为InnoDB会不会有什么影响?

在Homestead主机上新建数据库,执行php artisan migrate命令后
新建的三个表的引擎为InnoDB,但是我不用homestead主机,使用我自己搭建的PHP环境运行命令后,会显示MyISAM的引擎
请问这两种引擎有什么不一样吗?

Jelly,这个问题我找到原因了,现在访问homestead.dev:8000 端口是可以访问的
问题是如何不用8000端口,直接访问呢?

这就是我想要的答案,但是有第二层数组怎么解决?取第一层数组的NAME和第二层数组的AGE,在前台循环显示

代码如下:

$relation = Relation::where('mid',$id)->with('members')->get();

dd($relation);

array:2 [▼
  0 => array:7 [▼
    "id" => 1
    "mid" => 1
    "rid" => 2
    "name" => "兄弟"
    "created_at" => "2017-01-12 00:10:45"
    "updated_at" => "2017-01-12 00:10:45"
    "members" => array:1 [▼
      0 => array:12 [▼
        "id" => 2
        "name" => "张三"
        "avatar" => "images/avatar/default.png"
        "mobnumber" => "13800138000"
        "address" => "XXX省XXX市XX县"
        "created_at" => "2017-01-09 01:38:20"
        "updated_at" => "2017-01-09 01:38:23"
        "streaming" => "F"
        "pivot" => array:2 [▶]
      ]
    ]
  ]
  1 => array:7 [▼
    "id" => 3
    "mid" => 1
    "rid" => 10
    "name" => "表兄弟"
    "created_at" => "2017-01-12 00:22:20"
    "updated_at" => "2017-01-12 00:22:20"
    "members" => array:1 [▼
      0 => array:12 [▼
        "id" => 10
        "name" => "李四"
        "avatar" => "images/avatar/20170109090138-wXCL2.png"
        "mobnumber" => "13800138000"
        "address" => "XXX省XXX市"
        "created_at" => "2017-01-09 09:26:38"
        "updated_at" => "2017-01-09 09:26:38"
        "streaming" => "F"
        "pivot" => array:2 [▶]
      ]
    ]
  ]
]

可以看到所有的数据,我想在blade模板里调用第一级的name和members里的name字段,

@foreach($relation as $r)
    <p> $r->name : $r->members->name </p>
 @endforeach

怎么会提示错误呢?
我想显示为

兄弟:张三
表兄弟:李四

感谢回答!解决了,是因为没有运行:php artisan config:clear