未定义的变量。分类 (View: C:\xampp\htdocs\laravel\Third\resources\views/postedits\edit.blade.php) http:/commmanager.composts7edit这个问题好像在edit.blade.php上,但我已经尝试搜索答案,但仍然没有任何结果。请大家帮忙。
帖子控制程序.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\POst;
use DB;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth' , ['except' => ['index','show']]);
}
public function index()
{
//$posts = POst::all();
//return POst::where('title', 'Post Two')->get();
//$posts = DB::select('SELECT * FROM p_osts');
//$posts POst::orderBy('title', 'asc')->take(1)->get();
//$posts = POst::orderBy('title', 'asc')->get();
$posts = POst::orderBy('created_at', 'asc')->paginate(10);
return view('posts.index')->with('posts', $posts);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'Organizer' => 'required',
'EventName' => 'required',
'EventDate' => 'required|date|date_format:Y-m-d',
'Description' => 'required',
'EventLocation' => 'required',
'Collaborator' => 'required',
'Contact' => 'required',
'RecruitmentDate' => 'required',
'Categories' => 'required',
'TotalCommittee' => 'required',
'poster_image' => 'image|nullable|max:1999',
]);
// Handle DatePicker
// Handle File Upload
if($request->hasFile('poster_image')){
//Getfilename with the extension
$filenameWithExt = $request->file('poster_image')->getCLientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('poster_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('poster_image')->storeAs('public/poster_images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
// Create Post
$post = new POst;
$post->Organizer = $request->input('Organizer');
$post->EventName = $request->input('EventName');
$post->EventDate = $request->input('EventDate');
$post->Description = $request->input('Description');
$post->EventLocation = $request->input('EventLocation');
$post->Collaborator = $request->input('Collaborator');
$post->RecruitmentDate = $request->input('RecruitmentDate');
$post->Categories = implode(", ", $request->get('Categories'));
$post->TotalCommittee = $request->get('TotalCommittee');
$post->Contact = $request->input('Contact');
$post->user_id = auth()->user()->id;
$post->poster_image = $fileNameToStore;
$post->save();
return redirect('/posts')->with('success','Post Created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$post = POst::find($id);
return view('posts.show')-> with('post', $post);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$post = POst::find($id);
//Check if post exists before deleting
if (!isset($post)){
return redirect('/posts')->with('error', 'No Post Found');
}
// Check for correct user
if(auth()->user()->id !==$post->user_id){
return redirect('/posts')->with('error', 'Unauthorized Page');
}
return view('posts.edit')->with('post', $post);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request,[
'Organizer' => 'required',
'EventName' => 'required',
'EventDate' => 'required|date|date_format:Y-m-d',
'Description' => 'required',
'EventLocation' => 'required',
'Collaborator' => 'required',
'Categories' => 'required',
'RecruitmentDate' => 'required',
'TotalCommittee' => 'required',
'Contact' => 'required',
]);
// Handle Categories
// Handle File Upload
if($request->hasFile('poster_image')){
//Getfilename with the extension
$filenameWithExt = $request->file('poster_image')->getCLientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('poster_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('poster_image')->storeAs('public/poster_images', $fileNameToStore);
}
// Create Post
$post = POst::find($id);
$post->Organizer = $request->input('Organizer');
$post->EventName = $request->input('EventName');
$post->EventDate = $request->input('EventDate');
$post->Description = $request->input('Description');
$post->EventLocation = $request->input('EventLocation');
$post->Collaborator = $request->input('Collaborator');
$post->RecruitmentDate = $request->input('RecruitmentDate');
$post->Categories = explode(", ", $request->get('Categories'));
$post->TotalCommittee = $request->get('TotalCommittee');
$post->Contact = $request->input('Contact');
$post->user_id = auth()->user()->id;
if($request->hasFile('poster_image')){
$post->poster_image = $fileNameToStore;
}
$post->save();
return redirect('/posts')->with('success','Post Updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post = POst::find($id);
//Check if post exists before deleting
if (!isset($post)){
return redirect('/posts')->with('error', 'No Post Found');
}
//Check for correct user
if(auth()->user()->id !== $post->user_id){
return redirect('/posts')->with('error', 'Unauthorized Page');
}
if($post->poster_image != 'noimage.jpg'){
// Delete Image
Storage::delete('public/poster_images/'.$post->poster_image);
}
$post->delete();
return redirect('/dashboard')->with('success','Post Removed');
}
}
-----------------------------------
edit.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row my-2">
<div class="col-lg-8 order-lg-2">
<h1>EDIT RECRUITMENT APPLICATION</h1>
{!! Form::open(['action' => ['PostsController@update', $post->id], 'method' =>'POST', 'enctype'=>'multipart/form-data']) !!}
<div class="form-group">
{{Form::label('Organizer', 'Organizer')}}
{{Form::text('Organizer', $post->Organizer, ['class' => 'form-control','placeholder' =>'Organizer'])}}
</div><hr>
<div class="form-group">
{{Form::label('EventName', 'EventName')}}
{{Form::text('EventName', $post->EventName, ['class' => 'form-control','placeholder' =>'EventName'])}}
</div><hr>
<div class="form-group">
{{Form::label('EventDate', 'Event Date')}}
{{Form::date('EventDate', $post->EventDate, ['class' => 'form-control'])}}
</div><hr>
<div class="form-group">
{{Form::label('Description', 'Description')}}
{{Form::textarea('Description', $post->Description, ['class' => 'form-control','placeholder' =>'Description', 'rows' => 5, 'cols' => 40])}}
</div><hr>
<div class="form-group">
{{Form::label('EventLocation', 'EventLocation')}}
{{Form::text('EventLocation', $post->EventLocation, ['class' => 'form-control','placeholder' =>'EventLocation'])}}
</div><hr>
<div class="form-group">
{{Form::label('Collaborator', 'Collaborator')}}
{{Form::textarea('Collaborator', $post->Collaborator, ['class' => 'form-control','placeholder' =>'Collaborator', 'rows' => 5, 'cols' => 40])}}
</div><hr>
<div class="form-group">
<label>Categories</label>
<div class="checkbox" rows="5", cols="40">
<label>
<input type="checkbox" name="Categories[]" value="Education"
{{in_array("Education", $Categories) ? "checked": ""}}>Education
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="Categories[]" value="Animals and Nature"
{{in_array("Animals & Nature", $Categories) ? "checked": ""}}>Animals and Nature
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="Categories[]" value="Culture and Arts"
{{in_array("Culture and Arts", $Categories) ? "checked": ""}}>Culture and Arts
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="Categories[]" value="Community Service"
{{in_array("Community Service", $Categories) ? "checked": ""}}>Community Service
</label>
</div>
<div class="checkbox"> //The problem is here where the $Categories is undefined what does that mean?
<label>
<input type="checkbox" name="Categories[]" value="Festival"
{{in_array("Festival", $Categories) ? "checked": ""}}>Festival
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="Categories[]" value="Health"
{{in_array("Health", $Categories) ? "checked": ""}}>Health
</label>
</div>
</div><hr>
<div class="form-group">
{{Form::label('RecruitmentDate', 'Last Recruitment Date')}}
{{Form::date('RecruitmentDate', $post->RecruitmentDate, ['class' => 'form-control'])}}
</div><hr>
<div class="form-group">
{{Form::label('TotalCommittee', 'Total Committees Needed')}}
{{Form::selectRange('number', 0, 1000)}}
</div><hr>
<div class="form-group">
{{Form::label('Contact', 'Contact')}}
{{Form::text('Contact', $post->Contact, ['class' => 'form-control','placeholder' =>'Contact'])}}
</div><hr>
<div class="form-group">
{{Form::file('poster_image')}}
</div> <hr>
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('Submit', ['class' =>'btn btn-primary'])}}
{!! Form::close() !!}
</div>
</div>
</div>
@endsection
你使用了变量$Categories,而没有将其返回给刀片。
在此编辑
public function edit($id)
{
....
return view('posts.edit')->with('post', $post);
// you should define the variable then write
return view('posts.edit',['post'=> $post,'Categories' => $Categories]);
}