问题描述
我正在尝试测试应用程序的多重上传功能,但由于停留在Trying to get property 'task_name' of non-object
上而无法进入该阶段
我是Laravel的新手,并且有将表单数据上传到数据库的经验。但是,这种形式与我尝试上传图像数组时有些不同。我什至不确定我是否做对了。阻止我的是表单操作。除了Laravel错误之外,我还在控制台中得到了一些我也不了解的东西。我将Route::resource
用于存储方法,我将在下面更详细地发布。在控制台中,我注意到它正在重复执行任务路由,并且我不知道这是否正常。这是控制台错误:
/tasks/tasks?_token=***Token Info Here***_method=POST&task_name=Toilet+repair+&task_priority=3&task_description=dsfsdf&task_assigned_by=Ren%C3%A9+Gomez&task_assigned_to=John+Whitmore&task_to_be_completed_date=2020-09-13&task_status=Pending&task_notes=sdfsdf&imageName%5B%5D=broken-toilet.jpg&imageName%5B%5D=carpet.jpg&imageName%5B%5D=molding.jpg:1 GET http://taskapp.test/tasks/tasks?_token=***Token Info Here***_method=POST&task_name=Toilet+repair+&task_priority=3&task_description=dsfsdf&task_assigned_by=Ren%C3%A9+Gomez&task_assigned_to=John+Whitmore&task_to_be_completed_date=2020-09-13&task_status=Pending&task_notes=sdfsdf&imageName%5B%5D=broken-toilet.jpg&imageName%5B%5D=carpet.jpg&imageName%5B%5D=molding.jpg 500 (Internal Server Error)
这是我的create.blade.PHP中的表格
<form action="tasks" method="GET" enctype="multipart/form-data" class="mb-5">
{{ csrf_field() }}
@method('POST')
<div class="form-group">
<input
id="task_name"
type="text"
name="task_name"
class="form-control"
placeholder="Task Name" />
</div>
<div class="form-group">
<select
id="task_priority"
type="text"
name="task_priority"
class="form-control"
>
<option value>Select a Priority Level</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="form-group">
<textarea
id="task_description"
type="text"
name="task_description"
class="form-control"
placeholder="Task Description"
></textarea>
</div>
<div class="form-group">
<input
id="task_assigned_by"
type="text"
name="task_assigned_by"
class="form-control"
placeholder="Assigned By"
/>
</div>
<div class="form-group">
<input
id="task_assigned_to"
type="text"
name="task_assigned_to"
class="form-control"
placeholder="Assigned To"
/>
</div>
<div class="form-group">
<label for="task_to_be_completed_date">Due:</label>
<input
id="task_to_be_completed_date"
type="date"
name="task_to_be_completed_date"
class="form-control"
placeholder="Due: "
/>
</div>
<div class="form-group">
<select
id="task_status"
type="text"
name="task_status"
class="form-control"
>
<option value>Select Task Status</option>
<option value="Pending">Pending</option>
<option value="Finished">Finished</option>
<option value="Incomplete">Incomplete</option>
</select>
</div>
<div class="form-group">
<label for="task_notes">Notes:</label>
<textarea
id="task_notes"
type="text"
name="task_notes"
class="form-control"
placeholder="Notes: "
></textarea>
</div>
<label for="imageName">Image(s)</label>
<input type="file" name="imageName[]" multiple />
<button type="submit" class="btn btn-success float-right">Create Task</button>
</form>
web.PHP
Route::resource('tasks','TasksController');
public function create()
{
return view('tasks.create');
}
public function store(Request $request)
{
$this->validate($request,[
'task_name' => 'required','task_description' => 'required'
]);
// Create Task
$task = new Task;
$task->task_name = $request->input('task_name');
$task->task_description = $request->input('task_description');
$task->task_priority = $request->input('task_priority');
$task->task_assigned_by = $request->input('task_assigned_by');
$task->task_assigned_to = $request->input('task_assigned_to');
$task->task_to_be_completed_date = $request->input('task_to_be_completed_date');
$task->task_notes = $request->input('task_notes');
$task->task_status = $request->input('task_status');
$imageName = array();
if ($files = $request->file('imageName')) {
foreach ($files as $file) {
$name = $file->getClientOriginalName();
$file->move('imageName',$imageName);
$imageName[] = $name;
}
}
Task::insert([
'imageName' => implode("|",$imageName)
]);
// $task->imageName = $request->input('imageName');
$task->save();
return redirect('/tasks')->with('success','Task Created');
}
public function show($id)
{
$task = Task::find($id);
return view('tasks.show')->with('task',$task);
}
最后是我的show.blade.PHP
<div class="container">
<a href="/tasks" class="btn bg-purple mb-4">Go Back</a>
<div class="card p-3">
<div class="row">
<div class="col-md-4 col-sm-12">
<h3>Task</h3>
<p>{{ $task->task_name }}</p>
<h3>Assigned On:</h3>
<p>{{ $task->created_at->format('m/d/Y') }}</p>
<h3>Assigned To:</h3>
<p>{{ $task->task_assigned_to }}</p>
</div>
<div class="col-md-4 col-sm-12">
<h3>Task Description</h3>
<p>{{ $task->task_description }}</p>
<h3>Priority</h3>
<p>{{ $task->task_priority }}</p>
<h3>Status</h3>
<p>{{ $task->task_status }}</p>
</div>
<div class="col-md-4 col-sm-12">
<h3>Due:</h3>
<p>{{ $task->task_to_be_completed_date }}</p>
<h3>Notes</h3>
<p>{{ $task->task_notes }}</p>
<h3>Images</h3>
{{-- @if(count($tasks) > 0)
@foreach($tasks as $task)
<img src="{{ $task->imageName }}" alt="">
@endforeach
@else
<p>No Images to show</p>
@endif --}}
</div>
</div>
</div>
</div>
如果有人可以看看并让我知道我可能做错了什么,我将非常感激。如果我缺少任何文件或代码,请告诉我,以便我可以编辑查询。再次感谢您。
解决方法
upload an array of images
和multipart/form-data
要求POST
表单
如果您使用的是Route::resource('tasks','TasksController');
,请添加@method('put')
因此,使method="POST"
<form action="tasks" method="POST" enctype="multipart/form-data" class="mb-5">
@method('put')
并使用它,这样您就不会收到像Trying to get property 'task_name' of non-object
$task = Task::find($id);
到
$task = Task::findOrFail($id);// if task is not present will throw 404 error
我可以看到的另一个问题
Task::insert([
'imageName' => implode("|",$imageName)
]);
应该是
$task->imageName = implode("|",$imageName);