Hello!

Began to study laravel5, and here such a question arose.

I have a controller, an API that responds to ajax requests. But I can not figure out how to connect the desired model, depending on the request.

// Код контроллера namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Message; class ApiController extends Controller { public function index($action){ $messages = Message::count(); $data = array( 'status' => 'success', 'action' => 'action' ); return json_encode($data); } } 

Here I have one model connected

 use App\Models\Message; 

I can use it, everything is wonderful. But, I will have to take records from different models, how to connect them depending on $ action-a? I do not want to immediately connect 10 models through use

 use App\Models\Message; use App\Models\Users; use App\Models\Servers; use App\Models\Caegory; 

I just don’t know how to do it right, so I’m asking, maybe I’m not doing it right .. Please do not throw slippers :)

  • one
    If the models are interconnected, you can use the with method. If not, switch on the action to go through - Alex_01

1 answer 1

I think that the problem is that you have one action that gives different models! this is not based on the RestAPI principle, remember, ideally, each route gives its essence. Well, if you still give different models in one action, then there is nothing to worry about adding them to use.