【发布时间】: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
【问题讨论】: