API in laravel im using API to retrive data from database.
first view will contain all data.
second view will show only the selected data.
here my api code
public function show($slug){
$job = Job::findBySlugOrFail($slug);
// return $job;
return Response::json($job->toArray());
}
how to retrive this specific data from api
first view will contain all data
public function index()
{
$jobs = Job::all();
return response()->json($jobs);
}
second view will show only the selected data.
public function show($slug)
{
$job = Job::findBySlugOrFail($slug);
return response()->json($job->toArray());
}
@TYKUS - yes that what i have done.
here my code for retriving all jobs
public function index()
{
$client = new \GuzzleHttp\Client();
$request = $client->get('http://aaaa.test/api/jobs');
$response = $request->getBody();
$jobs = json_decode($response,true);
return view('careers/index', compact('jobs'));
}
MY question is abou how to show the specific data?
@TYKUS - its same model, i wand to show this job detail seperetly
And these two projects are sharing a common models package?
Does Project A expose an endpoint for retrieving a single job?
@TYKUS - - all models are in project A
i just did a show method for retrieving single job
So there is no Job model on Project B?
If you own both projects, you either (i) add the models to Project B and query your database directly, or (ii) add an endpoint to Project A to retrieve a single Job and fetch it using Guzzle on Project B s you are already for the collection, or (iii) if you don't own A, then get the entire collection as you are in the index method and filter for the particular Job record you need.
@TYKUS - Thank you for your reply. (I) I was doing that bu face problems when deploying these two projects so I cancele it. (ii) i own the two projects. I was thinking I’m doing this approach by creating an api for single job in project A but I face problem in fetching data in project B
What is the problem with making an endpoint in Project A, you need to:
Define a route /api/jobs/{slug} which will respond with the result of
$job = Job::findBySlugOrFail($slug);
In project B, you do the same thing...
A route that will accept a slug, and in the controller
make an HTTP request to Project A
returning a show view with the result.
public function show($slug)
{
$client = new \GuzzleHttp\Client();
$request = $client->get('http://aaaa.test/api/jobs/' . $slug );
$response = $request->getBody();
$job = json_decode($response,true);
return view('careers/show', compact('job'));
}
This will all feel quite slow since there are two HTTP requests needed to get the view and data.
Please sign in or create an account to participate in this conversation.