0
votes

I try to made simple crud and found this error

Undefined variable: section (View:D:\xampp\htdocs\laravel5\resources\views\cms\sections.blade.php)

Route:

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

View:

{!! 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>

Controller:

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
Your code is completely unreadable here. Please take some time to improve its readability. It will help you get answers (hopefully good ones)Arcesilas
Welcome to StackOverflow! I improved your formatting to make the code readable, as @Arcesilas suggested. This makes it easier for us to help you, so it increases your chances of getting an answer. Good luck!mindriot

1 Answers

0
votes

There is one definite problem in your View code: You use $section after you end your @foreach block that defines it, for example here:

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

I suspect you intended to place the <div class="row"> blocks inside your @foreach block.