没有找到Laravel 5.5 Trait(Laravel 5.5 Trait not found)
我无法绕过这个,因为我在另一个项目上有这个并且工作得很好。 它目前给我这个错误
Symfony \ Component \ Debug \ Exception \ FatalErrorException(E_UNKNOWN)Trait'App \ Traits \ ResponseTrait'未找到
UserRepository工作正常,特性没有。 我已经尝试重命名特征,将其移动到存储库文件夹,chmod到777,我不知道什么是错的。
认识我的HomeController,laravel的默认值:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Repositories\UserRepository as User; use App\Traits\ResponseTrait; class HomeController extends Controller { use ResponseTrait; private $user; /** * Create a new controller instance. * * @return void */ public function __construct(User $user) { $this->user = $user; $this->middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function index() { //dd($this->user->all()->toArray()); //You can ignore the self:: part, as it doesn't reach this far. self::setData($this->user->all()->toArray()); return "test"; return view('home'); } }
特点:
<? namespace App\Traits; trait ResponseTrait { public function setData($array) { } }
特征的路径是/app/Traits/ResponseTrait.php
在我的composer.json中,我已经使用App指令进行了PSR-4自动加载。
"psr-4": { "App\\": "app/" }
Edit1:我尝试过composer dump-autoload,问题仍然存在
I can't wrap my head around this, as I have this on another project and works just fine. It is currently giving me this error
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Trait 'App\Traits\ResponseTrait' not found
The UserRepository works just fine, the trait does not. I've tried renaming the trait, moving it to the repository folder, chmod it to 777, and I have no idea what could be wrong.
Meet my HomeController, the laravel's default:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Repositories\UserRepository as User; use App\Traits\ResponseTrait; class HomeController extends Controller { use ResponseTrait; private $user; /** * Create a new controller instance. * * @return void */ public function __construct(User $user) { $this->user = $user; $this->middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function index() { //dd($this->user->all()->toArray()); //You can ignore the self:: part, as it doesn't reach this far. self::setData($this->user->all()->toArray()); return "test"; return view('home'); } }
The trait:
<? namespace App\Traits; trait ResponseTrait { public function setData($array) { } }
The trait's path is /app/Traits/ResponseTrait.php
At my composer.json, I've got PSR-4 autoload with the App directive.
"psr-4": { "App\\": "app/" }
Edit1: I have tried composer dump-autoload, issue remains
原文:https://stackoverflow.com/questions/48095680
最满意答案
如果它对我这样优化的人有用:
$newPosts = DB::table('posts') ->where('created_at', '>=', Carbon::now()->subHours(24)->toDateTimeString()) ->groupBy('category') ->select('category', DB::raw('COUNT(category) as number')) ->pluck('number', 'category');
结果如下所示:
[ 'Interviews' => 5, 'Lifestyle' => 7, 'About me' => 1, ]
并在视图中
<nav> <div class="nav-wrapper"> <ul> <li><a href="/"><i class="fa fa-home"></i> Home</a></li> <li> <a href="/interviews"> Interviews @if(isset($newPosts['Interviews'])) <span class="badge">{{ $newPosts['Interviews'] }}</span> @endif </a> </li> <li> <a href="/lifestyle"> Lifestyle @if(isset($newPosts['Lifestyle'])) <span class="badge">{{ $newPosts['Lifestyle'] }}</span> @endif </a> </li> <li> <a href="/about-me"> About me @if(isset($newPosts['About me'])) <span class="badge">{{ $newPosts['About me'] }}</span> @endif </a> </li> </ul> </div> </nav>
这样只有一个查询被使用,你可以显示新帖子的数量。
感谢willvincent ar Laracasts。
If it's useful for someone I optimized it like this:
$newPosts = DB::table('posts') ->where('created_at', '>=', Carbon::now()->subHours(24)->toDateTimeString()) ->groupBy('category') ->select('category', DB::raw('COUNT(category) as number')) ->pluck('number', 'category');
The result is something like:
[ 'Interviews' => 5, 'Lifestyle' => 7, 'About me' => 1, ]
And in the view
<nav> <div class="nav-wrapper"> <ul> <li><a href="/"><i class="fa fa-home"></i> Home</a></li> <li> <a href="/interviews"> Interviews @if(isset($newPosts['Interviews'])) <span class="badge">{{ $newPosts['Interviews'] }}</span> @endif </a> </li> <li> <a href="/lifestyle"> Lifestyle @if(isset($newPosts['Lifestyle'])) <span class="badge">{{ $newPosts['Lifestyle'] }}</span> @endif </a> </li> <li> <a href="/about-me"> About me @if(isset($newPosts['About me'])) <span class="badge">{{ $newPosts['About me'] }}</span> @endif </a> </li> </ul> </div> </nav>
That way only one query is used and you can show the number of the new posts.
Thanks to willvincent ar Laracasts.
相关问答
更多-
我已就WPSE的这个主题做了非常详细的解释,并且为了SO用户可能拥有的价值和利益,这里是从WPSE上的那个问题复制的完整帖子。 为了感兴趣,这里是WPSE上完整帖子的链接: 有些疑问主要查询和自定义查询如何在此自定义主题中工作? 您的实际问题基本上是何时运行自定义查询以及何时使用主查询。 让我们把它分成三部分 第一部分 何时运行自定义查询(这不是确定的列表) 创建自定义内容滑块 在页面中创建精选内容区域 在page.php模板上,如果您需要显示帖子 如果您需要静态首页上的自定义内容 显示相关,热门或信息性帖 ...
-
在菜单上标记新帖子(Mark on menu if new posts)[2023-09-30]
如果它对我这样优化的人有用: $newPosts = DB::table('posts') ->where('created_at', '>=', Carbon::now()->subHours(24)->toDateTimeString()) ->groupBy('category') ->select('category', DB::raw('COUNT(category) as number')) ... -
Wordpress - Header菜单中的最新帖子代码打破了后期数据(Wordpress - Recent Posts code in Header Menu breaking Post Data)[2021-12-24]
在while循环之后使用wp_reset_postdata 。 Use wp_reset_postdata after your while loop. -
所以我设法解决了这个问题,将WP类别slug作为一个类添加到我的菜单项中,并在每个类别概述页面上的每个类别中添加一个容器div。 我使用AJAX根据单击的菜单项将类别容器div加载到我的filter-content div中。 $('.menu-item li').click( function () { //Category slug is the 5th class on my menu item ...
-
这里包括http://9-sec.com/2012/10/how-to-exclude-pages-from-wp_nav_menu-2/ 另一种选择是使用自定义walker函数。 This is covered here http://9-sec.com/2012/10/how-to-exclude-pages-from-wp_nav_menu-2/ The other option is to use a custom walker function.
-
您可以使用名为“ Options Pages ACF的高级选项。 这是链接 You can use for that premium option for ACF called Options Pages. Here is the link
-
试试这个插件,我认为这就是你需要的。 Try this plugin, I think it's what you need.
-
页面适合你正在做的事情。 为每个顶级导航项创建一个页面。 接下来,为每个子级别创建子页面(即在管理控制台中设置其父页面)。 您将得到一个如下所示的页面结构: Photography Fashion Gallery 1 Gallery 2 Etc. Still Life Gallery 1 Gallery 2 Etc. Art & Illustration Gallery 1 Gal ...
-
@Taz Harish已经制作了类似的Ajax菜单,你可以从这里按照他的教程: 我可以直接包含该代码到这个答案,但我不是实际代码的作者,所以你必须遵循教程。 @Taz Harish has made an Ajax Menu like that, you can follow his tutorial from here: i could include that code straight forward to this answer but i'm not the author of actual co ...