0

I'm creating a website by using Laravel 5.3. There is a problems about database connection. I have 2 independent tables called languages and documents when I retrieve data from these tables the problems happen. Here is my code in language controller

use App\Languages;
use Illuminate\Support\Facades\DB;
class LanguageController extends Controller {
    public function getLanName(){
        $lans = Languages::all();
        return view('index',compact('lans'));
    }
}

and here is the code of language model

namespace App;
use Illuminate\Database\Eloquent\Model;
class Languages extends Model{
    protected  $table = 'languages';
    public $timestamps = true;
    protected  $primaryKey = 'lan_id';
}

the Document controller and model are same as language

namespace App\Http\Controllers;
use App\Documents;
use Illuminate\Support\Facades\DB;
class DocumentController extends Controller{
    public function getDocName(){
        $docs = Documents::all();
            return view('index',compact('docs'));

    }
}

and this is my route and some of html in the view:

Route::get('/',['uses' => 'DocumentController@getDocName']);
Route::get('/',['uses' => 'LanguageController@getLanName']);

        <select >
                <option  >Select Document</option>
                @foreach($docs as $doc)
                    <option value="{{$doc->docName}}">{{$doc->docName}}    </option>
                @endforeach
            </select>  
            <select>
                <option >Select Language</option>
                @foreach($lans as $lan)
                    <option value="{{$lan->language}}">{{$lan->language}}</option>
                @endforeach
        </select>

I can only retrieve either language or document. If I comment language or document, the other one works well.Can't I use two controller in same route?

Many Thanks to help..

Rick Z
  • 41
  • 1
  • 8

1 Answers1

4

No, this isnt possible. You can not bind two separate controllers to a single Route. One could even argue that it is very bad practice. You could create something like an OverviewController for this single page, and just call the 2 models in your controller method.

You could also use a Repository, answered here: https://stackoverflow.com/a/26092119

Community
  • 1
  • 1
Jan Willem
  • 1,280
  • 1
  • 9
  • 9
  • namespace App; use App\Languages; use App\Documents; class DataRepository{ public function getData(){ return Documents::all(); } public function getAllLang(){ return Languages::all(); } } – Rick Z Oct 17 '16 at 09:18