[英]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.