Skip to content
Advertisement

send to repository class variable laravel from controller

i´m traying to do query into my repository to get galleries that to have a restaurant and i need seach for id. One restaurant´s gallery only can to have 10 images, but we can to have more than one restaurant. I need that when one restaurant will have a 10 images, it can´t upload any more and disable this restaurant.

sorry for my english, i hope that i can explain me correctly.

my controller:

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)*/;

here i´m calling to model function galleries() where i have a function to get, restaurant_id

but in my web, result is:

Call to a member function galleries() on string

i need to get restaurant_id for call here 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();
    }

i don´t want auth()->id() i need restaurant_id for in my controller extract that restaurant to have 10 image

i hope that any can help me, please

updated

i´m instaciate class in construct:

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;
    }

i can show now, that restaurant_id it´s in galleries table, but i don´t know how to access to this property

updated2

i change my controller and repository:

controller

// 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);

repository

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

with this i get id for restaurant, and in blade i need disable option that value greater than 10 but, only get duplicate select…

blade:

@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

Advertisement

Answer

you can do something link this

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();
}

here whenever your calling this class you need to pass restaurant_id like new ClassName($restaurant_id)


Update

you can create instance of repo and pass value like this

// get all media gallery of restaurant
foreach ($restaurant as $res => $id) {
    $media[] = (new GalleryRepository($id))->getCountMedia($res);
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement