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..

2
  • Can you add also getDocName function? Commented Oct 17, 2016 at 4:33
  • I edited and add getDocName function. Thank you Commented Oct 17, 2016 at 4:35

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

namespace App; use App\Languages; use App\Documents; class DataRepository{ public function getData(){ return Documents::all(); } public function getAllLang(){ return Languages::all(); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.