0

When I am editing my post then I am prompted with the following error message

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');

}

and blade code

<div class="form-group">

Select a Category

@foreach($categories as $category)

id}}"

@if($post->$category->id == $category->name)

selected

@endif

{{$category->name}}

@endforeach

  • List item

4
  • from what i can presume...because it is not very clear you are trying to select a property from an ARRAY not an object, so just change it in a way that is selecting an array element and not an object eg: selecting property from object: $array->my_object; selecting element from array : $array['my_object']; Commented Apr 26, 2019 at 7:47
  • @AndreiFiordean, I did but still it has same issue, how can change for category Commented Apr 26, 2019 at 7:54
  • that is what i am trying to say, on this $post->$category->id you should do it like this $post->$category['id'], your post is an array Commented Apr 26, 2019 at 7:56
  • thank you,, I mean to say that not getting error after using array['id'], however still I have same issue, When going to edit my post, category slecting home category(only one Home category). Commented Apr 26, 2019 at 8:31

4 Answers 4

0

My Post method for post and category

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends 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');
}

namespace App;

use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends 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'];

}

}

Sign up to request clarification or add additional context in comments.

Comments

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 -->

@stop

Comments

0

Controller...

public function store(Request $request) {

    $this->validate($request, [


       'title' =>'required',
        'featured'=>'required|mimes:jpeg,pdf,docx,png:5000',

        'file'=>'required|mimes:jpeg,pdf,docx,png:5000',
        'content'=>'required',
        'category_id'=>'required',

    ]);




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

    $file=$request->file;
    $file_name=time().$file->getClientOriginalName();
    $file->move('uploads/posts', $file_name);

    $post = Post::create([

        'title'=>$request->title,
        'content'=>$request->content,
        'featured'=>'uploads/posts/'. $featured_new_name,
        'file'=>'uploads/posts'. $file_name,
        'category_id'=>$request->category_id,
        'slug'=>str_slug($request->title)


    ]);


  Session::flash('success', 'New Blog has been Published on Website for Particular Menu');

  return redirect()->back();

}

Comments

0

This the area in your blade where you are getting the issue:

<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>

Where are you fetching and providing $categories to this blade template? I mean the controller method which loads the edit blade? Can you post that code as well? And please update your question instead of posting answers.

7 Comments

I tried this but issue is still same, when editing the post , it has always selected home category as a default. thank you for your response
can you please provide your controller, view and model code?
Please wait , I will update my code in a proper way
Post controller public function update(Request $request, $id) { $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(); }
please provide the code of the controller method that loads your edit blade, this should be the edit method of your controller.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.