-1

So, I've created a controller to add topic in my app, and it stores it in database. The only problem is when I show a view it's giving me ErrorException.

Here is the Topic Controller

 public function store(Request $request)
{
    $users = User::all();

    $userid = Auth::user()->id;

    $topic = new Topic([
      'user_id' => $userid,
      'title' => $request->get('title'),
      'description' => $request->get('description'),
      'tags' => $request->get('tags')
    ]);

    $topic->save();


    //Creating a loop for each user, send a notification.
     $message_content = "The " . Auth::user()->first_name . " " . Auth::user()->last_name . " just added a new topic. \n " . $topic->title . " \n ";

        foreach ( $users as $user ) {

            mail( $user->email, 'New Topic!', $message_content);
        }

    return redirect('/topics')->with('topics', $topics);



}

Here is the view

@foreach($topics as $topic)
    <div class="col-lg-4 col-md-6">
      <div class="widget widget-shadow">
        <div class="widget-content padding-20 bg-green-500 white height-full">

          <a class="avatar pull-left margin-right-20" href="javascript:void(0)">
            <img src=" {{URL::to('uploads')}}/{{ $topic->creator->image }} " alt=" ">
          </a>
          <div style="overflow:hidden;">
            <small class="pull-right grey-200">{{$topic['created_at']}}</small>
            <div class="font-size-18"> {{$topic->creator->first_name}}  {{$topic->creator->last_name}}</div>
            <div class="grey-200 font-size-14 margin-bottom-10">{{$topic->creator->role}}</div>
            <blockquote class="cover-quote font-size-16 white">{{$topic['title']}}
            </blockquote>
          </div>

        </div>
      </div>
    </div>
    @endforeach

Here is the topic model

class Topic extends Model

public $fillable = ['title', 'user_id', 'description', 'tags'];

public function creator() {
    return $this->belongsTo( User::class, 'user_id', 'id');

and here is the user model(only relation to topic model)

public function hasTopic() {
    return $this->hasMany( Topic::class, 'id', 'user_id' );
}

lastly here is the controller that gets the topic data and shows a view

public function sdp()
{

    $topics = Topic::all();

    $users = User::with([ 'hasTopic' => function ($query) {
        $query->where('id', 'user_id');

    }]);


    return view('topics.student', ['topics' => $topics, 'users' => $users] );

}
jlo
  • 1
  • 1
  • 4
  • Provide the error that you getting @ffs33 – Harish Karthick Oct 04 '17 at 01:37
  • [01:44:39] LOG.error: ErrorException: Trying to get property of non-object in – jlo Oct 04 '17 at 01:45
  • I just checked on my localhost it can show the view but on server it wont work – jlo Oct 04 '17 at 01:46
  • What is the data format of your `$topics`? – aldrin27 Oct 04 '17 at 01:58
  • well $topics is just a reference to the topics table – jlo Oct 04 '17 at 02:00
  • Check in your laravel.log file and get the exact line number and file causing the issue. In eloquent relationships, if it's defined incorrectly, you are going to be attempting to get a "property of a non-object". For example, if your "creator" relationship isn't right, you are going to be calling `role` on `null`. Double check your log, and all of your relationships. – brokekidweb Oct 04 '17 at 02:27
  • I just can't figure out why is it showing view on localhost, I add the topic I get the view with creator name but on server it shows that error – jlo Oct 04 '17 at 02:31
  • 1
    Did you look at your log file? It's at `storage/logs/laravel.log`, scroll all the way to the bottom to find your latest error. – brokekidweb Oct 04 '17 at 02:36
  • pls check with the no of characters that you are trying to store and the length mentioned to column bcoz i got the same error while iam trying to store and retrieve excess of char . – Harish Karthick Oct 04 '17 at 02:38

1 Answers1

-1

You didn't passed the $topic variable with your redirect

return redirect('/topics')->with('topics', $topics);

UPDATE 1: Can you try like this

$topic->created_at

Instead of using like array as you have object in $topic, not array.

$topic['created_at']

Read more

Ariful Haque
  • 3,662
  • 5
  • 37
  • 59
  • Still getting the same error – jlo Oct 04 '17 at 01:54
  • Then how do you pass `$topics` variable to your view, in this controller you just redirect to `/topics` route. Which Controller and Function you are using for the `/topics` route? Error must generate there. – Ariful Haque Oct 04 '17 at 01:56
  • I just edited my question, can you check above you'll see the view – jlo Oct 04 '17 at 01:57
  • To mention that I can see topics posted on localhost but when on server it's showing that error – jlo Oct 04 '17 at 01:58
  • Can you share your exception properly? Which object is not found? Line no, File name? – Ariful Haque Oct 04 '17 at 02:44
  • [02:47:50] LOG.error: ErrorException: Trying to get property of non-object in /home/118696.cloudwaysapps.com/fwzyssdrmj/public_html/storage/framework/views/7bfce097aa53fc730f134b425d87dd73f09bd893.php:21 Stack trace: – jlo Oct 04 '17 at 02:50
  • doesnt show which object only which view – jlo Oct 04 '17 at 02:51
  • Updated answer. Try it on your all property. – Ariful Haque Oct 04 '17 at 02:56
  • Just did that, same error – jlo Oct 04 '17 at 03:11
  • can you post contents of /home/118696.cloudwaysapps.com/fwzyssdrmj/public_html/storag‌​e/framework/views/7b‌​fce097aa53fc730f134b‌​425d87dd73f09bd893.p‌​hp especially whats on line 20,21,22 – aimme Oct 04 '17 at 03:23
  • foreach
    {{$topic->created_at}}
    {{$topic->creator->first_name}} {{$topic->creator->last_name}}
    {{$topic->creator->role}}
    {{$topic->title}}
    endforeach
    – jlo Oct 04 '17 at 03:56