0

When I go on my edit view recieve this error "Trying to get property of non-object". I have an edit view for editing questionnaires, and in that view I can add questions.

my edit view:

@extends('layouts.master')

@section('title', 'Edit Questionnaire | SurveySays!')

@section('content')
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <h1>Edit {{$questionnaires->title}}</h1>
        <h3>Edit your questionnaire using the form below:</h3>

        {!! Form::model($questionnaires, ['method' => 'PATCH', 'url' => '/questionnaires/' . $questionnaires->id]) !!}

        <div class="container-fluid">
            <div class="form-group">
                {!! Form::label('title', 'Title:') !!}
                {!! Form::text('title',null,['id' => 'title','class' => 'form-control']) !!}
            </div>
            <div class="form-group">
                {!! Form::label('description', 'Description:') !!}
                {!! Form::textarea('description',null,['class' => 'form-control']) !!}
            </div>
            <div class="form-group">
                {!! Form::label('ethics', 'Ethical considerations:') !!}
                {!! Form::textarea('ethics',null,['class' => 'form-control']) !!}
            </div>
            <hr>
            <div class="form-group">
                {!! Form::label('questions', 'Questions:') !!}
                @foreach($questions as $question)
                    <div class="form-group">
                        {!! Form::checkbox('question[]', $question->id, $questionnaires->question->contains($question->id), ['id' =>$question->id]) !!}}
                        {!! Form::label($question->question) !!}
                    </div>
                @endforeach
            </div>
            <hr>
            <div class="form-group">
                {!! Form::submit('Update', array('class' => 'btn btn-success form-control')) !!}
            </div>
        </div>
    </div>

    {!! Form::close() !!}


@endsection

My QuestionnairesController@edit function:

public function edit($id)
    {
        $questionnaires = Questionnaire::findOrFail($id);
        $questions = Question::findOrFail($id);

        return view('questionnaires.edit', compact('questionnaires'), compact('questions'));
    }

What is causing this error? Please help! :)

CJF
  • 39
  • 6

3 Answers3

0

I think maybe you just forgot to set relation in Questionnaire Model or Questionnaire just can't find relation question in $questionnaires->question.

Make sure it. and hope that can help you .

0

Try below code in your controller

public function edit($id)
{
    $questionnaires = Questionnaire::findOrFail($id);
    $questions = Question::findOrFail($id);

    return view('questionnaires.edit', compact('questionnaires','questions'));
}
krunal nerikar
  • 436
  • 4
  • 12
0

findOrFail will return an instance of the Model, not a Collection of Models. In your controller, you have:

$questions = Question::findOrFail($id);

With this code, $questions will be a Question instance (or null). Then, in your view, you're looping over this variable with a foreach:

@foreach($questions as $question)
    // snip
@endforeach

This will end up just looping over the attributes on the instance. The first attribute that it hits that is not itself an object will give you the "Trying to get property of non-object" error.

From your code, I'm assuming that $questions is supposed to be a list of all the questions. I'm guessing your code in your controller should actually be:

$questions = Question::get();
patricus
  • 59,488
  • 15
  • 143
  • 145