小编Mar*_*łek的帖子

不存在的模型类的雄辩关系

我想在我的应用程序中有许多模型/模块,但有些客户端会删除它们中的一些.

现在我有这样的关系:

public function people()
{
    return $this->hasMany('People', 'model_id');
}
Run Code Online (Sandbox Code Playgroud)

当我运行$model = Model::with('people')->get();它工作正常

但如果People模型不存在怎么办?

目前我得到了:

ClassLoader.php第386行中的1/1 ErrorException:include(...):无法打开流:没有这样的文件或目录

我试过了

public function people()
{ 
    try {
       return $this->hasMany('People', 'model_id');
    }
    catch (FatalErrorException $e) {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者:

public function people()
{ 
    return null; // here I could add checking if there is a Model class and if not return null
}
Run Code Online (Sandbox Code Playgroud)

但是使用这种方法时$model = Model::with('people')->get();不起作用.

我将有几十个关系,我不能列出它们用于with.最好的方法是使用一些empty relation(返回null)只是为了使Eloquent不做任何事情,但在这种情况下,Eloquent仍然试图让它工作,我会得到:

哎呀,看起来像出事了.Builder.php第430行中的1/1 FatalErrorException:在null上调用成员函数addEagerConstraints()

那有什么简单的解决方案吗?

laravel eloquent laravel-5

2
推荐指数
1
解决办法
2886
查看次数

我如何整合和初始化laravel 5的文件系统?

我按照这个步骤https://github.com/GrahamCampbell/Laravel-Flysystem

作曲家更新"graham-campbell/flysystem":"~1.0"DONE

php artisan供应商:发布DONE

我有这个代码

public function store()
    {
      $file = Request::file('images');
      $extension = $file->getClientOriginalExtension();
        Storage::disk('local')->put($file->getFilename().'.'.$extension,                     File::get($file));
      $entry = new Fileentry();
      $entry->mime = $file->getClientMimeType();
      $entry->original_filename = $file->getClientOriginalName();
      $entry->filename = $file->getFilename().'.'.$extension;
   }
Run Code Online (Sandbox Code Playgroud)

我的表格

@extends('app')

@section('content')

<h1>Create New Timelinemeta</h1>
<form method="POST" action="{{ url('timelinemeta/store') }}" enctype="multipart/form-data">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <div class="form-group">
    <label for="">Images</label>
    <input type="file" class="form-control" name="images">
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>
@endsection
Run Code Online (Sandbox Code Playgroud)

有错误:

Class 'App\Http\Controllers\Storage' not found
Run Code Online (Sandbox Code Playgroud)

php filesystems namespaces laravel laravel-5

2
推荐指数
1
解决办法
1万
查看次数

插入集合以保存许多 Eloquent

我有一个充满模型的集合,我如何将它们放入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)

我将如何解决这个问题?

php laravel eloquent laravel-5

2
推荐指数
1
解决办法
5456
查看次数

Laravel 5在商店后重定向

如何在L5存储过程后重定向路由?

我的路线是

Route::get('/pages/aracislemler/{id}', ['middleware' => ['roles'], 'uses' => 'PagesController@aracislemler', 'roles' => ['Admin']]);
Run Code Online (Sandbox Code Playgroud)

和控制器功能

public function store()
{

        $brand_name = Request::get('brand_id');
        $type_id = Request::get('type_id');
        //$client_id = Request::get('representive_client_id');

        if (!Brand::find($brand_name)) {
            $brand = new Brand;
            $brand -> brand_name = $brand_name;
            $brand -> type_id = $type_id;
            $brand -> save();

            $last_brand_id = $brand -> id;

            $input = Request::except('brand_id');
            Vehicle::create($input);

            $vehicle = Vehicle::orderBy('created_at', 'desc')->first();
            $vehicle -> update(array('brand_id' => $last_brand_id));

        }else{         
            $input = Request::all();
            Vehicle::create($input);
        }

        return  redirect('pages/aracislemler');
}
Run Code Online (Sandbox Code Playgroud)

php redirect laravel laravel-routing laravel-5

2
推荐指数
1
解决办法
7730
查看次数

我怎样才能只运行一次 Laravel Queue?

我使用 laravel 5.1 Queue 作为我的异步后台功能。但我只想运行该功能 1 次。如果该功能失败,我想做其他过程​​。我如何检测我的工作是否失败?我做得对还是错?我应该改变什么?ps:我是初学者。我在我的控制器中这样使用。

$job = new myjob($var1, $var2);
$this->dispatch($job);
$job->release();
if ($job->attempts() >= 1)
{
    $job->delete();
    //will do other process
}
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-5 laravel-5.1 laravel-queue

2
推荐指数
1
解决办法
3042
查看次数

使用 Laravel 5.1 授权方法时如何返回自定义 403 异常

在 laravel 5.1 中,如果您使用以下方法,您可以在检查能力时返回自定义响应:

if (Gate::denies('update', $post)) {
        return response()->view('errors.403');
}
Run Code Online (Sandbox Code Playgroud)

有没有什么办法在使用authorize方法时返回类似的自定义错误:

$this->authorize('update', $post);
Run Code Online (Sandbox Code Playgroud)

上面只是抛出一个状态码为 403 的 http 异常。

acl laravel laravel-5 laravel-5.1 laravel-authorization

2
推荐指数
1
解决办法
6788
查看次数

Route :: auth()和命名空间

我已经习惯make:auth创建登录脚手架,它在基本应用程序中运行良好.但是我正在创建一个包,所以我已经将文件移动到我的包中各自的位置.

我已将make:auth应用程序创建的路由命名为

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/home', 'Package\Namespace\HomeController@index');
});
Run Code Online (Sandbox Code Playgroud)

当我评论Route::auth();一切似乎工作正常.当我继续Route::auth我得到一个错误

Class Auth\AuthController does not exist
Run Code Online (Sandbox Code Playgroud)

我无法理解问题所在.我对auth()助手功能了解不多.

laravel laravel-5 laravel-5.2

2
推荐指数
1
解决办法
4993
查看次数

Laravel 在 Blade 中获取文件扩展名

当我想在用户上传了 docx 文件时显示 docx 图标时,我收到以下错误:

Call to undefined method Illuminate\Database\Query\Builder::getClientOriginalExtension() (View: /home/vagrant/Code/support/local/resources/views/users/ticket.blade.php)
Run Code Online (Sandbox Code Playgroud)

我正在尝试这样:

@foreach($ticket->image as $photo)
     @if($photo->getClientOriginalExtension() == 'docx') 
           <img src="icons/word.png">
      @else
<a href="{{ $photo->path }}"><img src="{{ $photo->path }}" alt=""/></a>
       @endif
 @endforeach
Run Code Online (Sandbox Code Playgroud)

显然这getClientOriginalExtension()是不对的。但我应该用什么来代替呢?

php laravel laravel-blade

2
推荐指数
1
解决办法
1万
查看次数

Laravel 5.0 file_put_contents 错误

刚刚将 laravel 项目移至托管,它说:

Production.ERROR:异常“ErrorException”,消息“file_put_contents(/Users/max/sites/evocate.dev/storage/framework/sessions/7f7df88c52734c34a3f89286dc74d517d446c4fd):无法打开流:/home/f/中没有这样的文件或目录” fb7929gh/evocate2/vendor/compiled.php:6440

为什么它需要我的本地主机路径以及在哪里可以解决这个问题?

php laravel laravel-5

2
推荐指数
1
解决办法
1万
查看次数

响应头名称“Access-Control-Allow-Origin”包含无效字符,中止请求

我有一个 Laravel 应用程序(部署在 heroku 上),我在其中收到了500 Internal Server error.

当我检查我的日志时,我在标题中看到错误方法:

响应头名称“Access-Control-Allow-Origin”包含无效字符,中止请求

我有cors middleware以下代码:

public function handle($request, Closure $next)
{
    header('Access-Control-Allow-Origin : *');
    header('Access-Control-Allow-Headers : Content-type, X-Auth-Token, Authorization, Origin');
    return $next($request);
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

heroku laravel laravel-5 laravel-middleware laravel-5.7

2
推荐指数
1
解决办法
1688
查看次数