【问题标题】:Larvel Eloquent Model save function is not saving but not errorLaravel Eloquent 模型保存功能不保存但不报错
【发布时间】:2018-11-04 13:14:45
【问题描述】:

我目前的问题是我想将一些值保存到数据库中,但没有保存,我也没有收到错误..

$product-save();$product-update(array(...)) 都不起作用,我不知道为什么。我的 ASIN 模型看起来不错,并且填充了正确的可填充属性...

你们知道为什么它不起作用吗?

我的 Laravel 版本:Laravel Framework 5.5.36

到目前为止,这是我的课:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\ASIN;

class CheckPrice extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'post:CheckPrice';

    /**
     * Create a new command instance.
     *
     * @return void
     */

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {

        $product = ASIN::find(1410);

        $product->price = "HELLO";
        $product->amountSaved = "HELLO";
        $product->percentageSaved = "HELLO";
        $product->url = "HELLO";
        $product->imgUrl = "HELLO";
        $product->save();

        //$product->update(array("price" => "HELLO", "amountSaved" => "HELLO", "percentageSaved" => "HELLO", "url" => "HELLO", "imgUrl" => "HELLO"));

        $this->printProduct(ASIN::find(1410));
    }

到目前为止我的 ASIN 型号: 命名空间应用程序;

use Illuminate\Database\Eloquent\Model;

class ASIN extends Model
{
    protected $connection = 'facebook_amazon';
    public $table = "ASINS";

    protected $fillable = [
        'ASIN',
        'price',
        'amountSaved',
        'percentageSaved',
        'category',
        'url',
        'imgUrl',
        'showApp'
    ];
}

诚挚的问候和感谢!

【问题讨论】:

  • 您是否通过 validate 函数验证数据?
  • 可能有拼写错误,可能与表格列不匹配。
  • 我没有验证数据并且列名是正确的。我已经复制并粘贴了它们
  • try catch块内调用save函数,看看你的数据库连接是否有问题
  • 效果不佳...但我确实有连接,否则我无法将产品属性打印到控制台!

标签: mysql laravel laravel-5 eloquent


【解决方案1】:

在句柄方法中使用这个

$product = App\ASIN::find(1410);

或者在导入 ASIN 模型时,如果您想保持句柄方法相同,请使用它

use App\ASIN as ASIN;

【讨论】:

  • 您的 find() 方法中的参数是否是 ANSI 模型中主键的任何值。首先检查模型是否被检索到。
  • 它也在检索第一名!
  • 是的!它有两列!
  • 试试这个 $product->fill(['filed_1' => 'value' , 'field_2' => 'value']);
  • 将列名从“ID”更改为“id”。您的 id 列大写或在模型中定义它像这样 protected $primaryKey = 'ID'; laravel 假设你的主键是“id”
【解决方案2】:

使用 Laravel 日志:

if(!$product->save()){
foreach($products->errors() as $error){ 
Log::info($error);
}
}

希望对您有所帮助。

【讨论】:

  • 您确定您的命令正在运行吗?尝试在句柄函数开头添加Log。
  • 是的,它肯定在运行!!
  • 还要确保你的连接是正确的,并且你没有将数据写入错误的数据库
  • 您可以检索数据吗? $product = ASIN::find(1410); dd($产品);尝试使用 forceSave 方法
猜你喜欢
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 2015-09-06
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 2021-02-02
相关资源
最近更新 更多