【问题标题】:send to repository class variable laravel from controller从控制器发送到存储库类变量 laravel
【发布时间】:2020-12-29 08:19:35
【问题描述】:

我正在准备对我的存储库进行查询,以获取拥有餐厅的画廊,并且我需要搜索 id。一间餐厅的画廊只能有 10 张图片,但我们可以拥有不止一间餐厅。我需要当一家餐厅有 10 张图片时,它不能再上传并禁用这家餐厅。

对不起我的英语,我希望我能正确地解释我。

我的控制器:

if (auth()->user()->hasRole('admin')){
            $restaurant = $this->restaurantRepository->pluck('name', 'id');
        }else{
            $restaurant = $this->restaurantRepository->galleries()->myActiveRestaurants()->pluck('name', 'id');
        }
        // get all media gallery of restaurant
        //$media = $this->galleryRepository->getCountMedia();
        foreach($restaurant as $res){
            $res->cg = (count($res->galleries()->get()) == 10);
        }

        $hasCustomField = in_array($this->galleryRepository->model(), setting('custom_field_models', []));
        if ($hasCustomField) {
            $customFields = $this->customFieldRepository->findByField('custom_field_model', $this->galleryRepository->model());
            $html = generateCustomField($customFields);
        }
        return view('galleries.create')->with("customFields", isset($html) ? $html : false)->with("restaurant", $restaurant)/*->with('imagesRestaurant', $media)*/;

我在这里调用模型函数 gallery(),我有一个函数要获取,restaurant_id

但在我的网络中,结果是:

Call to a member function galleries() on string

我需要得到restaurant_id 在这里打电话my repository:

public function getCountMedia(){
        return Gallery::join("user_restaurants", "user_restaurants.restaurant_id", "=", "galleries.restaurant_id")
                        ->where('user_restaurants.user_id', auth()->id())->count();
    }

我不想要 auth()->id() 我需要 restaurant_id 以便在我的控制器中提取该餐厅以获得 10 张图像

希望有人能帮帮我

更新

我在构造中加入类:

public function __construct(GalleryRepository $galleryRepo, CustomFieldRepository $customFieldRepo, UploadRepository $uploadRepo
        , RestaurantRepository $restaurantRepo)
    {
        parent::__construct();
        $this->galleryRepository = $galleryRepo;
        $this->customFieldRepository = $customFieldRepo;
        $this->uploadRepository = $uploadRepo;
        $this->restaurantRepository = $restaurantRepo;
    }

我现在可以显示,restaurant_id 它在画廊表中,但我不知道如何访问此属性

更新2

我更改了我的控制器和存储库:

控制器

// get all media gallery of restaurant
foreach($restaurant as $res => $id){
   $media[] = $this->galleryRepository->getCountMedia($res);
}
return view('galleries.create')->with("customFields", isset($html) ? $html : false)->with("restaurant", $restaurant)->with('imagesRestaurant', $media);

存储库

public function getCountMedia($restaurantId){
        return Gallery::join("user_restaurants", "user_restaurants.restaurant_id", "=", "galleries.restaurant_id")
                        ->where('galleries.restaurant_id', $restaurantId)->count();
    }

有了这个我得到餐厅的 id,在刀片中我需要禁用值大于 10 的选项,但是,只得到重复的选择......

刀片:

@foreach($imagesRestaurant as $images)
      {{$images}}
    
      @if($images >= 10)
        {!! Form::select('restaurant_id', $restaurant, null, ['class' => 'select2 form-control']) !!}
      @else
        {!! Form::select('restaurant_id', $restaurant, null, ['class' => 'form-control', 'disabled' => true]) !!}
      @endif
    @endforeach

【问题讨论】:

  • 您的调用存储库在哪里?
  • @KamleshPaul 感谢您的回复,在这一行 $media = $this->galleryRepository->getCountMedia();但是这条线被评论了,因为我需要为餐厅做这个。为此,我有一个 for-each,但我不太了解这个
  • Call to a member function galleries() on string 这个$this->restaurantRepository->galleries() 这里restaurantRepository 你在哪里创建实例?
  • @KamleshPaul 我更新了我的问题。我不得不说“restaurant_id”它在画廊表中,但我不知道如何将此属性传递给存储库

标签: php laravel laravel-5 eloquent


【解决方案1】:

你可以做一些链接这个

public function __construct($restaurant_id)
{
    parent::__construct();
    dd($restaurant_id);
    $this->galleryRepository = new GalleryRepository(); // if you need you can pass here 
    $this->customFieldRepository = new CustomFieldRepository();
    $this->uploadRepository = new UploadRepository();
    $this->restaurantRepository = new RestaurantRepository();
}

在这里,每当您调用此类时,您都需要像新的ClassName($restaurant_id) 一样传递restaurant_id


更新

您可以创建 repo 实例并像这样传递值

// get all media gallery of restaurant
foreach ($restaurant as $res => $id) {
    $media[] = (new GalleryRepository($id))->getCountMedia($res);
}

【讨论】:

  • 感谢您的帮助。我改变了我所有的代码,我更新了我的问题,如果你能帮我解决这个问题,我会非常感激你
  • 感谢您的回复,但 GalleryRepository 没有构造,只有 getCountMedia 方法,在我的 update2 中我得到了我需要,但在刀片中我正在复制选择,但是我需要禁用它有 10 个或更多图像的选项
  • @daviserraalonso $imagesRestaurant 里面是什么?
  • 这是我从控制器传递到视图的变量,其中包含来自餐厅画廊的大量图像。我在 update2 的控制器部分更新了我的问题
  • 所以你的意思是disabled 属性不起作用?
猜你喜欢
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多