编辑帖子时,系统会提示我以下错误消息
Trying to get property 'id' of non-object (View: C:\xampp\htdocs\CERCAA\resources\views\admin\posts\edit.blade.php)
public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required',
'content' => 'required',
'category_id' => 'required'
]);
$post = Post::find($id);
if($request->hasFile('featured'))
{
$featured = $request->featured;
$featured_new_name = time() . $featured->getClientOriginalName();
$featured->move('uploads/posts', $featured_new_name);
$post->featured = 'uploads/posts/'.$featured_new_name;
}
$post->title = $request->title;
$post->content = $request->content;
$post->category_id = $request->category_id;
$post->save();
Session::flash('success', 'Post updated successfully.');
return redirect()->route('posts');
}
和刀片代码
<div class="form-group">
选择一个类别
@foreach($ categories为$ category)
id}}“
@if($ post-> $ category-> id == $ category->名称)
已选择
@endif
{{{$ category-> name}}
@endforeach
列表项
答案 0 :(得分:0)
这是您刀片中出现问题的区域:
<label for ="category">Select a Category</label>
<select name="category_id" id="category" class="form-control">
@foreach($categories as $category)
<option value="{{$category->id}}"
@if(property_exists($post, 'category') && $post->$category['id'] == $category->name)
selected
@endif
>{{$category->name}}</option>
@endforeach
</select>
您在哪里获取此刀模板并为其提供$ categories?我的意思是加载编辑刀片的控制器方法? 您也可以发布该代码吗? 并且,请更新您的问题,而不要发布答案。
答案 1 :(得分:0)
我的文章和类别发布方法
命名空间应用;
使用Illuminate \ Database \ Eloquent \ Model;
类类别扩展模型 {
protected $table = 'categories'; // here set table's name
protected $primaryKey = 'id'; // here set table's primary Key field name
protected $fillable = ['id']; // here set all table's fields name
public function posts()
{
return $this->hasMany('App\Post');
}
命名空间应用;
使用Illuminate \ Database \ Eloquent \ Model; 使用Illuminate \ Database \ Eloquent \ SoftDeletes;
class Post扩展了Model {
public function category()
{
return $this->belongsTo('App/Category');
}
public function getFeaturedAttribute($featured)
{
return asset($featured);
}
use SoftDeletes;
protected $dates=['deleted_at'];
protected $fillable=['title','content','category_id','featured','slug'];
}
}
答案 2 :(得分:0)
Edit.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-header bg-info">
<div class="text-center">
<h4 class="m-b-0 text-white">
<div class="panel panel-default">
<div class="panel-heading">
Edit Post:{{$post->title}}
</div>
<div class="panel-body">
<form action="{{route('post.update', ['id'=>$post->id])}} " method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for ="title">Title</label>
<input type="text" name="title" class="form-control" value="{{$post->title}}">
</div>
<div class="form-group">
<label for ="featured">Featured image</label> <input type="file" name="featured" class="form-control">
</div>
<div class="form-group">
<label for ="category">Select a Category</label>
<select name="category_id" id="category" class="form-control">
@foreach($categories as $category)
<option value="{{$category->id}}"
@if(property_exists($post, 'category') && $post->$category['id'] == $category->name)
selected
@endif
>{{$category->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for ="content">Content</label>
<textarea name="content" id="content" cols="5" rows="5" class="form-control"> {{$post->content}}</textarea>
</div>
<div class="form-group">
<div class="text-center">
<button class="btn btn-success" type="submit"> Update Post</button>
</div>
</div>
</form>
</div>
</div>
</h4>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Row -->
@停止
答案 3 :(得分:0)
控制器...
公共功能存储(请求$ request) {
Regex _regex = new Regex(@"\d\d\d\d-\d\d-\d\d");
Match _date = _regex.Match(<Your_Date_String>);
if (_date.Success)
{
var datetime = Convert.ToDateTime(_date.Value);
}