Laravel为生产服务器(Forge)上的模型ID返回0。

时间:2018-11-04 14:09:00

标签: php laravel

我有一个奇怪的情况,Laravel为ID返回0。这在本地有效,并且我还确认了该模型已保存并在另一个函数中正确返回了ID。为什么ID总是为0? (模型总是成功保存)。

“ ID”列是自动递增的。

//模型

class Email extends Model
{
public $fillable = ['to', 'from', 'relates_to', 'name', 'email',
 'subject', 'greeting','attachment',
'body', 'module_id', 'sign_off', 'course_id' ];

protected $casts = ['attachment'=>'integer'];

protected $dates = ['sent_at'];

public function scopeMine($query, $user)
{
    return $query->where('from', $user);
}
}

//控制器

public function storeAndSend(Request $request)
{
    $validatedData = $request->validate([
        'subject' => 'sometimes|required|string',
        'greeting' => 'required',
        'body' => 'required',
        'sign_off' => 'required',
        'email' => 'required|email',
        'module_id' => 'required',
        'name' => 'required|string'
    ]);

    //save Email   
    $email = new \App\Email;
    $email->to = $request->has('user') ? $request->user : 0;
    $email->name = $request->name;
    $email->email = $request->email;
    $email->relates_to = $request->has('entity') ? $request->entity : null;
    $email->from   = $request->user()->id;
    $email->subject = $request->subject;
    $email->greeting = $request->greeting;
    $email->attachment = $request->attachment ?? null;
    $email->body = $request->body;
    $email->sign_off = $request->sign_off;
    $email->course_id = $request->course_id;
    $email->module_id = $request->module_id ?? null;

    $email->save();
    dd($email->toArray()); // $email->id = 0

    //get to
    dispatch(new \App\Jobs\SendCourse($email->id));

    return response()->json(['success'=>true]);
}

//转储的输出

    array:15 [
  "to" => "58"
  "name" => "joe"
  "email" => "joe@boobo.com"
  "relates_to" => "App\User"
  "from" => 1
  "subject" => "test"
  "greeting" => "Hello"
  "attachment" => null
  "body" => "<p>test</p>"
  "sign_off" => "Catch you later"
  "course_id" => 27
  "module_id" => -1
  "updated_at" => "2018-11-04 14:00:59"
  "created_at" => "2018-11-04 14:00:59"
  "id" => 0
]

1 个答案:

答案 0 :(得分:0)

这是望远镜的问题。 如评论中所述,这就是问题所在:https://github.com/laravel/telescope/issues/289

看来,现在它已被修复,顺便说一句。