我正在使用laravel FindOrNew()来获取带有两个参数的条目,或者创建一个新参数:
$option = \App\Option::findOrNew(['user_id' => $this->id , 'option_name' => $optionName]);
Run Code Online (Sandbox Code Playgroud)
我想为$ optionName中具有名称的用户获取一个选项.问题是它只是检查user_id,并且当option_name不存在时不会创建新的..而是"找到"与$ optionName值不匹配的那个.
谁能说出我做错了什么?我怎样才能做到这一点?
我正在 Laravel 中配置维护模式。我正在尝试添加 IP 白名单。
当我运行此代码时:
<?php
namespace App\Http\Middleware;
use Closure;
class CheckForMaintenanceMode
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->app->isDownForMaintenance() &&
!in_array($request->getClientIP(), ['127.0.0.1']))
{
return response('Be right back!', 503);
}
return $next($request);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
未定义的属性:App\Http\Middleware\CheckForMaintenanceMode::$app
有人可以告诉我有什么问题吗?
我在Laravel模型上有一个雄辩的关系,它是动态的 - 也就是说,特定数据库字段的值决定了哪个模型将被加载.当我第一次实例化模型实例然后引用关系时,我能够很好地加载这种关系,但是当我急切地加载这种关系时它不起作用.
具体来说,我有一个Product模型.该产品可能是也可能不是其他产品的父母.如果parent_id产品的设置被设置为0那么该产品被视为父产品(无论是否有孩子).如果将parent_id其设置为其他产品的ID,则该产品是子产品.我需要能够访问Product::with('parent')和知道的parent关系将返回要么本身(是的,重复的数据),或者不同的产品,如果它是一个孩子.
这是我到目前为止的关系:
public function parent()
{
if ($this->parent_id > 0) {
return $this->belongsTo('App\Product', 'parent_id', 'id');
} else {
return $this->belongsTo('App\Product', 'id', 'id');
}
}
Run Code Online (Sandbox Code Playgroud)
当我急切加载时,$this->parent_id总是未定义,因此即使它实际上是父产品,这种关系也只会自行返回.
在关系被急切加载之前,有没有办法访问模型的属性?在我返回关系之前,我考虑过在单独的查询中工作,但我意识到我甚至无法访问产品的id来运行该查询.
如果不可能,还有哪些其他方法可以解决这类问题?这似乎不能通过传统的多态关系来解决.我只有两个可能的想法:
belongsTo在我动态确定外键的关系中添加某种约束.老实说,我不知道如何实现其中任何一个.我是以正确的方式来做这件事的吗?有什么我可以忽略的吗?
在考虑了这个之后,我认为提出问题的最简单方法是:有没有办法在运行时为关系本身内部的关系动态选择外键?当我调用关系时,我的用例不允许我使用急切的加载约束 - 约束需要应用于关系本身.
我正在努力为Lumen + Dingo Rest API找到一个基本的工作基础,但我无法弄清楚如何将和平放在一起.
Lumen工作正常,但是当我尝试添加Dingo时,我会遇到各种各样的错误.从Dingo文档我读到:
获得程序包后,您可以在config/api.php文件或服务提供程序或引导程序文件中配置提供程序.
'jwt' => 'Dingo\Api\Auth\Provider\JWT'
Run Code Online (Sandbox Code Playgroud)
要么
app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
});
Run Code Online (Sandbox Code Playgroud)
我已经安装了一个新的Lumen副本,我没有看到config/api.php,所以我假设我使用这段代码放在我的bootstrap/app.php
这就是我的bootstrap/app.php样子:
<?php
require_once __DIR__.'/../vendor/autoload.php';
try {
(new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
});
$app->group(['namespace' => 'App\Api\Controllers'], function ($app) {
require __DIR__.'/../app/Api/routes.php';
});
return $app;
Run Code Online (Sandbox Code Playgroud)
运行时,我收到以下错误:
BindingResolutionException …Run Code Online (Sandbox Code Playgroud) 我在 Laravel eloquent 中的查询有问题,运行查询时没有得到结果,并且生成的查询似乎不是我所期望的。
这是控制器中的代码:
$lastUpdate = Input::get('last_update');
$userId = Auth::user()->id;
$eventIds = EventVendor::where('user_id', $userId)
->where('is_active', 1)->get()->lists('event_id');
$events = EventDetails::whereIn('id', $eventIds)
->where(function($query) use ($lastUpdate) {
$query->where('created_at', $lastUpdate);
$query->orWhere('updated_at', $lastUpdate);
})
->where('is_active', 1)
->with("details_sub")
->with("extras")
->with("chargesDiscounts")
->toSql();
Run Code Online (Sandbox Code Playgroud)
这是生成的查询:
select * from `mtgh_event_details`
where `mtgh_event_details`.`deleted_at` is null
and 0 = 1
and (`created_at` = ? or `updated_at` = ?)
and `is_active` = ?
Run Code Online (Sandbox Code Playgroud)
除了0 = 1不应该出现在那里的之外,我也没有看到完整的查询。
我能够使Laravel Valet版本1.0.12正常工作,但是当我今天早上更新到1.1.12时,我现在收到502 - Bad Gateway错误.我没有运行任何其他服务器实例,我知道.
此外,我已经尝试再次运行"valet安装",并重新启动,但我仍然得到相同的错误.
我不确定我是否遗漏了什么,或者是什么原因造成的.有任何想法吗?如果您需要我提供有关任何可能有用的详细信息,请与我们联系.
提前致谢!
我正在使用 Laravel 实现一个简单的注册表单。失败时,它应该返回一个 json 响应:
{"success":"false", "message":"Could not log in newly registered user"}`
Run Code Online (Sandbox Code Playgroud)
这按预期工作。如果注册和登录成功,我想返回:
{"success":"true", "message":$html}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,$html将使用视图创建。这其中的重要部分如下:
if(Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password'))))
{
$html = View::make('welcome_new_user', array('first_name' => Input::get('first_name')));
return Response::json(array('success' => 'true', 'message' => $html));
}
else
{
return Response::json(array('success' => 'false', 'message' => 'Could not log in newly created user.'));
}
Run Code Online (Sandbox Code Playgroud)
失败时,我得到预期的响应。成功后,我得到"success":"true",但是一个空的message. 我$html在返回之前立即回显了,它包含预期的 html。为什么返回的消息是空的?
我正在尝试通过我的路由将变量传递给我的控制器,但我有多个路由(类别)通向同一个控制器,即
Route::get('/category1/{region}/{suburb?}', 'SearchController@search');
Route::get('/category2/{region}/{suburb?}', 'SearchController@search');
Run Code Online (Sandbox Code Playgroud)
使 /category1、2 等成为参数 /{category} 不是一个选项,我不想为每个类别制作单独的控制器功能。
如何将 url 的第一段发送到我的搜索控制器?即类别1或类别2?
目前控制器如下:
public function search($region, $suburb = null) { }
Run Code Online (Sandbox Code Playgroud)
谢谢!
我试图在 Lumen 上配置日志轮换,但我被卡住了。我知道如何用 Laravel 做到这一点,但是(再一次)我对 Lumen 迷失了方向。
我尝试将其添加到 中app/bootstrap.php,因为我在某处阅读了它:
$app->configureMonologUsing(function(Monolog\Logger $monolog) use ($app) {
$monolog->pushHandler(
new \Monolog\Handler\RotatingFileHandler($app->storagePath().'/logs/lumen.log', 5)
);
});
Run Code Online (Sandbox Code Playgroud)
我可以查看此错误:
Fatal error: Uncaught Error: Call to a member function error() on null in /x/vendor/laravel/lumen-framework/src/Exceptions/Handler.php:36
Stack trace:
#0 /x/app/Exceptions/Handler.php(36): Laravel\Lumen\Exceptions\Handler->report(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#1 /x/vendor/laravel/lumen-framework/src/Concerns/RegistersExceptionHandlers.php(123): App\Exceptions\Handler->report(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#2 /x/vendor/laravel/lumen-framework/src/Concerns/RegistersExceptionHandlers.php(51): Laravel\Lumen\Application->handleUncaughtException(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#3 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(Object(Error))
#4 {main}
thrown in /x/vendor/laravel/lumen-framework/src/Exceptions/Handler.php on line 36
Run Code Online (Sandbox Code Playgroud)
我不明白这是什么意思,我应该做什么。
有没有人在lumen 5.2中成功旋转登录?
我有3个型号:车辆,经销商和省
雄辩的关系如下:
车辆属于经销商
经销商有很多车辆
省有很多经销商
经销商属于省
省有很多车辆
我想让所有车辆都在特定的省份,但是我的代码需要从车辆模型开始调用,因为我正在为车辆调用许多过滤器.
我的代码:
$p = 'Alberta';
$d = Vehicle::whereHas('province', function($q) use ($p) {
$q->where('province_name', $p);
return $q;
})->get();
dd($d);
Run Code Online (Sandbox Code Playgroud)
不幸得到错误
未找到列:1054'where子句'中的未知列'dealers.province_id'(SQL:select*from
vehicleswhere exists(select*fromprovinceswheredealers.province_id=provinces.idandprovince_name= Alberta))
但是$$_id列确实存在于经销商表中.我怎样才能使它工作?