-1
Hello!
I’m starting my studies on api Rest. In my application I can only return the employee data according to the id of the company that it belongs to, however, the api is returning the data independent of the company id that I pass as parameter.
Example:
Route
api/company/company_id/employee/employee_id
If I pass the parameters:
api/company/2/employee/3
It returns me the correct data!
But if I do it:
api/company/qualquercoisa/employee/3
Keeps returning the same data.
How do I limit the route so that only pass the employee data according to the id of the company to which it belongs?
My code:
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employee;
use App\Http\Controllers\Controller;
class EmployeesController extends Controller
{
public function getEmployee(Request $request)
{
return Employee::findOrFail($request->employee_id);
}
}
Model
class Employee extends Model
{
protected $fillable = [
'name',
'position',
'email',
'phone',
'admission',
'company_id'
];
public function company(){
return $this->belongsTo('App\Company','company_id','id');
}
}
Route
Route::get('api/company/{company_id}/employee/{employee_id}','EmployeesController@getEmployee');
Thank you very much! That’s right, besides having solved, your code clarified a logic that I thought I understood, but I was thinking in the wrong way! @Luanmichel
– Lucasmoore
Show!! If you can mark the answer as accepted, then your question is answered. @Lucasmoore
– Luan Michel
yes, I just freaked out! Thanks! @Luanmichel
– Lucasmoore