小编Pet*_*ter的帖子

在架构构建器中为枚举类型字段添加默认值

我正在使用以下方法ENUM在架构生成器中创建类型的数据库列:

$table->enum('status', array('new', 'active', 'disabled'));
Run Code Online (Sandbox Code Playgroud)

我想将它的默认值设置为active.
我试着这样做:

$table->enum('status', array('new', 'active', 'disabled'))->default('active');
Run Code Online (Sandbox Code Playgroud)

但是你可以猜测它没有设置它的默认值.我正在使用MySQL数据库,如果这很重要的话.

mysql enums database-schema laravel laravel-4

7
推荐指数
2
解决办法
1万
查看次数

Laravel 获取二级关系

我有三个数据库表:

+------+-----------+---------------------+
| user | user_type | user_type_relations |
+------+-----------+---------------------+
Run Code Online (Sandbox Code Playgroud)

每个用户可以有多种类型,但一种用户类型只能有一个用户。为了存储这种关系,我使用了第三个关系表,其结构如下:

+---------------------+
| user_type_relations |
+---------------------+
| id                  |
| user_id             |
| user_type_id        |
+---------------------+
Run Code Online (Sandbox Code Playgroud)

我已经定义了我的模型中的关系,如下所示:

User 模型:

public function userTypeRelations()
    {
        return $this->hasMany('UserTypeRelations', 'user_id', 'id');
    }
Run Code Online (Sandbox Code Playgroud)

UserType 模型:

public function userTypeRelation()
    {
        return $this->hasMany('UserTypeRelations', 'user_type_id', 'id');
    }
Run Code Online (Sandbox Code Playgroud)

UserTypeRelations 模型:

 public function user()
    {
        return $this->hasMany('User', 'id', 'user_id');
    }

    public function userType()
    {
        return $this->hasMany('UserType', 'id', 'user_type_id');
    }
Run Code Online (Sandbox Code Playgroud)

这就是我在将其传递给视图之前尝试访问控制器中特定用户的用户类型的方式:

$users = User::with('userTypeRelations')->with('userType')->orderBy($order)->where('status', 'active')->paginate(10);
Run Code Online (Sandbox Code Playgroud)

我以为首先我会得到关系表的值,然后我会很容易地得到每个用户的用户类型,但我收到以下错误:

BadMethodCallException

Call to undefined method …
Run Code Online (Sandbox Code Playgroud)

php model-view-controller orm database-relations laravel

4
推荐指数
1
解决办法
4408
查看次数

Laravel全文搜索

我正在尝试对数据库实施全文搜索查询.这是我的客户发给我的规范:

"The free text search limits the result of the data table to records with a matching first
name, last name, country, city, state, or zip code. If several words are input,
each word must match one of the columns for a record to be visible."
Run Code Online (Sandbox Code Playgroud)

我在我的控制器中制作了一些非常丑陋的意大利面条代码,试试它是否有效:

public function search($searchTerms){
        $searchTerms = explode(' ', $searchTerms);
        $results = array();
        foreach ($searchTerms as $searchTerm) {
            if (!People::where('firstname', 'LIKE', '%'.$searchTerm.'%')->get()->isEmpty()) {
                array_push($results, People::where('firstname', 'LIKE', '%'.$searchTerm.'%')->get());
            }
            else if (!People::where('lastname', 'LIKE', '%'.$searchTerm.'%')->get()->isEmpty()) …
Run Code Online (Sandbox Code Playgroud)

php database search laravel

3
推荐指数
1
解决办法
5112
查看次数