OunknownO
OunknownO

Reputation: 1196

How to use methods from one controller in another

I have installed waavi package for manipulation of translation files. I need to use methods from it's controller to mine? I tried something like this but it doesn't work

LanguageRepository::findByLocale(1);

This is what I am using in beginning of my controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Waavi\Translation\Repositories\LanguageRepository;

use Waavi\Translation\Repositories\TranslationRepository;

use Illuminate\Foundation\Application;

Upvotes: 1

Views: 68

Answers (1)

Ravisha Hesh
Ravisha Hesh

Reputation: 1504

If you have successfully done all the steps in here, you should be able to access to LanguageRepository using depedency injection(" It is recommended that you instantiate this class through Dependency Injection")

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;

class DefaultController extends Controller
{
    private $language_repository;

    function __construct(LanguageRepository $language_repository)
    {
        $this->language_repository = $language_repository;
    }

    public function index()
    {
        dd($this->language_repository->findByLocale("en"));
    }
}

Note: you need pass language string instead of id to findByLocale method. see line 97

Upvotes: 1

Related Questions