dongmeng2509 2019-08-14 09:33
浏览 165

ErrorException(E_ERROR)未定义的变量:group

when i search it will show error

ErrorException (E_ERROR) Undefined variable: group (View: C:\xampp\htdocs\ff\Laravel-Webboard-Workshop-masteresources\views\topic\index.blade.php) Previous exceptions Undefined variable: group (0)

in topic page at line of route('topic.create', $group->id) that is code about button new topic

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

model 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] );
     }

}

How should i do to fix this bug for search in my topic page??

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思