这是我的代码和错误:
正如您所看到的,我已经测试了if ( $inserted )
之类的插入语句,但id
没有进入该对象。我该如何解决?
这是我的代码:
public function register_guarantee_ticket()
{
$problem = json_decode($_COOKIE['guarantee_ticket']);
$guarantee_tickets = new guarantee_tickets;
$guarantee_tickets->unique_product_id = $problem->unique_product_id;
$guarantee_tickets->user_id = Auth::user()->id;
$guarantee_tickets->submit_time = time();
$guarantee_tickets->brick = !empty($problem->turn_on) ? 1 : 0;
$guarantee_tickets->title = $problem->title;
$guarantee_tickets->description = $problem->description;
$guarantee_tickets->tracking_code = DB::select("SELECT LEFT(UUID(), 8) as random_unique_string;")[0]->random_unique_string;
$guarantee_tickets->status_id = 1; // waiting ...
$inserted = $guarantee_tickets->save();
if ($inserted){
// echo "ticket registered";
$ticket_id = $guarantee_tickets->id;
return \redirect()->route('list_of_ticket')->with($ticket_id);
unset($_COOKIE['guarantee_ticket']);
setcookie('guarantee_ticket', null, -1, '/');
// redirect to user's panel, in the ticket list
} else {
$status = "register-unique-device-alert-danger";
$msg = "something went wrong, try again";
return \redirect()->route('form_serial_number')->with($status, $msg);
}
}
答案 0 :(得分:2)
$guarantee_tickets->save();
$inserted = $guarantee_tickets->id;
这样就可以了。
答案 1 :(得分:1)
你正在尝试一种不好的方法来使这个东西工作你可以直接访问id作为模型对象的变量来保存价值,这是' $ guarantee_tickets'
并访问id:
$id = $guarantee_tickets->id;
因为$ inserted变量
中没有任何内容答案 2 :(得分:0)
首先,您需要检查inserted
是什么,因为您在if
语句中检查它,但它可能是一个不会返回true或false的对象。
答案 3 :(得分:0)
在public $fillable = [ 'unique_product_id", "user_id", /* other fields */ ]
模型上放置一个guarantee_tickets
,因为除非您将可填写字段设置到模型中,否则它可能无法将字段保存在数据库中。