【问题标题】:Laravel model.save() dying silently without exceptionLaravel model.save() 无一例外地默默死去
【发布时间】:2019-08-09 02:32:54
【问题描述】:

我是 Laravel 新手,但曾使用过其他语言的 ORM。
新创建的表之一上的保存方法失败。
没有例外,我知道它失败了,因为保存后的回声没有显示。
../app/storage/logs/laravel.log 中的 laravel 日志为空(即使出现错误也从未显示任何内容)。
我如何找出问题所在?

            try
            {
                $p1 = $this->postsSharedWithMe->newInstance();
                $p1->shared_with_user_id = $sharedID;
                $p1->post_id = $post->id;
                echo '$p1->shared_with_user_id : ', $p1->shared_with_user_id, '<br></br>';
                echo '$p1->post_id : ', $p1->post_id, '<br></br>';
                echo 'Now saving : ', $sharedID, '<br></br>';
                $r1 = $p1->save();
                echo 'Result of saving : ', $r1, '<br></br>'; // *** THIS NEVER PRINTS
            }
            catch(Exception $e)
            {
                // THIS EXCEPTION NEVER PRINTS EITHER
                echo '**** Caught exception: ',  $e->getMessage(), "<br></br>";
            }

try/catch 执行后没有代码,因此肯定会失败。
模型很简单:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;

/**
*
*@author : xxxx
*/
class PostsSharedWithMe extends Model
{
    use PresentableTrait;

    protected $table = "posts_shared_with";

    protected $presenter = "App\\Presenters\\PostsSharedWithPresenter";

    public function fromPost()
    {
        return $this->belongsTo('App\\Models\\Post', 'post_id');
    }

    public function fromUser()
    {
        return $this->belongsTo('App\\Models\\User', 'shared_with_user_id');
    }
}

添加 var_dump($p1)

的转储
object(App\Models\PostsSharedWithMe)[1085]
  protected 'table' => string 'posts_shared_with' (length=17)
  protected 'connection' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'perPage' => int 15
  public 'incrementing' => boolean true
  public 'timestamps' => boolean true
  protected 'attributes' => 
    array (size=2)
      'shared_with_user_id' => string '3' (length=1)
      'post_id' => int 46
  protected 'original' => 
    array (size=0)
      empty
  protected 'relations' => 
    array (size=0)
      empty
  protected 'hidden' => 
    array (size=0)
      empty
  protected 'visible' => 
    array (size=0)
      empty
  protected 'appends' => 
    array (size=0)
      empty
  protected 'fillable' => 
    array (size=0)
      empty
  protected 'guarded' => 
    array (size=1)
      0 => string '*' (length=1)
  protected 'dates' => 
    array (size=0)
      empty
  protected 'touches' => 
    array (size=0)
      empty
  protected 'observables' => 
    array (size=0)
      empty
  protected 'with' => 
    array (size=0)
      empty
  protected 'morphClass' => null
  public 'exists' => boolean false
  protected 'softDelete' => boolean false

【问题讨论】:

标签: php laravel eloquent


【解决方案1】:

方法 save() 不工作时的选项:

  • 启用调试模式(如果尚未启用)
  • 检查该表的迁移并检查模型中的表名
  • 如果没有额外的设置,检查变量名和列名

【讨论】:

    【解决方案2】:

    您使用的是p1.save(),它应该是p1-&gt;save()。

    【讨论】:

    • 我刚刚意识到并将其更改为那个,但这并没有帮助。
    • 另外,请注意您使用逗号而不是句点来连接。
    • 我去检查了 Model.php 里面的 $query 是空的 "$saved = $this->performInsert($query);"当我创建新表时,我是否错过了做某事?
    • @PlanetUnknown:发布 var_dump($p1)。您应该在 PostsSharedWithMe 模型中定义了 shared_with_user_id 和 post_id。此外,您必须使用模型中的变量进行迁移,否则 save() 将不起作用。
    • @PlanetUnknown:我假设您缺少模型中具有相同列名和变量名的迁移(创建表)。调试模式或任何错误报告处于活动状态?
    【解决方案3】:

    我遇到了这个问题:我会保存一个新模型,它会存储,但不会返回。

    我的问题:我有一个带有 Created 方法的模型观察者。此方法正在更新计算值并再次尝试保存。我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      相关资源
      最近更新 更多