保存/更新Laravel中的问题

时间:2016-02-18 01:13:01

标签: php laravel insert-update laravel-5.2

我一直在尝试使用Laravel - 我遇到了保存/更新记录(用户描述)的问题。我的代码基于一些有用的代码 - 所以我对我搞砸的事情感到困惑。

我可以使用php tinker进行更改(它反映在页面上 - 所以它不是数据库的东西) - 但是当使用网站本身时,laravel不会保存输入到表单中的任何文本。< / p>

这些不是完整的文件 - 我觉得可能是相关的

谢谢! :)

P.S - 它默默地失败,因此沮丧,并且需要堆栈溢出帖子:))

用于更改说明的表单元素

{!! Form::model($user, ['method' => 'PATCH', 'action' => ['UserController@update', $user->id]]) !!}
    {!! Form::text('description', null, ['class' => 'form-control']) !!}
    {!! Form::submit('save', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}

UserController中

public function store(EditUserRequest $request)
{
    $user = new User($request->all());
    Auth::user()->users()->save($user);
    return redirect('home');
}

public function edit(User $user)
{
    // if owner is posts creator (collection id matches user id)
    if ($user->id == Auth::user()->id) {
        return view('home');
    } else {
        return redirect('home');
    }
}

public function update(User $user, EditUserRequest $request)
{
    // if owner is posts creator (collection id matches user id)
    if ($user->id == Auth::user()->id) {
        $user->update($request->all());
        return redirect('home');
    } else {
        return redirect('home');
    }
}

EditRequestController

<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;

class EditUserRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'description'    =>  'required',
        ];
    }
}

DD($用户)

 User {#142 ▼
  #fillable: array:3 [▼
    0 => "name"
    1 => "email"
    2 => "password"
  ]
  #hidden: array:2 [▼
    0 => "password"
    1 => "remember_token"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:9 [▼
    "id" => 1
    "name" => "Oliver Williams"
    "email" => "Oliver.jack.williams@gmail.com"
    "description" => "lorem ipsum nonsense"
    "password" => "$2y$10$c65lDNJ0V8ibYT4QD6cnk.HmXPwBqlskHWzOzKi1vOlNKVR48y0iS"
    "remember_token" => "1e6Il0T1rjQQMKSAn09Gd8FLdh0CcYTs4Xd7B6IYUp02rNJ9IpSpV374kwRg"
    "created_at" => "2016-02-17 23:37:39"
    "updated_at" => "2016-02-18 00:48:44"
    "time" => "0000-00-00 00:00:00"
  ]
  #original: array:9 [▼
    "id" => 1
    "name" => "Oliver Williams"
    "email" => "Oliver.jack.williams@gmail.com"
    "description" => "lorem ipsum nonsense"
    "password" => "$2y$10$c65lDNJ0V8ibYT4QD6cnk.HmXPwBqlskHWzOzKi1vOlNKVR48y0iS"
    "remember_token" => "1e6Il0T1rjQQMKSAn09Gd8FLdh0CcYTs4Xd7B6IYUp02rNJ9IpSpV374kwRg"
    "created_at" => "2016-02-17 23:37:39"
    "updated_at" => "2016-02-18 00:48:44"
    "time" => "0000-00-00 00:00:00"
  ]
  #relations: []
  #visible: []
  #appends: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

DD($用户&GT;更新($请求 - &GT;所有()));

true

1 个答案:

答案 0 :(得分:0)

这不适用于所有人 - 所以我不确定它是否有资格作为答案 - 但我没有将新行添加到'User.php'中的'fillable'数组 - 这意味着它无法'被批量分配。

我很笨拙。 谢谢大家:)