我得到這個錯誤,說:「模型[App \ dummy]沒有查詢結果。」我相信問題出在控制器上。當你提交表單時,它應該在評論控制器中觸發該功能。這個控制器是新的,所以我相信錯誤在這裏。那是當它停止工作。這裏是commentController文件:爲什麼不laravel識別我的模型?
<?php
namespace App\Http\Controllers;
use App\Dummy;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; //this one is for sql builders
use App\Comments;
use Illuminate\Http\RedirectResponse;
use DateTime; //to create a new date object you need to include this namespace
class commentController extends Controller
{
public function store(Dummy $post){
$date = new DateTime();
$timestamp = $date->getTimestamp();
$id = $post->id;
$post->addComment(request('body'));
return view('post', compact('post', 'timestamp', 'id'));
}
}
我試着讓App \ Dummy小寫,所以它是App \ dummy,但它仍然沒有工作。它仍然給我錯誤。
這裏是我的假人模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class dummy extends Model
{
protected $guarded = [];
public function comments(){
return $this->hasMany(Comments::class, 'post_id');
}
public function addComment($body){
$this->comments()->create(compact('body'));
}
}
在這裏顯示您的模型文件 –
@Kris Roofe我在主要帖子上張貼了我的虛擬模型。 – Lami