搜索时会显示错误
ErrorException(E_ERROR)未定义变量:group(视图:C:\ xampp \ htdocs \ ff \ Laravel-Webboard-Workshop-master \ resources \ views \ topic \ index.blade.php)先前的例外未定义变量:group( 0)
位于路线(“ topic.create”,$ group-> id)行的主题页面中,该页面是有关按钮新主题的代码
view / topic / index.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
@if (! Auth::guest())
<form action="/search_topic" method="get">
<button type="submit" class="btn btn-primary" style="float:right;">Search</button>
<a style="float:right;">
<input type="search" name="search" class="form-control" >
</a>
</form>
<br><br>
<div>
<a href="{{ url('/') }}" style="text-align:left;">
<button type="button" class="btn btn-default">
<span aria-hidden="true"></span>
Back to Home
</button>
</a>
<a href="{{ route('topic.create', $group->id) }}" style="float:right;"> <!--error-->
<button type="button" class="btn btn-primary">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
New Topic
</button>
</a>
</div>
<br />
@endif
<div class="panel panel-default">
<div class="panel-heading" style="font-size: 18px;">
Group: {{ $group->title }}
</div>
<div class="panel-body">
<table class="table table-striped table-linkable table-hover">
<thead>
<tr>
<th class="text-center">Topic</th>
<th class="text-center">Posted By</th>
<th class="text-center">Posted At</th>
@if (! Auth::guest())
<th>Edit</th>
<th>Delete</th>
@endif
</tr>
</thead>
<tbody>
@foreach($topics as $topic)
<tr onclick="document.location.href = '{{ action('TopicController@show', $topic->id) }}'">
<td>{{ $topic->title }}</td>
<td class="text-center">{{ $topic->user['name'] }}</td>
<td class="text-center">{{ $topic->created_at->diffForHumans() }}</td>
@if (! Auth::guest())
<td>
@if (Auth::user()->id == $topic->user_id)
<a href="{{ action('TopicController@edit', $topic->id) }}" class="btn btn-warning">Edit</a>
@endif
</td>
<td>
@if (Auth::user()->id == $topic->user_id)
<form method="post" class="delete_form" action="{{action('TopicController@destroy', $topic->id)}}">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit" class="btn btn-danger" onclick="return myFunction();">Delete</button>
<script>
function myFunction() {
if(!confirm("Are You Sure to Delete"))
event.preventDefault();
}
</script>
</form>
@endif
</td>
@endif
</tr>
@endforeach
</tbody>
</table>
<div class="text-center">
{!! $topics->links(); !!}
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
模型Topic.php
<?php
namespace App;
use App\Group;
use App\Topic;
use Illuminate\Database\Eloquent\Model;
class Topic extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title', 'body'
];
/**
* Get the Comments of a given topic.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany('App\Comment');
}
/**
* Get a user of a given topic
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('App\User');
}
public function group()
{
return $this->belongsTo('App\Group');
}
}
TopicController.php
<?php
namespace App\Http\Controllers;
use App\Comment;
use App\Topic;
use App\Group;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
class TopicController extends Controller
{
public function search(Request $request)
{
$search = $request->get('search');
//$groups = DB::table('groups')->where('title', 'like', '%'.$search.'%')->paginate(5);
$topics = Topic::with('user')->where('title', 'like', '%'.$search.'%')->paginate(5);
return view('topic.index', ['topics' => $topics] );
}
}
我应该如何解决此错误以在主题页面中进行搜索?