无法将数据插入数据库,并且找不到IDE(phpStrom)中显示的所有查询类和Model类的方法如何解决?
这里是我的扩展类(Post.php),这里显示最新错误和where方法:
<?php namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
protected $fillable=[
'title',
'description',
'location',
'contact',
'type',
'published_at'
];
protected $date=['published_at'];
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
/**
* @param $query
*/
public function scopePublished($query)
{
$query->where('published_at', '<=', Carbon::now());
}
public function scopeUnPublished($query)
{
$query->where('published_at', '>=', Carbon::now());
}
/**
* An post is owned by a user.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(){
return $this->belongsTo('App\User');
}
}
以下是我使用它的Controller类:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Requests\CreatePostRequest;
use App\Post;
use Request;
use Illuminate\Support\Facades\Auth;
use Session;
class PostsController extends Controller {
//
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
//return \Auth::user()->name;
$posts = Post::latest('published_at')->published()->get();
$latest= Post::latest()->first();
return view('tolet.index', compact('posts','latest'));
}
/**
* @param Post $post
* @return \Illuminate\View\View
* @internal param Articles $article
* @internal param Articles $articles
*/
public function show(Post $post)
{
return view('tolet.show', compact('post'));
}
public function create()
{
if (Auth::guest()) {
return redirect('tolet.index');
}
return view('tolet.create');
}
/**
* @param CreatePostRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(CreatePostRequest $request)
{
//validation
$this->createPost($request);
// flash('Your tolet has been created!')->important();
return redirect('tolet.index');
}
/**
* @param Post $post
* @return \Illuminate\View\View
* @internal param Articles $article
*/
public function edit(Post $post)
{
return view('tolet.edit', compact('post'));
}
/**
* @param Post $post
* @param CreatePostRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @internal param Articles $article
* @internal param $id
*/
public function update(Post $post, CreatePostRequest $request)
{
$post->update($request->all());
return redirect('tolet.index');
}
/**
* sync up the list of tags in the database
*
* @param Post $post
*/
/**
* save a new post
*
* @param CreatePostRequest $request
* @return mixed
*/
private function createPost(CreatePostRequest $request)
{
$post = Auth::user()->posts()->create($request->all());
return $post;
}
}
答案 0 :(得分:23)
由于方法where
,latest
,find
,findOrFail
和其他方法在Model
类中不存在,但在Builder
中且不存在通过魔术方法加载,IDE无法检测到这些。
虽然广泛建议的laravel-ide-helper很棒,但也没有帮助。关于这个主题有多个issues和discussions以及解决方法,但都有自己的问题。
迄今为止我发现的最佳解决方案,如果课程中存在__magic方法,恕我直言将降级严重程度。 PhpStorm在检查设置中有这个确切的选项。
签入Settings -> Inspections -> PHP -> Undefined -> Undefined method
这不会让您点击该方法,只是禁用恼人的标记。 Read more about severities或查看此more expressive SO answer
答案 1 :(得分:19)
如果您想扩展Model类以识别Eloquent方法,只需添加上面的类PHPDoc注释
@mixin Eloquent
实施例
<?php namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Post
*
* @mixin Eloquent
*/
class Post extends Model {
答案 2 :(得分:6)
对于任何来这里寻求解决方案的人来说,对我有用的是此StackOverflow中的解决方案:
PhpStorm laravel 5 method not found
特别是在我跑步时:
php artisan ide-helper:models
回答“是”
在此方法被识别之后。
答案 3 :(得分:4)
我的课。注释将帮助PhpStorm识别这些方法。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;
/**
* @method static Builder where($column, $operator = null, $value = null, $boolean = 'and')
* @method static Builder create(array $attributes = [])
* @method public Builder update(array $values)
*/
class User extends Model
{
protected $table = 'users';
protected $fillable = [
'email',
'name',
'password',
];
}
答案 4 :(得分:3)
Laravel 8.x:
将Traceback (most recent call last):
File "/Users/Anu/PycharmProjects/pythonProject3/Demo/test1.py", line 4, in <module>
driver = webdriver.Chrome("../Drivers/chromedriver.exe")
File "/Users/Anu/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Users/Anu/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
添加到类注释:
@mixin
答案 5 :(得分:2)
添加到您的所有模型有点烦人,但您可以将该方法添加到模型docblock中。这将使它在PHPStorm中正常工作。
/*
* @method static \Illuminate\Database\Query\Builder|\App\MyModelName where($field, $value)
*/
答案 6 :(得分:1)
我是laravel的新手,所有关于模型和phpstorm的问题都很奇怪。这是一个很大的缺点。 像添加@mixin Eloquent或运行php artisan ide-helper:models之类的解决方案对我不起作用。 PHPStorm找不到Eloquent或\ Eloquent。 ide-helper:models不会添加所有可用的静态方法。 因此,我带来了一个自己的基本模型,其中包含具有所有相关模型方法的php文档:
<?php
namespace App;
use Closure;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
* Class BaseModel
* @package App
* @method EloquentModel|Collection|null static $this find($id, $columns = ['*']) Find a model by its primary key.
* @method EloquentModel|EloquentBuilder|null first($columns = ['*']) Execute the query and get the first result.
* @method EloquentModel|EloquentBuilder firstOrFail($columns = ['*']) Execute the query and get the first result or throw an exception.
* @method Collection|EloquentBuilder[] get($columns = ['*']) Execute the query as a "select" statement.
* @method mixed value($column) Get a single column's value from the first result of a query.
* @method mixed pluck($column) Get a single column's value from the first result of a query.
* @method void chunk($count, callable $callback) Chunk the results of the query.
* @method \Illuminate\Support\Collection lists($column, $key = null) Get an array with the values of a given column.
* @method LengthAwarePaginator paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) Paginate the given query.
* @method Paginator simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page') Paginate the given query into a simple paginator.
* @method int increment($column, $amount = 1, array $extra = []) Increment a column's value by a given amount.
* @method int decrement($column, $amount = 1, array $extra = []) Decrement a column's value by a given amount.
* @method void onDelete(Closure $callback) Register a replacement for the default delete function.
* @method EloquentModel[] getModels($columns = ['*']) Get the hydrated models without eager loading.
* @method array eagerLoadRelations(array $models) Eager load the relationships for the models.
* @method array loadRelation(array $models, $name, Closure $constraints) Eagerly load the relationship on a set of models.
* @method static EloquentBuilder where($column, $operator = null, $value = null, $boolean = 'and') Add a basic where clause to the query.
* @method EloquentBuilder orWhere($column, $operator = null, $value = null) Add an "or where" clause to the query.
* @method EloquentBuilder has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) Add a relationship count condition to the query.
* @method static EloquentBuilder find($value)
* @method static EloquentBuilder orderBy($column, $direction = 'asc')
* @method static EloquentBuilder select($columns = ['*'])
*
*
* @method static QueryBuilder whereRaw($sql, array $bindings = [])
* @method static QueryBuilder whereBetween($column, array $values)
* @method static QueryBuilder whereNotBetween($column, array $values)
* @method static QueryBuilder whereNested(Closure $callback)
* @method static QueryBuilder addNestedWhereQuery($query)
* @method static QueryBuilder whereExists(Closure $callback)
* @method static QueryBuilder whereNotExists(Closure $callback)
* @method static QueryBuilder whereIn($column, $values)
* @method static QueryBuilder whereNotIn($column, $values)
* @method static QueryBuilder whereNull($column)
* @method static QueryBuilder whereNotNull($column)
* @method static QueryBuilder orWhereRaw($sql, array $bindings = [])
* @method static QueryBuilder orWhereBetween($column, array $values)
* @method static QueryBuilder orWhereNotBetween($column, array $values)
* @method static QueryBuilder orWhereExists(Closure $callback)
* @method static QueryBuilder orWhereNotExists(Closure $callback)
* @method static QueryBuilder orWhereIn($column, $values)
* @method static QueryBuilder orWhereNotIn($column, $values)
* @method static QueryBuilder orWhereNull($column)
* @method static QueryBuilder orWhereNotNull($column)
* @method static QueryBuilder whereDate($column, $operator, $value)
* @method static QueryBuilder whereDay($column, $operator, $value)
* @method static QueryBuilder whereMonth($column, $operator, $value)
* @method static QueryBuilder whereYear($column, $operator, $value)
*/
abstract class BaseModel extends Model
{
}
然后我自己的模型扩展了该模型:
<?php
namespace Modules\Shop\Entities;
use App\BaseModel;
class MyEntity extends BaseModel
然后一切正常。 BaseModel现在还不完整,请随时添加其他静态方法,我可以根据需要添加它们。
答案 7 :(得分:1)
您可以将Scaffold(
resizeToAvoidBottomPadding: false,
添加到@mixin QueryBuilder
类的phpdoc中
文件路径:Model
答案 8 :(得分:0)
这个问题可以回答&#34;,你需要laravel ide-helper。关注these instructions,一切都应该适合你。
答案 9 :(得分:0)
同意并+1 +1 @rutter 。我会补充一点,因为我专注于Laravel项目,所以这个问题一直在我的脸上。
Barry的Laravel-IDE Git非常适合主打&#39;方法,它无法真正捕捉到每一个问题,Laravel的供应商包范围(后来通过其他类/方法调用)会发生很多事情。
我把球丢在那里但是如果intelliJ会创建一个能够尝试/捕捉设定魔术方法的编译器(在他们设置之后)只需按一下按钮 (返回一个bool并成功通往该方法的路径)好吧......那会很棒。
关闭严重性是处理它的一种很酷的方式(上面详细说明),但是你仍然没有方便的快捷方式,甚至让你知道它存在。
建设性地,我建议他们使同步按钮意味着什么(除了刷新)。
答案 10 :(得分:0)
尝试了Barry的_ide_help.php
解决方案后,我发现了一个可行且简单的解决方案。
展示解决方案的Laracast视频:https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15
-在第一条评论中,您可以找到Barry的链接。
添加此内容后,它对我不起作用,但为了完整起见,我仍在提及它。
然后我尝试了这个:
在模型中,我在顶部添加了use Eloquent;
。 (我通过自动完成而不是键入方式添加了Eloquent)。
然后在我的课程上方,我键入“ / ** hit ENTER”,它会自动生成PHP文档
在新生成的PHP文档中,我在下面添加了@mixin Eloquent
。
作为最后一步,我点击了 Ctrl + Alt + Y (默认设置),该文件在PhpStorm中进行同步(文件->同步) 。
这已修复警告,并在我的Controller中找到了:: find方法,并且自动补全正在工作。
下面以我的班级为例:
namespace App;
use Illuminate\Database\Eloquent\Model; <br>
use Eloquent;
/**
* Class Student
* @package App
* @mixin Eloquent
*/
class Student extends Model <br>
{
}
答案 11 :(得分:0)
我已经解决了这种方式。
Barryvdh附带了对Laravel的出色IDE支持:
https://github.com/barryvdh/laravel-ide-helper
安装后,只需在控制台中调用:
php artisan ide-helper:generate
在_ide_helper.php文件中生成alll facede快捷方式(您必须从git中排除它)
PhpStorm还有一些特别之处:
php artisan ide-helper:meta
答案 12 :(得分:0)
我用这种方法解决了这个问题,而不使用任何插件: 只需在模型名称后使用query() 例如:
Model_name::query()->select('column_name')->where('some_where')->get();
然后识别phpstorm!
答案 13 :(得分:0)
这里的所有解决方案都不适用于不建议或不方便修改供应商文件的包模型。
ide-helper 的另一个工具是 php artisan ide-helper:eloquent
这会将 /** @mixin \Eloquent */
直接写入 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php
文件。
这将对从 Model 扩展的任何模型的问题进行排序。
虽然这确实写入了供应商文件,但它是可管理的,并且只需要在更新 laravel 时再次执行。您可以在 Composer 脚本中添加一行,以在每个开发者 Composer 安装时实现这一点(对团队有利)。