Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉维尔';s save()不会保存所有字段_Php_Laravel_Eloquent - Fatal编程技术网

Php 拉维尔';s save()不会保存所有字段

Php 拉维尔';s save()不会保存所有字段,php,laravel,eloquent,Php,Laravel,Eloquent,AdvertiseController.php class AdvertiseController extends Controller { ... public function NewAdvertiser() { $advertiser = new Advertiser((string)Uuid::generate(4)); $advertiser->createAdvertiser(); return Redir

AdvertiseController.php

class AdvertiseController extends Controller
{
    ...
    public function NewAdvertiser()
    {
        $advertiser = new Advertiser((string)Uuid::generate(4));
        $advertiser->createAdvertiser();
        return Redirect::route('dashboard');
    }
    ...
}
Advertiser.php

class Advertiser extends Model
{
    ...
    protected $fillable = ['token'];

    private $token;

    function __construct($token)
    {
        $this->token = $token;
    }

    function createAdvertiser()
    {
        //dd($this);
        $this->save();
        //dd($this);
    }
    ...
}
第一个
dd($this)
打印以下内容:

Advertiser {#181 ▼
  #fillable: array:1 [▼
    0 => "token"
  ]
  -token: "44f1e74b-ad19-4e73-ac2e-b37ffde59e99"
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: []
  #original: []
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: false
  +wasRecentlyCreated: false
}
第二个
dd($this)

token
有一个减号,这和它有什么关系吗?我找不到关于它的任何信息

该字段的数据库迁移:

$table->uuid('token');

问题是所有记录都已成功存储,但
标记
字段为空。

您的模型存在一些问题。您只需要填充可填充属性。移除构造函数,因为它移除了正确填充模型的雄辩能力,而这正是问题的根源。也不需要包装save方法。同时,将令牌作为类的属性并不是使其成为模型属性的方法。回答您关于
-
标志的问题,这是因为它被声明为私有财产

这是一个关于模型外观的示例

class Advertiser extends Model
{
    protected $fillable = ['token'];
}
如果确实必须重写构造函数,则应确保它使用属性数组构造父级

class Advertiser extends Model
{
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        // Your code here
    }
}
在这种情况下,控制器需要如下所示

class AdvertiseController extends Controller
{
    public function NewAdvertiser()
    {
        // Create the model with an attributes array
        $advertiser = new Advertiser([
            'token' => (string)Uuid::generate(4)
        ]);

        // Save the model
        $advertiser->save();

        return Redirect::route('dashboard');
    }
}
class AdvertiseController extends Controller
{
    public function NewAdvertiser()
    {
        // Create the model with an attributes array
        $advertiser = new Advertiser([
            'token' => (string)Uuid::generate(4)
        ]);

        // Save the model
        $advertiser->save();

        return Redirect::route('dashboard');
    }
}