$reviews 有 10 个数组,我试图只显示前 5 个。这是我到目前为止所想到的。
@for ($i = 0; $i < 6; $i++)
@foreach ($reviews as $reviews; i++)
<p>Body</p>
@endforeach
@endfor
Run Code Online (Sandbox Code Playgroud)
知道为什么这不起作用吗?
我正在使用 laravel 5.5,我正在尝试使用到控制器的自动路由,但它不起作用
在 web.php(此版本的路由文件)中,我有以下行
Route::resource('panel', 'panel');
Route::resource('/', 'HomeController');
Run Code Online (Sandbox Code Playgroud)
在面板中我有以下操作
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class panel extends Controller{
public function index(){
return \View::make('panel.index');
}
public function registrar(){
return \View::make('panel.registrar');
}
}
Run Code Online (Sandbox Code Playgroud)
但它只是调用index()视图,当用户访问 url 时,不会调用 registrar() 视图
site.com/panel/registrar
Run Code Online (Sandbox Code Playgroud)
屏幕上打印以下错误
"Method [show] does not exist on [App\Http\Controllers\panel]."
Run Code Online (Sandbox Code Playgroud)
我尝试使用 base_controller 但它也不起作用
"Class 'App\Http\Controllers\Base_Controller' not found"
Run Code Online (Sandbox Code Playgroud)
有没有办法识别这些行为?
有没有办法将内容添加到 Laravel 5.5 项目的每个渲染视图中?我想要一个 Composer 包,例如,它将一些 JavaScript 代码添加到每个渲染的视图中,而不需要对实际项目代码进行任何修改。
现在,我已经通过在包中创建视图并将其包含到我的布局标题中来实现这一点,但如果可以在不手动包含的情况下以某种方式完成此操作,那就更好了。
我创建了中间件
php artisan make:middleware isTeacher
Run Code Online (Sandbox Code Playgroud)
在 App/Http/isTeacher.php 中我放置了检查:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class isTeacher
{
public function handle($request, Closure $next)
{
$user = Auth::user();
if($user && $user->capability == 3)
{
return $next($request);
}
else
return redirect('/login');
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我在 app/Http/Kernel.php 中定义了中间件
protected $routeMiddleware = [
...
'auth.teacher' => \App\Http\Middleware\isTeacher::class,
...
];
Run Code Online (Sandbox Code Playgroud)
问题是:如何在刀片模板中查看教师能力?我正在尝试这样做:
@if (Auth::isTeacher())
Run Code Online (Sandbox Code Playgroud)
但不起作用
任何帮助表示赞赏
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use App\Classes\DynamicMenu;
use App\Http\Controllers\WelcomeController;
use Auth;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
dd(Auth::user());
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说返回“null”,但其他控制器返回用户详细信息。如何解决这个问题?
我正在尝试从数据库中获取所有数据,其中 id 等于数组中的 id。我希望它按顺序运行,例如,如果调用数组$attributes并且看起来像这样;
array:2 [\xe2\x96\xbc\n 0 => 11\n 1 => 12\n]\nRun Code Online (Sandbox Code Playgroud)\n\n我想获取 id 等于的所有结果11并将它们分组在一起以便稍后循环。然后继续查询数据库以获取 id 等于 的所有结果12。
我可以创建一个循环并在每次迭代中运行一个查询,但如果数组有 30 多个 id,则将导致 30 个查询,这些查询本质上是相同的,只是匹配不同的 id。
\n\n我可以将数组传递给where or子句并对结果进行分块,以便我可以循环认为它稍后分组吗?
所以我有一个BaseValuation抽象类及其实现示例FooValuation和BarValuation.
我想根据使用输入来实例化Foo或实现。Bar所以我自己创建了一个简单的类,Valuation它的作用是:
<?php
namespace App\Valuation;
class Valuation
{
public $class;
public function __construct($id, $type = 'basic')
{
$class = 'App\Valuation\Models\\' . ucfirst($type) . 'Valuation';
$this->class = new $class($id);
}
public function make()
{
return $this->class;
}
}
Run Code Online (Sandbox Code Playgroud)
使用这个我可以简单地做到这一点(new App\Valuation\Valuation($id, $type))->make(),我将根据用户的要求获得所需的实现。
但我知道 laravel 的容器很强大,必须允许我以某种方式做到这一点,但我不明白这是如何完成的。有人有什么想法吗?
我在我的项目中使用 laravel 5.1,当我使用 时Mail::later(3600, ...);,它会立即发送电子邮件,这应该是在 1 小时后。为什么1小时后还没有发送?
队列驱动程序:同步
我通过以下方式将 vue js 的请求保存到数据库:
public function store(Request $request)
{
//validate
$this->validate($request, [
'name' => 'required',
'description' => 'required',
'price' => 'required'
]);
//get image
$exploded = explode(',', $request->cover_image);
$decoded = base64_decode($exploded[1]);
if(str_contains($exploded[0],'jpeg'))
$extension = 'jpg';
else
$extension = 'png';
$fileName = str_random().'.'.$extension;
$path = public_path().'/cover_images/'.$fileName;
file_put_contents($path, $decoded);
//save
$product = new Product;
$product->name = $request->input('name');
$product->description = $request->input('description');
$product->price = $request->input('price');
$product->cover_image = $fileName;
if($product->save()) {
return new ProductsResource($product);
}
}
Run Code Online (Sandbox Code Playgroud)
如何验证 Base64 图像?我保存图像的过程是否vue js正确,或者有更好的方法吗?请告诉我。谢谢,我刚刚接触 laravel 和 vue …