所以我试图打开包含报告数据的刀片之一。但是,它让我发送这样的错误,这是控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Report;
class ReportController extends Controller
{
public function approve(Request $request, $id)
{
$report = Report::findOrFail($id);
$data = $request->validate([
'response_message' => 'required|string|max:1000',
]);
$report->update([
'status' => 'approved',
'response_message' => $data['response_message'],
]);
session()->flash('success', 'Laporan berhasil di-ACC dan pesan balasan telah dikirim.');
return redirect()->back();
}
public function store(Request $request)
{
$user = auth()->id();
if ($user === null) {
return redirect()->route('login')->with('error', 'User ID tidak ditemukan. Pastikan Anda sudah login.');
}
$data = $request->validate([
'reason' => 'required|string|max:255',
'description' => 'nullable|string',
'reportable_id' => 'required|integer',
'reportable_type' => 'required|string',
]);
$data['user_id'] = auth()->id();
Report::create($data);
return response()->json(['success' => true]);
}
public function showReports()
{
$reports = Report::with('user')->latest()->get();
return view('admins.adminnotifikasi', compact('reports'));
}
public function userShow()
{
$reports = Report::where('user_id', auth()->id())
->where('status', 'approved')
->get();
return view('pages.utama.user.show', compact('reports'));
}
public function deleteReport($id)
{
$report = Report::findOrFail($id);
if (auth()->user()->role !== 'admin' && $report->user_id !== auth()->id()) {
return redirect()->back()->with('error', 'Anda tidak memiliki izin untuk menghapus laporan ini.');
}
$report->delete();
session()->flash('success', 'Laporan berhasil dihapus.');
return redirect()->back();
}
}
这是模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Report extends Model
{
use HasFactory;
protected $table = 'reports';
protected $fillable = [
'reason',
'description',
'user_id',
'status',
'response_message',
'reportable_id',
'reportable_type',
];
protected static function booted()
{
static::creating(fn($report) => $report->user_id = Auth::id());
}
public function user()
{
return $this->belongsTo(User::class, 'user_id'); // pastikan 'user_id' adalah foreign key yang benar
}
public function reportable()
{
return $this->morphTo();
}
}
下面是我报告的型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
class AdaApa extends Model
{
use HasFactory;
protected $table = 'adaapa';
protected $fillable = ['title', 'content', 'image'];
public function comments(): MorphMany
{
return $this->morphMany(Comment::class, 'commentable');
}
public function likes(): MorphMany
{
return $this->morphMany(Like::class, 'likeable');
}
public function reports(): MorphMany
{
return $this->morphMany(Report::class, 'reportable');
}
}
用户模型:
<?php
namespace App\Models;
use App\Http\Controllers\DiscussionsController;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'username', // Tambahkan username di sini
'email',
'password',
'role',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
public function discussions()
{
return $this->hasMany(Discussions::class);
}
/**
* Relasi ke model Report.
*/
public function reports()
{
return $this->hasMany(Report::class);
}
}
我的数据库没有任何问题,我将其成功发送到我的数据库这里。所以我的路线也没有任何问题,但我不知道为什么它总是说这样的错误。这也是我用来显示数据的刀片:
@foreach ($reports as $report)
<div class="alert alert-info">
<strong>Nama User:</strong> {{ $report->user->name ?? 'Tidak diketahui' }}<br>
<strong>Berita yang Di-Report:</strong> {{ $report->reportable->title ?? 'Tidak diketahui' }}<br>
<strong>Alasan:</strong> {{ $report->reason }}<br>
<strong>Alasan:</strong> {{ $report->status }}<br>
<strong>Deskripsi:</strong> {{ $report->description ?? 'Tidak ada deskripsi' }}<br>
<small>Dilaporkan pada: {{ $report->created_at }}</small>
<!-- Form untuk ACC dan mengirim pesan balasan ke user -->
<form action="{{ route('report.approve', $report->id) }}" method="POST" class="mt-3">
@csrf
<div class="mb-2">
<label for="responseMessage">Pesan Balasan:</label>
<textarea name="response_message" class="form-control" rows="2" placeholder="Masukkan pesan balasan untuk user..."></textarea>
</div>
<button type="submit" class="btn btn-success">Approve Report</button>
</form>
<form action="{{ route('report.delete', $report->id) }}" method="POST" class="mt-2" onsubmit="return confirm('Apakah Anda yakin ingin menghapus laporan ini?');">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete Report</button>
</form>
</div>
@endforeach
该错误似乎是由于多态关系设置问题造成的。检查
reportable_type
和 reportable_id
,确保报告中的 reportable_type
具有正确的型号名称 App\Models\AdaApa
,并且 reportable_id
引用正确的记录。更新下面AdaApa
中的关系。
public function reports(): MorphMany
{
return $this->morphMany(Report::class, 'reportable');
}
在刀片模板中,还要确认
{{ $report->reportable->title }}
正确引用数据。