問題描述
向數據庫中插入數據失敗,IDE (phpStrom) 中未找到所有查詢類和模型類的方法,我該如何解決?
failed insert data into database, and all query class and Model class's method not found show in IDE (phpStrom) how can I solve it?
這是我的擴展類(Post.php),這里顯示最新的錯誤和方法:
here is my extended class (Post.php) here show error in latest and where method:
<?php namespace App;
use CarbonCarbon;
use IlluminateDatabaseEloquentModel;
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 IlluminateDatabaseEloquentRelationsBelongsTo
*/
public function user(){
return $this->belongsTo('AppUser');
}
}
這是我使用它的控制器類:
and Here is my Controller class where i use it :
<?php namespace AppHttpControllers;
use AppHttpRequests;
use AppHttpRequestsCreatePostRequest;
use AppPost;
use Request;
use IlluminateSupportFacadesAuth;
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 IlluminateViewView
* @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 IlluminateHttpRedirectResponse|IlluminateRoutingRedirector
*/
public function store(CreatePostRequest $request)
{
//validation
$this->createPost($request);
// flash('Your tolet has been created!')->important();
return redirect('tolet.index');
}
/**
* @param Post $post
* @return IlluminateViewView
* @internal param Articles $article
*/
public function edit(Post $post)
{
return view('tolet.edit', compact('post'));
}
/**
* @param Post $post
* @param CreatePostRequest $request
* @return IlluminateHttpRedirectResponse|IlluminateRoutingRedirector
* @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;
}
}
推薦答案
如果你想要一個擴展 Model
的類來識別 Eloquent 方法,只需在該類的 PHPDoc 頂部注釋中添加以下內容:
If you want a class extending Model
to recognize Eloquent methods, just add the following in the top PHPDoc comment on the class:
@mixin Eloquent
示例:
<?php namespace App;
use CarbonCarbon;
use Eloquent;
use IlluminateDatabaseEloquentModel;
/**
* Post
*
* @mixin Eloquent
*/
class Post extends Model {
編輯 Laravel 6+
use IlluminateDatabaseEloquentBuilder;
/**
* @mixin Builder
*/
注意:你們中的大多數人可能正在為 Laravel 使用 ide-helper,因此這個 @mixin
屬性為模型類自動生成.
Note:
Most of you probably are using ide-helper for Laravel, therefore this @mixin
attribute is automatically generated for model Classes.
這篇關于PHPStorm 無法識別 Laravel 5.0 中我的模型類的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!