Laravel更新功能不起作用并且没有显示错误

时间:2020-03-08 18:33:32

标签: php laravel

我正在尝试通过laravel控制器中的简单查询来更新数据库记录。

这是我的代码,

import discord

表格

DB::table('calender_events')->where('id', '=', '1')->update(
    array(
        'title' => $request->title,

    )
);

当我单击“保存”按钮时,它会按预期重定向到主页,但数据库没有更新。 我也使用插入和选择查询,没有任何错误。 是的,数据库中有一条ID为1的记录。 请帮忙!

2 个答案:

答案 0 :(得分:1)

您应该这样编写代码

DB::table('calender_events')->where('id',1)->update(
   array(
      'title' => $request->title,

   )
);

答案 1 :(得分:0)

这是我最后想出的工作代码,

ion-content
<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class EditUserController extends Controller
{
     /**
     * Update the specified user in storage
     *
     * @param  \App\Http\Request   $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request)
    {
         $image = $request->file('avatar');
    if($image!=null){
        $avatar_name = $request->get('NIC').'.'.$image->getClientOriginalExtension();
        $folder = '/uploads/avatars/';
       // $filePath = $folder . $avatar_name;
        $this->uploadOne($image, $folder, 'public', $avatar_name);
    }else{
        $avatar_name = null;  
    } 
    DB::table('users')->where('id', '=', $request->user_id_)->update(
        array(
            'avatar_id' => $avatar_name,
            'name'=>$request->name,
            'NIC'=>$request->NIC,
            'email'=>$request->email,
            'current_job_position'=>$request->current_job_position,
            'birthday'=>$request->birthday,
            'anniversary'=>$request->anniversary,
            'phone_home'=>$request->phone_home,
            'phone_mobile'=>$request->phone_mobile,
            'address_permanent'=>$request->address_permanent,
            'address_temporary'=>$request->address_temporary,
            'branch'=>$request->branch,
            'bank'=>$request->bank,
            'bank_number'=>$request->bank_number,
            'user_role'=>$request->user_role,
            'supervisor_manager'=>$request->supervisor_manager,
            'next_kin_name'=>$request->next_kin_name,
            'next_kin_occupation'=>$request->next_kin_occupation,
            'next_kin_address'=>$request->next_kin_address,
            'next_kin_office_address'=>$request->next_kin_office_address,
            'next_kin_phone_mobile'=>$request->next_kin_phone_mobile,
            'next_kin_phone_home'=>$request->next_kin_phone_home,
            'updated_at'=>now()

        )
    );
        return redirect()->route('user.index')->withStatus(__('User successfully updated.'));
    }

     /**
     * Store a newly created user in storage
     *
     * @param  \App\Http\Request   $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {

        
        $image = $request->file('avatar');
        $pass = Hash::make($request->password);
    if($image!=null){

        $avatar_name = $request->get('NIC').'.'.$image->getClientOriginalExtension();
        $folder = '/uploads/avatars/';
       // $filePath = $folder . $avatar_name;
        $this->uploadOne($image, $folder, 'public', $avatar_name);

        DB::table('users')->insert([
            [
            'avatar_id' => $avatar_name,   
            'name'=>$request->name,
            'NIC'=>$request->NIC,
            'email'=>$request->email,
            'password'=>$pass,
            'current_job_position'=>$request->current_job_position,
            'birthday'=>$request->birthday,
            'anniversary'=>$request->anniversary,
            'phone_home'=>$request->phone_home,
            'phone_mobile'=>$request->phone_mobile,
            'address_permanent'=>$request->address_permanent,
            'address_temporary'=>$request->address_temporary,
            'branch'=>$request->branch,
            'bank'=>$request->bank,
            'bank_number'=>$request->bank_number,
            'user_role'=>$request->user_role,
            'supervisor_manager'=>$request->supervisor_manager,
            'next_kin_name'=>$request->next_kin_name,
            'next_kin_occupation'=>$request->next_kin_occupation,
            'next_kin_address'=>$request->next_kin_address,
            'next_kin_office_address'=>$request->next_kin_office_address,
            'next_kin_phone_mobile'=>$request->next_kin_phone_mobile,
            'next_kin_phone_home'=>$request->next_kin_phone_home,
            'created_at'=>now(),
            ],
        ]);  
    }else{
        DB::table('users')->insert([
            [
            'name'=>$request->name,
            'NIC'=>$request->NIC,
            'email'=>$request->email,
            'password'=>$pass,
            'current_job_position'=>$request->current_job_position,
            'birthday'=>$request->birthday,
            'anniversary'=>$request->anniversary,
            'phone_home'=>$request->phone_home,
            'phone_mobile'=>$request->phone_mobile,
            'address_permanent'=>$request->address_permanent,
            'address_temporary'=>$request->address_temporary,
            'branch'=>$request->branch,
            'bank'=>$request->bank,
            'bank_number'=>$request->bank_number,
            'user_role'=>$request->user_role,
            'supervisor_manager'=>$request->supervisor_manager,
            'next_kin_name'=>$request->next_kin_name,
            'next_kin_occupation'=>$request->next_kin_occupation,
            'next_kin_address'=>$request->next_kin_address,
            'next_kin_office_address'=>$request->next_kin_office_address,
            'next_kin_phone_mobile'=>$request->next_kin_phone_mobile,
            'next_kin_phone_home'=>$request->next_kin_phone_home,
            'created_at'=>now(),
            ],
        ]);  
        }
    return redirect()->route('user.index')->withStatus(__('User successfully created.'));
    }

    public function uploadOne(UploadedFile $uploadedFile, $folder = null, $disk = 'public', $filename = null)
    {
        $name = !is_null($filename) ? $filename : Str::random(25);

        $path =storage_path('uploads/avatars/'.$filename.'');

        if( Storage::exists($path)){
          
         Storage::disk('public')->delete('uploads/avatars/'.$filename.'');  
        }
        $file = $uploadedFile->storeAs($folder, $name, $disk);
        return $file;
    }

}

我不得不制作另一个刀片并将编辑功能移到另一个控制器上,这是因为我使用的是免费的仪表板模板,它内置了身份验证功能,但阻止了部分免费用户使用,无论如何都要感谢您的帮助。