我有一个充满模型的集合,我如何将它们放入saveMany关系的方法中?我知道它必须是一个数组,但是当我这样做时,$collection->toArray()它们就不再是 Eloquent 模型了。
public function uploadAndGetModels($messageId, array $attachments)
{
$models = new Collection();
$attachments = new Collection($attachments);
$attachments->each(function (UploadedFile $attachment) use ($messageId, $models) {
$identifier = $this->uploader->makeIdentifier($attachment->getClientOriginalExtension());
$this->uploader->uploadImage($attachment, "attachments/{$identifier}");
$model = new Attachment(array('message_id' => $messageId, 'path' => $identifier));
$models->push($model);
});
return $models;
}
Run Code Online (Sandbox Code Playgroud)
结果$models是一个Collection完整的Attachment模型。
if ($request->hasFile('attachments')) {
$attachments = $attachmentRepository->uploadAndGetModels($message->id, $request->file('attachments'));
$message->attachments()->saveMany($attachments);
}
Run Code Online (Sandbox Code Playgroud)
我将如何解决这个问题?
我正在尝试在我的 Docker 映像中安装语言环境文件,但由于某种原因它没有正确安装。
我的Dockerfiledo configure + install the locale files 中的这些行:
# Install and configure locales
RUN ["apt-get", "install", "-y", "locales"]
RUN ["locale-gen", "nl_NL.UTF-8"]
RUN ["dpkg-reconfigure", "locales"]
RUN ["update-locale"]
ENV LANG nl_NL.UTF-8
Run Code Online (Sandbox Code Playgroud)
图像创建成功。当我运行时,docker exec **ID** locale -a我仍然收到以下错误:
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_COLLATE to default locale: No such file or directory
C
C.UTF-8
POSIX …Run Code Online (Sandbox Code Playgroud) 我正在尝试管理我的文件上传,但似乎我无法写入该文件夹public/uploads.我有写权限,所以我不确定它是什么,有人看到错字吗?
我使用干预/图像库.
我的代码是:
$file = Input::file($fileName);
if ($file->isValid()) {
if ($file->getMimeType() == 'image/jpeg' || $file->getMimeType() == 'image/png') {
$path = public_path("uploads", $file->getClientOriginalName());
Image::make($file->getRealPath())->resize(180, null)->save($path);
} else {
throw new FileException;
}
}
Run Code Online (Sandbox Code Playgroud)
抛出的异常是:
Intervention \ Image \ Exception \ NotWritableException
Can't write image data to path (/home/vagrant/Sites/cms/public/uploads)
我有两个叫做的模型Page,User并且我建立了如下的Eloquent关系:
public function user() {
return $this->belongsTo('User')
}
Run Code Online (Sandbox Code Playgroud)
当我dd(Page::with('user')->get())得到正确的结果.但是现在我想对这个结果进行搜索过滤.
我已经使用了scopeSearch但我不确定如何搜索结果集.
目前我有这样的范围:
public function scopeSearch($query, $search) {
$query->where('username', 'LIKE', '%'.$search.'%')
->orWhere('name', 'LIKE', '%'.$search.'%')
}
Run Code Online (Sandbox Code Playgroud)
所以当我Page::with('user')->search('Test')->get()不工作的时候.问题(可能)是username列是User表的name一部分,而且是表的一部分Page.
我如何能够使用范围或熟悉的东西来搜索结果集,而不是在大多数查询中重复它?
我通过这样做缓存了我的Laravel 5路线php artisan route:cache.这已经成功了很长一段时间,当我改变路线时,可以再次缓存它们,它会像它应该的那样工作.
问题是我已经将一些路由移动到另一个路由组,似乎它们由于某种原因不会被缓存.
我试图再次缓存它们但它没有用.我也试过php artisan cache:clear但仍然没有工作.
Routes.php更改:
Route::group(['prefix' => 'api', 'middleware' => 'auth'], function () {
Route::get('invites', 'InvitationController@get');
Route::get('invites/check', 'InvitationController@check');
});
Run Code Online (Sandbox Code Playgroud)
变成:
Route::group(['prefix' => 'api'], function () {
Route::post('auth', 'Auth\AuthController@authenticate');
Route::get('invites', 'InvitationController@get');
Route::get('invites/check', 'InvitationController@check');
});
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我已经将这些invitation路由移动到没有Authenticate中间件的Route组.当我再次缓存路由时,Auth即使它们被移出组,它仍然会执行中间件.
通过在执行Promise之间添加延迟,我遇到了麻烦.我希望它们以特定的顺序执行.我试过Promise.mapSeries没有成功.
我的工作代码目前看起来像:
await Promise.delay(1000);
await accept(user.id, admin.id);
await Promise.delay(1000);
await accept(user.id, employee.id);
await Promise.delay(1000);
await reject(user.id, employee.id);
Run Code Online (Sandbox Code Playgroud)
我试过这个Promise.mapSeries没有成功:
const promises = [
accept(user.id, admin.id),
accept(user.id, employee.id),
reject(user.id, employee.id)
];
await Promise.mapSeries(promises, () => Promise.delay(1000));
Run Code Online (Sandbox Code Playgroud) laravel ×4
php ×4
eloquent ×2
laravel-4 ×2
laravel-5 ×2
bluebird ×1
docker ×1
dockerfile ×1
ecmascript-6 ×1
javascript ×1
locale ×1
node.js ×1
ubuntu ×1
ubuntu-14.04 ×1