[英]Laravel Eloquent $model->save() not saving but no error
更新我的Post
model 时,我运行:
$post->title = request('title');
$post->body = request('body');
$post->save();
这不会更新我的帖子。 但它应该根据Laravel 文档更新 Eloquent 模型。 为什么我的 model 没有更新?
save
是否成功的结果为true
。 Post
model:
class Post extends Model
{
protected $fillable = [
'type',
'title',
'body',
'user_id',
];
....
}
Post
controller:
public function store($id)
{
$post = Post::findOrFail($id);
// Request validation
if ($post->type == 1) {
// Post type has title
$this->validate(request(), [
'title' => 'required|min:15',
'body' => 'required|min:19',
]);
$post->title = request('title');
$post->body = request('body');
} else {
$this->validate(request(), [
'body' => 'required|min:19',
]);
$post->body = request('body');
}
$post->save();
return redirect('/');
}
运行dd($post->save())
返回true
。
跑步
$post->save();
$fetchedPost = Post::find($post->id);
dd($fetchedPost);
向我显示$fetchedPost
与之前的帖子相同,但没有更新数据。
如果“id”列是大写“ID”,请检查您的数据库表。 将其更改为小写允许我的 save() 方法工作。
我有同样的结果,结果是因为我在没有主键的情况下过滤了输出列。
$rows = MyModel::where('...')->select('col2', 'col3')->get();
foreach($rows as $row){
$rows->viewed = 1;
$rows->save();
}
固定与
$rows = MyModel::where('...')->select('primary_key', 'col2', 'col3')->get();
在审查时非常有意义,如果没有可用的主键,更新命令将为 Null。
我有同样的问题,改变我获取模型的方式解决了它!
尽管正如您提到的那样,一切都应该正常工作,但没有保存:
$user = User::find($id)->first();
这是有效的:
$user = User::find($id);
您必须确保调用save()
的实例具有属性id
由于 Laravel 5.5 laravel 改变了一些验证机制,我想你需要尝试这种方式。
public function store(Request $request, $id)
{
$post = Post::findOrFail($id);
$validatedData = [];
// Request validation
if ($post->type == 1) {
// Post type has title
$validatedData = $request->validate([
'title' => 'required|min:15',
'body' => 'required|min:19',
]);
} else {
$validatedData = $request->validate([
'body' => 'required|min:19',
]);
}
$post->update($validatedData);
return redirect('/');
}
在DB::transaction
中运行dd()
将导致回滚,并且数据库中的数据不会改变。
原因是,该事务只会在最后保存对数据库的更改。 因此,运行“dump and die”的行为自然会导致脚本停止,因此不会更改数据库。
如果主键不是 id(“列名只能是小写字母”),请检查您的表如果您使用不同的键设置了列名,然后将代码像这样放在您的模型中
protected $primaryKey = 'Id';
因此,如果您的列名包含大写字母,这也可能是您的情况的可能解决方案之一。 是的,这对我来说很好,你应该有小写字母的列名,如果你没有然后在模型文件中提到它,主要是你的模型将尝试访问数据库的 primaryKey。
如果数据库的主键不是“id”,则使用save()方法更新或删除。 需要在模型中声明属性primaryKey = "",才会生效
尝试这个
public function store($id,Request $request)
{
$post = Post::findOrFail($id);
// Request validation
if ($post->type == 1) {
// Post type has title
$request->validate([
'title' => 'required|min:15',
'body' => 'required|min:19',
]);
$post->update([
'title' => request('title');
'body' => request('body');
]);
} else {
$request->validate([
'body' => 'required|min:19',
]);
$post->update([
'body' => request('body');
]);
}
return redirect('/');
}
以我的经验,如果你从数据库中选择一个 Eloquent 模型并且primary_key
列不是获取的列的一部分,你的$model->save()
将返回 true,但没有任何东西被持久化到数据库中。
因此,与其做\App\Users::where(...)->first(['email'])
,不如做\App\Users::where(...)->first(['id','email'])
,其中id
是在目标表上定义的primary_key
。
如果通过仅检索几列实现的(有时是微优化)对您来说并不重要,您可以通过执行\App\Users::where(...)->first()
来获取所有列,在在这种情况下,您无需担心primary_key
列的名称,因为将获取所有列。
如果您使用交易。 不要忘记调用DB::commit();
它必须如下所示:
try{
DB::beginTransaction();
// Model changes
$model->save();
DB::commit();
}catch (\PDOException $e) {
DB::rollBack();
}
我有同样的问题,虽然在controller@action()
中有try / catch
块但是没有响应,它只是停止在$model->save();
apache error.log
或laravel.log
中没有日志条目。 我刚刚用try / cactch
包装了save()
如下,这帮助我找出了问题
try{
$model->save();
}
catch (\PDOException $e) {
echo $e->getMessage();
}
我遇到了同样的问题并找到了解决方法。 我发现我无法在home.blade.php
模板上名为{{ generateUrl() }}
的函数中save()
我的模型。 起作用的是将save()
调用移动到return
home.blade.php
模板的控制器。 (IE,在视图return
之前save()
ing,然后只在{{ generateUrl() }}
内执行读取操作。)
我正在(并且正在)生成一个state
以在页面加载时放入 URL:
<!--views/home.blade.php-->
<a href="{{ EveAuth::generateUrl() }}">Add Character</a>
下面是什么不起作用。
// Providers/EveAuth.php
function generateUrl()
{
$authedUser = auth()->user();
if (!$authedUser) {
return "#";
}
$user = User::find($authedUser->id);
$user->state = str_random(16);
$user->save();
$baseUrl = 'https://login.eveonline.com/oauth/authorize?state=';
return $baseUrl . $user->state;
}
这能够从数据库中find()
User
,但无法将其save()
回来。 没有产生错误。 该功能似乎可以正常工作......直到我稍后尝试读取User
的state
,发现它与 URL 中的state
不匹配。
这是起作用的。
在组装页面时,我没有尝试save()
我的User
,而是生成了state
, save()
d 它,然后呈现页面:
// routes/web.php
Route::get('/', 'HomeController@index');
登陆根目录会将您带到HomeController.php
的index()
函数:
// Controllers/HomeController.php
public function index()
{
$authedUser = auth()->user();
if ($authedUser) {
$user = User::find($authedUser->id);
$user->state = str_random(16);
$user->save();
}
return view('home');
}
然后,在生成 URL 时,我不必save()
User
,只需从中读取:
// Providers/EveAuth.php
function generateUrl()
{
$authedUser = auth()->user();
$user = User::find($authedUser->id);
$baseUrl = 'https://login.eveonline.com/oauth/authorize?state=';
return $baseUrl . $user->state;
}
这行得通! 唯一的区别(据我所知)是我在页面组装开始之前save()
模型,而不是在页面组装期间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.