我是Laravel 9的初学者。当我点击编辑表单时,我会收到错误,点击编辑表单后,我无法提交编辑,而是点击侧边栏中的 Jmeter 板菜单,在给出错误Trying to get property 'slug' of non-object
后,我查看URL,URL更改为http://127.0.0.1:8000/laporan-edit/dashboard
,应该有链接http://127.0.0.1:8000/dashboard
。
我的控制器
<?php
namespace App\Http\Controllers;
use App\Models\Laporan;
use Illuminate\Http\Request;
class LaporanController extends Controller
{
public function index()
{
$laporan = Laporan::all();
return view('laporan', ['laporan' => $laporan]);
}
public function add()
{
return view('laporan-add');
}
public function store(Request $request)
{
$validated = $request->validate([
'nama_korban' => 'required|max:255'
]);
if ($request->file('foto')) {
$extension = $request->file('foto')->getClientOriginalExtension();
$NamaBaru = $request->nik_korban . '-' . now()->timestamp . '.' . $extension;
$request->file('foto')->storeAs('fotokorban', $NamaBaru);
}
$request['foto_korban'] = $NamaBaru;
Laporan::create($request->all());
return redirect('laporan')->with('status', 'Data Telah Ditambahkan !');
}
public function edit($slug)
{
$laporan = Laporan::where('slug', $slug)->first();
return view('laporan-edit', ['laporan' => $laporan]);
}
public function update(Request $request, $slug)
{
$laporan = Laporan::where('slug', $slug)->first();
$laporan->update($request->all());
return redirect('laporan')->with('status', 'Data Telah Di Edit');
}
}
我的模型文件
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Laporan extends Model
{
use HasFactory;
use Sluggable;
protected $fillable = [
'petugas_pendamping', 'kronologi_kejadian', 'id_pengguna', 'nama_pelaku', 'jenis_kasus', 'foto_korban', 'tempat_kejadian', 'alamat_kejadian', 'wilayah_pendamping', 'korban', 'slug', 'nama_pelapor', 'nik_pelapor', 'alamat_pelapor', 'hubungan_pelapor', 'nik_korban', 'nama_korban', 'jk_korban', 'umur_korban', 'alamat_korban', 'nik_pelaku', 'jk_pelaku', 'umur_pelaku', 'alamat_pelaku', 'tanggal_pengaduan', 'tanggal_kejadian', 'jam_pengaduan', 'status_laporan'
];
public function sluggable(): array
{
return [
'slug' => [
'source' => 'nama_pelapor'
]
];
}
}
我的路线
<?php
use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RekapController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\PetugasController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
})->middleware('auth');
Route::middleware('only_guest')->group(function () {
Route::get('login', [AuthController::class, 'login'])->name('login');
Route::post('login', [AuthController::class, 'authenticating']);
});
Route::middleware('auth')->group(function () {
Route::get('logout', [AuthController::class, 'logout']);
Route::get('dashboard', [DashboardController::class, 'index'])->middleware('only_admin');
Route::get('petugas', [PetugasController::class, 'petugas'])->middleware('only_petugas');
Route::get('rekaplaporan', [RekapController::class, 'index']);
Route::get('laporan', [LaporanController::class, 'index']);
Route::get('laporan-add', [LaporanController::class, 'add']);
Route::post('laporan-add', [LaporanController::class, 'store']);
Route::get('laporan-edit/{slug}', [LaporanController::class, 'edit']);
Route::put('laporan-edit/{slug}', [LaporanController::class, 'update']);
Route::get('user', [UserController::class, 'index']);
Route::get('profile', [ProfileController::class, 'index']);
});
查看edit. blade.php
<div class="card-body">
<form action="/laporan-edit/{{ $laporan->slug }}" method="post" enctype="multipart/form-data" class="form-horizontal">
@csrf
@method('put')
我已经尝试过这种情况
public function edit($slug)
{
if($laporan = Laporan::where('slug', $slug)->first()){
return view('laporan-edit', ['laporan' => $laporan]);
} else{
return redirect('dashboard');
}
// $laporan = Laporan::where('slug', $slug)->first();
// return view('laporan-edit', ['laporan' => $laporan]);
}
它的工作,但是,当我点击侧边栏另一个名字,然后它总是重定向http://127.0.0.1:8000/dashboard
1条答案
按热度按时间kxeu7u2r1#
在刀片服务器侧菜单中,使用绝对链接
href="/dashboard"
代替相对链接href="dashboard"
。或者使用命名路由,方法是为路由指定别名/名称,并使用它来生成链路
在叶片中
您还应该使用
$laporan = Laporan::where('slug', $slug)->firstOrFail();
而不是->first()
,以避免不必要的错误,并在链接不正确时显示404。