尝试通过关联获得 属性 的非对象问题
Trying to get property of non-object issue with associating
我正在尝试 post 通过创建和存储表单在我的一个论坛中创建一个新线程,但在尝试将线程与我 [=22= 的论坛相关联时一直遇到错误] 里面的线程。有人知道问题出在哪里吗?
问题:
Trying to get property of non-object
at HandleExceptions->handleError(8, 'Trying to get property of non-object', 'ThreadController.php', 51, array('request' => object(Request), 'forum_id' => 'soccer', 'forum' => null, 'thread' => object(Thread)))
控制器:
public function create($slug)
{
//
$forum = Forum::where('slug', '=', $slug)->first();
return view('threads.create')->withForum($forum);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $forum_id)
{
//
$this->validate($request, array(
'threadname' => 'required|max:90',
'post' => 'required|max:99000'
));
$forum = Forum::find($forum_id);
$thread = new Thread();
$thread->threadname = $request->threadname;
$thread->forums()->associate($forum);
$thread->user_id = $request->user()->id;
$thread->save();
return view('forum.index');
}
查看:
{!! Form::open(['route' => ['thread.store', $forum->slug], 'method' => 'POST', 'files' => 'true']) !!}
{!! Form::label('threadname', 'Title of Thread:') !!}
{!! Form::text('threadname', null, array('class' => 'form-control')) !!}
{!! Form::label('post', 'Message:') !!}
{!! Form::textarea('post', null, array('class' => 'form-control')) !!}
{!! Form::submit('Create', array('class' => 'btn-send btn-lg btn-block')) !!}
{!! Form::close() !!}
您正在尝试使用字符串 soccer
查找论坛,这应该是论坛的 ID。
'forum_id' => 'soccer'
$forum = Forum::find($forum_id);
如果您按别名或标题搜索,则不能使用 find()
我正在尝试 post 通过创建和存储表单在我的一个论坛中创建一个新线程,但在尝试将线程与我 [=22= 的论坛相关联时一直遇到错误] 里面的线程。有人知道问题出在哪里吗?
问题:
Trying to get property of non-object
at HandleExceptions->handleError(8, 'Trying to get property of non-object', 'ThreadController.php', 51, array('request' => object(Request), 'forum_id' => 'soccer', 'forum' => null, 'thread' => object(Thread)))
控制器:
public function create($slug)
{
//
$forum = Forum::where('slug', '=', $slug)->first();
return view('threads.create')->withForum($forum);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $forum_id)
{
//
$this->validate($request, array(
'threadname' => 'required|max:90',
'post' => 'required|max:99000'
));
$forum = Forum::find($forum_id);
$thread = new Thread();
$thread->threadname = $request->threadname;
$thread->forums()->associate($forum);
$thread->user_id = $request->user()->id;
$thread->save();
return view('forum.index');
}
查看:
{!! Form::open(['route' => ['thread.store', $forum->slug], 'method' => 'POST', 'files' => 'true']) !!}
{!! Form::label('threadname', 'Title of Thread:') !!}
{!! Form::text('threadname', null, array('class' => 'form-control')) !!}
{!! Form::label('post', 'Message:') !!}
{!! Form::textarea('post', null, array('class' => 'form-control')) !!}
{!! Form::submit('Create', array('class' => 'btn-send btn-lg btn-block')) !!}
{!! Form::close() !!}
您正在尝试使用字符串 soccer
查找论坛,这应该是论坛的 ID。
'forum_id' => 'soccer'
$forum = Forum::find($forum_id);
如果您按别名或标题搜索,则不能使用 find()