php - 从 laravel 中的空值创建默认对象时出错
问题描述
所以每次我尝试编辑某些东西时都会收到这个错误。我最好的猜测是我没有正确链接某些东西。或者它根本不起作用。
这是我收到错误时的 URL: http://sde2.test/ticket/%7Bid%7D 据我所知,当我尝试保存它时,它没有得到正确的 id。
无论如何,提前感谢您的回答!
这是我的票务控制器:
<?php
namespace App\Http\Controllers;
use App\Ticket;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Redirect, Response;
class TicketController extends Controller
{
public function setUpdate($ticketId, Request $request)
{
$ticket = Ticket::find($ticketId);
$ticket->name = $request->name;
$ticket->email = $request->email;
$ticket->phonenr = $request->phonenr;
$ticket->desc = $request->desc;
$ticket->update = $request->update;
$ticket ->save();
return Redirect::to('ticket')->withSuccess('Het is gelukt, ticket is geupdate!');
}
还有我的票模型:
class Ticket extends Model
{
protected $fillable = [
'name',
'email',
'phonenr',
'desc',
'update'
];
protected $dates = [
'created_at',
'updated_at'
];
}
这是我的票证:
<body>
<br>
<h1 class="d-flex justify-content-center">Tickets</h1>
<br>
<div class="row d-flex justify-content-center">
<div class="col-md-6">
@if($message = Session::get('success'))
<div class="row alert alert-success">
<strong>{{ $message }}</strong>
</div>
@endif
</div>
</div>
<div class="container-lg d-flex justify-content-start">
<form action="/ticketzoek" method="get">
<div class="input-group">
<input type="text" name="zoeken" placeholder="Zoeken op naam.." class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-secondary">Zoeken</button>
</span>
</div>
</form>
</div>
<div class="container-lg d-flex justify-content-center table-responsive-md">
<table id="tickets" class="table table-striped table-hover table-bordered" style="width:100%">
<thead>
<tr>
<th>Naam</th>
<th>Email</th>
<th>Telefoon nummer</th>
<th>Probleem</th>
<th>Updates</th>
<th>Datum ingediend</th>
<th>Status</th>
<th>Aanpassen</th>
<th>Verhelpen</th>
<th>Verwijderen</th>
</tr>
</thead>
@foreach($tickets as $ticket)
<tbody>
<tr>
<td>{{ $ticket->name }}</td>
<td>{{ $ticket->email }}</td>
<td>{{ $ticket->phonenr }}</td>
<td>{{ $ticket->desc }}</td>
<td>{{ $ticket->update }}</td>
<td>{{ Carbon\Carbon::parse($ticket->created_at)->format('d-m-Y') }}</td>
@if($ticket->solved == 0)
<td><a class="text-danger">Niet opgelost</a></td>
@endif
<td>
<div class="d-flex justify-content-center">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#view_{{$ticket->id}}">Aanpassen</button>
</div>
<br>
<div class="modal fade bd-example-modal-lg" id="view_{{$ticket->id}}" tabindex="-1" role="dialog" aria-labelledby="loaning" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="loaning">Tickets gegevens</h5>
<button type="button" class="btn btn-secondary" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row m-0" style="background-color: #005b82; padding-top: 20px">
<h2 class="d-flex justify-content-center">Aanpassen ticket</h2>
<form action="ticket/{id}" method="post">
@csrf
<br>
<div class="row d-flex justify-content-center">
<div class="col-md-6">
<label for="name">Naam:</label>
<input type="name" class="form-control" name="name" value="{{ $ticket->name }}">
</div>
</div>
<div class="row d-flex justify-content-center">
<div class="col-md-6">
<label for="email">Email:</label>
<input type="text" class="form-control" name="email" value="{{ $ticket->email}}">
</div>
</div>
<div class="row d-flex justify-content-center">
<div class="col-md-6">
<label for="phonenr">Telefoon nummer:</label>
<input type="text" class="form-control" name="phonenr" value="{{ $ticket->phonenr}}">
</div>
</div>
<div class="row d-flex justify-content-center">
<div class="col-md-6">
<label for="desc">Probleem:</label>
<input type="text" class="form-control" name="desc" value="{{ $ticket->desc}}">
</div>
</div>
<div class="row d-flex justify-content-center">
<div class="col-md-6">
<label for="update">Updates:</label>
<textarea class="form-control" rows="3" name="update">{{ $ticket->update}}</textarea>
</div>
</div>
<br>
<br>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Sluiten</button>
<button class="btn btn-primary" type="submit">Aanpassen</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</td>
<td>
<form method="post" action="{{ route('ticket.approve', ['id' => $ticket->id]) }}">
@csrf
<input type="hidden" name="ticketID" value="{{$ticket->id}}"/>
<button class="btn btn-success" type="submit">
Klaar
</button>
</form>
</td>
<td>
<form method="post" action="{{ route('ticket.delete', ['id' => $ticket->id]) }}">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Verwijderen</button>
</form>
</td>
</tr>
</tbody>
@endforeach
</table>
</div>
</body>
这是我的 web.php
Route::get('/ticket', 'TicketController@show');
Route::post('/ticket/{id}', 'TicketController@setUpdate')->name('ticket.update');
Route::post('/ticket/{id}', 'TicketController@setSolved')->name('ticket.approve');
Route::delete('/ticket/{id}', 'TicketController@setDelete')->name('ticket.delete');
解决方案
如果我对您的理解正确,则由于 URL 错误,您无法编辑模型。解决方案是更改刀片中的一些代码:
<form action="ticket/{id}" method="post">
至
<form method="post" action="{{ route('ticket.update', ['id' => $ticket->id]) }}">
还要检查approve
andupdate
方法,它们的 url 和 HTTP 方法是一样的,不认为它很好。
推荐阅读
- sql - 将每一行的计算结果用于下一行并进一步
- html - 刷新输入类型文件
- ios - Invertase:带有 iOS 设备的 Firebase 云消息传递 (FCM)
- azure-application-insights - 无代码 Application Insights 自定义计数器
- java - 抓取 Cloudflare 站点
- php - 在 Laravel 中映射数据
- java - 如何使用 JXL 库通过 Java 创建和更新 Excel 文件?
- spring - 瓦丁。同时运行 2 个 spring 应用程序
- json - JSON.parse 与 redux 状态有关的问题
- python - 机器人自动化:即使一个测试用例失败也继续循环