Call to undefined method App\Models\PDF::loadView()

huangapple go评论71阅读模式
英文:

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,
])-&gt;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 &quot;lazy&quot; loaded so they don&#39;t hinder performance.
|
*/

&#39;aliases&#39; =&gt; Facade::defaultAliases()-&gt;merge([
    // &#39;Example&#39; =&gt; App\Facades\Example::class,
    &#39;PDF&#39; =&gt; Mccarlosen\LaravelMpdf\Facades\LaravelMpdf::class,
    &#39;DataTables&#39; =&gt; Yajra\DataTables\Facades\DataTables::class,
])-&gt;toArray(),

I didn&#39;t know why gives me an error ?

...............................................................................................

I don&#39;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 &quot;use PDF&quot; (for calling via alias) OR &quot;use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf as PDF&quot; (for direct calling)

huangapple
  • 本文由 发表于 2023年6月16日 10:22:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定