尝试获取非对象的 属性 'id'(视图:E:\xampp\htdocs\mini_blog\resources\views\admin\posts\edit.blade.php)

Trying to get property 'id' of non-object (View: E:\xampp\htdocs\mini_blog\resources\views\admin\posts\edit.blade.php)

正在尝试获取 属性 'id' 的非对象 怎么修复这个bug,求大佬解释一下

edit.blade.php

@extends('layouts.app')

@section('content')

<div class="card">
              <div class="card-header text-center">Edit Post : {{$posts->title}}</div>

              <div class="card-body">
                  @if(count($errors)>0)

              <ul class="list-group alert">
                  @foreach($errors->all() as $error)
                  <li class="list-group-item text-danger">
                      {{$error}}
                  </li>

                  @endforeach
              </ul>

                  @endif

                <form action="{{route('post.update',['id'=>$posts->id])}}" method="post" enctype="multipart/form-data">
                  {{csrf_field()}}
                    <div class="form-group">
                        <label for="title">Post Title</label>
                        <input type="text" name="title" placeholder="Enter" class="form-control" value="{{$posts->title}} ">
                    </div>
                    <div class="form-group">
                        <label for="image">Featured Image</label>
                        <input type="file" name="image" 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 $cat)
                          <option value="{{$cat->id}}"

                          @if($posts->cat->id== $cat->id) 
                            selected 
                          @endif

                            >{{$cat->name}}</option>

                          @endforeach
                        </select>
                    </div>

                       <div class="form-group">
                        <label for="tag">Select Tags</label>
                  @foreach($tag as $tags)

                  <div class="checkbox">
                    <label><input type="checkbox" name="tags[]" value="{{$tags->id}}"

                    @foreach($posts->tags as $t)

                  @if($tags->id==$t->id)
                  checked 

                  @endif
                    @endforeach
                      >{{$tags->tag}}</label>
                  </div>

                  @endforeach
                    </div>

                    <div class="form-group">
                        <label for="content">Description</label>
                        <textarea name="content" id="content" cols="5" rows="5" class="form-control"> {{$posts->content}}</textarea>
                    </div>
                        <div class="form-group">
                        <input type="submit" name="submit" value="Submit" class="btn btn-primary">
                    </div>
                </form>
              </div>

          </div>

@endsection PostController.php @extends('layouts.app')

@section('content')

<div class="card">
              <div class="card-header text-center">Edit Post : {{$posts->title}}</div>

              <div class="card-body">
                  @if(count($errors)>0)

              <ul class="list-group alert">
                  @foreach($errors->all() as $error)
                  <li class="list-group-item text-danger">
                      {{$error}}
                  </li>

                  @endforeach
              </ul>

                  @endif

                <form action="{{route('post.update',['id'=>$posts->id])}}" method="post" enctype="multipart/form-data">
                  {{csrf_field()}}
                    <div class="form-group">
                        <label for="title">Post Title</label>
                        <input type="text" name="title" placeholder="Enter" class="form-control" value="{{$posts->title}} ">
                    </div>
                    <div class="form-group">
                        <label for="image">Featured Image</label>
                        <input type="file" name="image" 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 $cat)
                          <option value="{{$cat->id}}"

                          @if($posts->cat->id== $cat->id) 
                            selected 
                          @endif

                            >{{$cat->name}}</option>

                          @endforeach
                        </select>
                    </div>

                       <div class="form-group">
                        <label for="tag">Select Tags</label>
                  @foreach($tag as $tags)

                  <div class="checkbox">
                    <label><input type="checkbox" name="tags[]" value="{{$tags->id}}"

                    @foreach($posts->tags as $t)

                  @if($tags->id==$t->id)
                  checked 

                  @endif
                    @endforeach
                      >{{$tags->tag}}</label>
                  </div>

                  @endforeach
                    </div>

                    <div class="form-group">
                        <label for="content">Description</label>
                        <textarea name="content" id="content" cols="5" rows="5" class="form-control"> {{$posts->content}}</textarea>
                    </div>
                        <div class="form-group">
                        <input type="submit" name="submit" value="Submit" class="btn btn-primary">
                    </div>
                </form>
              </div>

          </div>

@endsection 尝试获取非对象的 属性 'id' 当我 select 类别不是 selected 并且显示 id 是非对象时

PostController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Category;
use App\Post;
use App\Tag;
use Session;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        return view('admin.posts.index')->with('posts',Post::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories=Category::all();

        if($categories->count()==0){

            Session::flash('info','You must have some categories before attempt post.');

            return redirect()->back();
        }

        return view('admin.posts.create')->with('category',$categories)->with('tags',Tag::all());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $this->validate($request,[

        'title'=>'required|max:255',
        'image'=>'required|image',
        'content'=>'required',
        'category_id'=>'required',
        'tags'=>'required'
        ]);

        $images=$request->image;
        $image_new_name=time().$images->getClientOriginalName();
        $images->move('uploads/posts',$image_new_name);

        $post=Post::create([

        'title'=>$request->title,
        'image'=>$request->image,
        'content'=>$request->content,
        'image'=>'uploads/posts/'.$image_new_name,
        'category_id'=>$request->category_id,
        'slug'=>str_slug($request->title)


        ]);

        $post->tags()->attach($request->tags);

        Session::flash('success','Post Created Successfully');

        return redirect()->back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $post=Post::find($id);


        return view('admin.posts.edit')->with('posts',$post)
                                       ->with('categories',Category::all())
                                       ->with('tag',Tag::all());

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $this->validate($request,[

            'title'=>'required',
            'content'=>'required',
            'category_id'=>'required',

        ]);

        $post=Post::find($id);

      if ($request->hasFile('image')) 
      {
            $featured=$request->image;
            $featured_new_name=time().$featured->getClientOriginalName();
            $featured->move('uploads/posts',$featured_new_name);

            $post->image='uploads/posts/'.$featured_new_name;
      }

        $post->title=$request->title;
        $post->content=$request->content;
        $post->category_id=$request->category_id;


        $post->save();

        $post->tags()->sync($request->tags);

        Session::flash('success','Your Post Updated Successfully');

        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $post=Post::find($id);


        $post->delete();

        Session::flash('success','Post was just trashed');

        return redirect()->back();
    }


    public function trashed(){

        $post=Post::onlyTrashed()->get();

        return view('admin.posts.trashed')->with('posts',$post);


    }


    public function kill($id){

        $post=Post::withTrashed()->where('id',$id)->first();

        $post->forceDelete();

        Session::flash('success','Post deleted permanently');

        return redirect()->back();
    }

    public function restore($id){

        $post=Post::withTrashed()->where('id',$id)->first();


        $post->restore();

        Session::flash('success','Post Restore ');

        return redirect()->route('posts');
    }
}

确保 categoriestags 不为空,并且 Post 关系为真。