英文:
Call to undefined method App\Models\PDF::loadView()
问题
namespace App\Http\Controllers\Pdf;
use App\Models\PDF;
use App\Models\TestMcq;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class TestPdfController extends Controller
{
public function createPDF($id) {
$TestMcq = TestMcq::where('test_id', $id)->get();
$data = [
'title' => 'Welcome to ItSolutionStuff.com',
'date' => date('m/d/Y')
];
$pdf = PDF::loadView('pdf.test', $data);
return $pdf->download('test.pdf');
}
}
web.php
Route::get('/test/pdf/{id}', [TestPdfController::class, 'createPDF']);
'providers' => ServiceProvider::defaultProviders()->merge([
Mccarlosen\LaravelMpdf\LaravelMpdfServiceProvider::class,
/*
* Package Service Providers...
*/
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Yajra\DataTables\DataTablesServiceProvider::class,
])->toArray(),
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/
'aliases' => Facade::defaultAliases()->merge([
// 'Example' => App\Facades\Example::class,
'PDF' => Mccarlosen\LaravelMpdf\Facades\LaravelMpdf::class,
'DataTables' => Yajra\DataTables\Facades\DataTables::class,
])->toArray(),
I didn't know why it gives me an error?
<details>
<summary>英文:</summary>
controller
namespace App\Http\Controllers\Pdf;
use App\Models\PDF;
use App\Models\TestMcq;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class TestPdfController extends Controller
{
public function createPDF($id) {
$TestMcq = TestMcq::where('test_id', $id)->get();
$data = [
'title' => 'Welcome to ItSolutionStuff.com',
'date' => date('m/d/Y')
];
$pdf = PDF::loadView('pdf.test', $data);
return $pdf->download('test.pdf');
}
}
web.php
Route::get('/test/pdf/{id}', [TestPdfController::class, 'createPDF']);
'providers' => ServiceProvider::defaultProviders()->merge([
Mccarlosen\LaravelMpdf\LaravelMpdfServiceProvider::class,
/*
* Package Service Providers...
*/
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Yajra\DataTables\DataTablesServiceProvider::class,
])->toArray(),
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/
'aliases' => Facade::defaultAliases()->merge([
// 'Example' => App\Facades\Example::class,
'PDF' => Mccarlosen\LaravelMpdf\Facades\LaravelMpdf::class,
'DataTables' => Yajra\DataTables\Facades\DataTables::class,
])->toArray(),
I didn't know why gives me an error ?
...............................................................................................
I don't know why it gives me this error. I tried to deal with it a lot, but it gives me this strange error. I made another file and put exactly the same things in it and it worked, but I need to add it to this project
</details>
# 答案1
**得分**: 0
PDF不存在于app/Models文件夹中。尝试以下操作:
```php
use App\Models\PDF; // 替换为"use PDF"(通过别名调用)或"use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf as PDF"(直接调用)
英文:
PDF doesn't exist in app/Models folder. Try this:
use App\Models\PDF; // replace this with "use PDF" (for calling via alias) OR "use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf as PDF" (for direct calling)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论