Laravel model.save()无声无息地死去

时间:2015-07-04 20:12:18

标签: php laravel eloquent

我是Laravel的新手,但是曾与其他语言的ORM合作过 其中一个新创建的表的save方法失败 没有例外&我知道它失败了,因为保存后的回声没有显示 lalarvel登录 ../ app / storage / logs / laravel.log 为空(即使出现错误也从未显示过任何内容)。
我怎样才能找到出错的地方?

            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

3 个答案:

答案 0 :(得分:1)

您使用的是p1.save(),它应该是p1->save()

答案 1 :(得分:1)

方法save()不起作用时要执行的选项:

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

答案 2 :(得分:0)

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

我的问题:我有一个带有Created方法的Model Observer。该方法正在更新计算值,然后再次尝试保存。我