未定义的变量:section(查看:D:\ xampp \ htdocs \ laravel5 \ resources \ views \ cms \ sections.blade.php)

时间:2016-05-22 18:24:04

标签: laravel-5 laravel-5.2

我尝试制作简单的crud并发现此错误

  

未定义的变量:部分   (查看:d:\ XAMPP \ htdocs中\ laravel5 \资源\视图\ CMS \ sections.blade.php)

路线:

Route::resource('sections','SectionsController');

查看:

{!! Form::open(["url"=>"sections","files" => "true"]) !!}
section name: {!! Form::text("section_name")!!}
<hr/>
{!! Form::file('image',["class"=>"filestyle","data-buttonText"=>"select image","data-input"=>"false"]) !!}
<br/>
{!! Form::submit("insert@upload",["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
<div class="row">
 <h3>get data from db ::</h3>
 @foreach($sections as $section)
  <div class="col-md-3">
   <div class="thumbnail">
    <p>{{$section->section_name}}</p>
    <img src="/uploads/{{$section->image_name}}" width="90%" height="90%">
   </div>
  </div>
 @endforeach
</div>
<div class="row">
 <div class="col-md-4">
  <div class="thumbnail">
   {!! Form::open(["url"=>"sections/$section->id","method" => "patch"]) !!}
   {!! Form::text("section_name",$section->section_name)!!}
   {!! Form::submit("update",["class"=>"btn btn-success"]) !!}
   {!! Form::close() !!}
  </div>
 </div>
 <div class="col-md-4">
  <div class="thumbnail">
   {!! Form::open(["url"=>"sections/$section->id","method" => "delete"]) !!}
   {!! Form::submit("delete",["class"=>"btn btn-danger"]) !!}
   {!! Form::close() !!}
  </div>
 </div>
</div>

控制器:

use DB;
use Input;
use Validator;

class SectionsController extends Controller {
  public function index() {
    $section=DB::table('sections')->get();
    return view('cms.sections')->withSections($section);
  }

  public function create() {
    return view('cms.sections');
  }

  public function store(Request $request) {
    $this->validate($request,
        ['section_name' => 'required|unique:sections,section_name|max:20',
         'image' => 'mimes:jpeg|max:1024']);
    $section_name=$request->input('section_name');
    $file=$request->file('image');
    $destinationPath='uploads';
    $filename=$file->getClientOriginalName();
    $file->move($destinationPath,$filename);
    $rules = array('image' => 'mimes:jpg,png,gif|required|max:10000');
    $validator = Validator::make(Input::all(), $rules);
    DB::table('sections')->insert(['section_name' => $section_name,
                                   'image_name' => $filename]);
    return redirect('sections');
  }

  public function update(Request $request, $id) {
    $section_name=$request->input('section_name');
    DB::table('sections')->where('id',$id)->update(['section_name'=>$section_name]);
    return redirect('sections');
  }

  public function destroy($id) {
    DB::table('sections')->where('id',$id)->delete();
    return redirect('sections');
  }
}

1 个答案:

答案 0 :(得分:0)

您的View代码中存在一个明确的问题:在您结束定义它的$section块之后使用@foreach,例如:

@foreach($sections as $section)
  ...
@endforeach
...
<div class="row">
 <div class="col-md-4">
  <div class="thumbnail">
   {!! Form::open(["url"=>"sections/$section->id","method" => "patch"]) !!}
------------------------------------^^^^^^^^

我怀疑您打算将<div class="row">放在<{1}}块中