laravel PHP 警告:include(C:\xampp\htdocs\hse\vendor\composer/../../app/Buildings.php): 无法打开流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46852504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:50:51  来源:igfitidea点击:

PHP warning: include(C:\xampp\htdocs\hse\vendor\composer/../../app/Buildings.php): failed to open stream

phpmysqllaraveleloquentlaravel-eloquent

提问by Q8root

I've previously manually deleted some models files in App* and created other with just removing the "s" symbol as because laravel by default can recognise the models from there name , and models by Laravel standadrd must be written normal not in plural.

我之前手动删除了 App* 中的一些模型文件,并通过删除“s”符号创建了其他文件,因为默认情况下 Laravel 可以识别来自 name 的模型,并且 Laravel 标准的模型必须正常书写而不是复数形式。

Before deleting the models that i created them using php artisan make:model Buildings -m

在删除我使用 php artisan make:model Buildings -m 创建的模型之前

The new model i created after i removed the (buildings) model is php artisan make:model Building Notice that i just created a new model without 's'

我删除(建筑物)模型后创建的新模型是 php artisan make:model Building 请注意,我刚刚创建了一个没有 's' 的新模型

Now in my User model i have created method :

现在在我的用户模型中,我创建了方法:

public function UserAssignedBuilding(){
        return $this->hasManyThrough('App\Building','App\Area','user_id','area_id');
    }

Building.php Model file

Building.php 模型文件

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Building extends Model
{
    protected $table = 'buildings';

    public function areas(){
        $this->belongsTo('App\Area');
    }

}

Area.php Model file:

Area.php 模型文件:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Area extends Model
{
    protected $fillable = [
        'name'
    ];

    public function users(){
        return $this->belongsToMany('App\User','area_user','area_id','user_id');
    }

    public function buildings(){
        return $this->hasMany('App\Building');
    }
}

In php artisan when i run the following command, to get the user assigned buildings:

在 php artisan 中,当我运行以下命令时,获取用户分配的建筑物:

>>> User::find(4)->UserAssignedBuilding
PHP warning:  include(C:\xampp\htdocs\hse\vendor\composer/../../app/Buildings.php): failed to open stream: No such file or directory in C:\xampp\htdocs\hse\vendor\composer\ClassLoader.php on line 444

It seems that the error is the framework trying to load the (Buildings.php) model file, which i had already delete it and created (Building.php) instead .

似乎错误是框架试图加载 (Buildings.php) 模型文件,我已经删除它并创建了 (Building.php) 代替。

I run the following :

我运行以下:

C:\xampp\htdocs\hse>composer dumpautoload
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Package manifest generated successfully.

But issue not fixed

但问题未解决

Also tried to get buildings rows, other error showing:-

还尝试获取建筑物行,其他错误显示:-

>>> Building::all()
PHP Fatal error:  Class 'Building' not found in eval()'d code on line 1

采纳答案by ljubadr

In your command you are missing model namespace

在您的命令中,您缺少模型命名空间

It should be

它应该是

App\User::find(4)->UserAssignedBuilding

And

App\Building::all()

Also change method UserAssignedBuildingto buildings

也将方法更改UserAssignedBuildingbuildings

回答by Q8root

The error is showing in Tinker (php artisan tinker) , When i dumped the composer autoload file

错误显示在 Tinker (php artisan tinker) 中,当我转储 Composer 自动加载文件时

composer dump-autoload

Then tried again in tinker , the same error is showing like the composer dump command did not do it work, but when i close the current tinker and open new one (php artisan tinker) , every thing now work perfectly .

然后在 tinker 中再次尝试,同样的错误显示为 composer dump 命令没有起作用,但是当我关闭当前的 tinker 并打开新的(php artisan tinker)时,现在每件事都可以完美运行。

So , the solution in brief is when you run the dumpautoload command, you need to restart the tinker to see the changes.

所以,简单的解决方案是,当您运行 dumpautoload 命令时,您需要重新启动 tinker 以查看更改。

回答by Gp17

I faced the same issue but in my case the problem was case sensitivity of composer autoload.

我遇到了同样的问题,但就我而言,问题是作曲家自动加载的区分大小写。

I had created userclass (with small 'u') using composer artisan make. So it created user.phpfile for me. Afterward, I decided I like User(with capital 'U') more, so I changed the class name but not the containing PHP file.

我已经user使用composer artisan make. 所以它user.php为我创建了文件。之后,我决定更喜欢User(大写'U'),所以我更改了类名,但没有更改包含的 PHP 文件。

So the problem was composer was looking for User.phpfile instead of user.php. changing the containing file to User.phpdid the job for me.

所以问题是作曲家正在寻找User.php文件而不是user.php. 更改包含文件来User.php为我完成这项工作。