Laravel - 奇怪更新帖子时无效的日期时间格式错误

时间:2017-11-30 17:06:36

标签: php laravel laravel-5 laravel-4

我正在制作一个编辑页面来编辑我网站上的帖子,每个帖子都在一个频道内。当我尝试编辑帖子而不更改它所在的频道时。它给我一个奇怪的错误说明:

Invalid DateTime format: 1366 Incorrect integer value: 'PHP' for column 'channel_id' at row 1

但是,当我编辑频道时,它完全正常。是什么造成的?我认为这与我的表格有关。帖子有一个channel_id。以便将其连接到具有该ID的通道。两者之间的关系很好。

这是我认为导致问题的形式:

<div class="form-group row">
   <div class="col-md-12">
      <label for="Kanaal">Kanaal:</label>
   </div>
   <div class="col-md-12">
     <select class="form-control" id="Kanaal" name="channel_id">
       <option selected>{{ $post->channel->channel_name }}</option>
       @foreach($channels as $channel)
       <option value="{{ $channel->id }}" {{ old('channel_id') == $channel->id ? 'selected' : '' }}>
       {{ $channel->channel_name }}
       </option>
       @endforeach
     </select>
   </div>
 </div>

我存储更新帖子的方法如下所示:

public function updatePost(Request $request, $id)
{

    $post = Post::find($id);
    $post->channel_id = $request->channel_id;
    $post->title = $request->title;
    $post->text = $request->text;
    $post->slug = str_slug($request->title);

    $post->save();

    return back()->with('flash', 'Het bericht is bewerkt!');
}

它到达方法就好了。但是,当我不编辑频道选项标签时,它会给我错误,而如果我编辑它,它就可以正常工作。

我不知道我是否在解释这个问题,但这是我能做的最好的事情。提前谢谢!

3 个答案:

答案 0 :(得分:0)

而不是这样做

    $post = Post::find($id);
    $post->channel_id = $request->channel_id;
    $post->title = $request->title;
    $post->text = $request->text;
    $post->slug = str_slug($request->title);

请尝试这种方式

$post = Post::find($id);
$post->channel_id = $request->get('channel_id');
$post->title = $request->get('title');
$post->text = $request->get('text');
$post->slug = str_slug($request->get('title'));

我希望这会有所帮助。

答案 1 :(得分:0)

<option selected>{{ $post->channel->channel_name }}</option>导致问题,因为它是默认选中的,并且它不包含任何整数值,并且在您的数据库中channel_id是一个整数。

如果是必填字段,请使用required标记中的select属性并更改您的第一个选项。要做那个改变

<div class="form-group row">
   <div class="col-md-12">
      <label for="Kanaal">Kanaal:</label>
   </div>
   <div class="col-md-12">
     <select class="form-control" id="Kanaal" name="channel_id">
       <option selected>{{ $post->channel->channel_name }}</option>
       @foreach($channels as $channel)
       <option value="{{ $channel->id }}" {{ old('channel_id') == $channel->id ? 'selected' : '' }}>
       {{ $channel->channel_name }}
       </option>
       @endforeach
     </select>
   </div>
 </div>

<div class="form-group row">
   <div class="col-md-12">
      <label for="Kanaal">Kanaal:</label>
   </div>
   <div class="col-md-12">
     <select class="form-control" id="Kanaal" name="channel_id" required="required">
       <option value="">Select Channel</option>
       @foreach($channels as $channel)
       <option value="{{ $channel->id }}" {{ $post->channel_id == $channel->id ? 'selected' : null }}>
       {{ $channel->channel_name }}
       </option>
       @endforeach
     </select>
   </div>
 </div>

答案 2 :(得分:0)

如果您只想使用请求的某些属性

,请尝试这样的操作
// Return the post object
$post = Post::find($id);

// Do your conversion of title to 'slug'
$request['slug'] = str_slug($request->title);

// Call the update method on the model to update
$post->update($request->only(
    'channel_id'
    'title',
    'text',
    'slug'
));

// Explicit save
$post->save();

听起来您的错误可能来自您的示例中未显示的表单请求中的某个位置。例如,您可能在某处尝试插入到channel_id列中的日期输入(表单输入上的重复name属性可能是原因)。

希望这有帮助!

相关问题