【发布时间】: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