I have this route
Route::get('/getLocation/{postalCode}', function () {
$response = Http::get('https://theapi');
return $response->json();
});
Sadly this doesn't work
I can reach the api with
https://theapi?PostalCode=xxxx
How can i replicate this in my route to work?
You got double }} there, try delete it so it will be like this:
Route::get('/getLocation/{postalCode}', function () {
$response = Http::get('https://theapi');
return $response->json();
});
Edit:
Add the route parameter to API function parameters
Route::get('/getLocation/{postalCode}', function ($postalCode) {
// do whatever you want to postalCode variable
$response = Http::get('https://theapi', [
'PostalCode' => $postalCode
]);
return $response->json();
});
Related
Update:
This line of code in the frontend was the culprit:
<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">
I had to change it to:
<inertia-link v-if="options.edit" :href="'/admin/gallery/1/edit'">
to make it comply with the laravel resource format for edit, provided by #Babak.
Original Post:
How would I transform this route in web.php:
Route::get('/admin/gallery/edit/{id}', function ($id) {
$data = Gallery::find($id);
return inertia('backend/cms-gallery-edit', ['data' => $data]);
});
to a resource route with its resource controller function:
Route::resource('/admin/gallery', GalleryController::class);
GalleryController.php:
public function edit($id)
{
$data = Gallery::find($id);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Edit:
I've tried both approaches of #Babak's answer, which work for index and create routes but the edit route still throws a 404. It is the only route encompassing an id.
web.php:
Route::resource('/admin/gallery', GalleryController::class)->only('index', 'create', 'edit');
GalleryController.php:
public function edit($gallery)
{
$data = Gallery::find($gallery);
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Inertia passes the id from the frontend via href:
<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">
Browser shows:
GET http://127.0.0.1:8000/admin/gallery/edit/1 404 (Not Found)
There is a fixed structure for laravel resource route method, you can see full list here. For edit page, it will generate something like '/admin/gallery/{gallery}/edit'
You can write it like below:
In your web.php file:
Route::resource('/admin/gallery', GalleryController::class)->only('edit');
And in your controller, name of the resource must be the same as your function's parameter.
public function edit($gallery)
{
$data = Gallery::find($gallery);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Or, you can customize it using parameter method. Refer to here
Route::resource('/admin/gallery', GalleryController::class)->only('edit')->parameters([
'gallery' => 'id'
]);
And your controller
public function edit($id)
{
$data = Gallery::find($id);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Newbie to all the tech I'm using here.
Trying to query a table using Axios in Laravel with Vue.js. Here's what I got:
from my component's <script>
this.axios
.get('http://localhost:8000/api/tasks', {params: {vid: this.properties[0].vid}})
.then(response => {
this.tasks = response.data;
console.log(response)
})
Regardless of what vid is in the params, the response contains ALL of the table's rows.
I do know that this may have something to do with api.php, and the Controller associated with the request. I'll post those to be verbose.
routes/api.php
Route::middleware('api')->group(function () {
Route::resource('vehicles', VehicleController::class);
Route::resource('tasks', TaskController::class);
Route::get('tasks?={vid}', [TaskControler::class, 'search']);
});
Controllers/TaskController.php
class TaskController extends Controller
{
//
public function index() {
$tasks = Task::all()->toArray();
return $tasks;
}
public function store(Request $request){
$task = new Task([
'make' => $request->input('make'),
'model' => $request->input('model'),
'year' => $request->input('year'),
'mileage' => $request->input('mileage'),
'type' => $request->input('type'),
'VIN' => $request->input('VIN')
]);
$task->save();
}
public function show($tid){
$task = Task::find($tid);
return response()->json($task);
}
public function update($tid, Request $request){
$task = Task::find($tid);
$task->update($request->all());
return response()->json('Task Updated!');
}
public function destroy($tid){
$task = Task::find($tid);
$task->delete();
return response()->json('Task Deleted!');
}
}
I tried for a while to mess around with api and the controller, but to no avail. Most of the questions asked here give "just use params" as an answer, but despite my best efforts I don't seem to be able to get away with "just" params (willing to be proven wrong.)
How can I remove the parameters from a URL after processing in my controller? Like this one:
mydomain/mypage?filter%5Bstatus_id%5D
to
mydomain/mypage
I want to remove the parameters after the ? then I want to use the new URL in my view file. Is this possible in laravel 5.2? I have been trying to use other approaches but unfortunately they are not working well as expected. I also want to include my data in my view file. The existing functionality is like this:
public function processData(IndexRequest $request){
//process data and other checkings
return view('admin.index')
->with([
'data' => $data,
'person' => $persons,
]);
}
I want it to be like:
public function processData(IndexRequest $request){
//process data and other checkings
// when checking the full url is
// mydomain/mypage?filter%5Bstatus_id%5D
// then I want to remove the parameters after the question mark which can be done by doing
// request()->url()
// And now I want to change the currently used url using the request()->url() data
return view('admin.index')
->with([
'data' => $data,
'person' => $persons,
]);
}
I'm stuck here for days already. Any inputs are appreciated.
You can use request()->url(), it will return the URL without the parameters
public function processData(IndexRequest $request){
$url_with_parameters = $request()->url();
$url= explode("?", $url_with_parameters );
//avoid redirect loop
if (isset($url[1])){
return URL::to($url[0]);
}
else{
return view('admin.index')
->with(['data' => $data,
'person' =>$persons,]);
}
}
add new url to your routes and assuming it will point to SomeController#SomeMethod, the SomeMethod should be something like :
public function SomeMethod(){
// get $data and $persons
return view('admin.index')
->with(['data' => $data,
'person' =>$persons,]);
}
I hope this helps
I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......
Following is route:
$router->get('/securityquestionlist', [ 'as'=> 'SecurityQuestionListIndexRoute', 'uses'=> 'SecurityQuestionListController#index']);
I have following action in controller class:
public function index()
{
$model = new SecurityQuestionListModel();
$data = $model->select('question','created_at', 'updated_at', 'status')->where('status', 1)
->orderBy('created_at', 'desc')
->paginate(3);
if(Request::ajax()){
return response()->json(['rData' => $data]);
}else{
return view('securityquestionlist.index' /* ,['rData'=> $data]*/ );
}
Following is Ajax code:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#IdSQLTable').DataTable({
'ajax' : 'http://localhost:9901/securityquestionlist',
'cache' : false
});
} );
</script>
I am receiving following AJAX response from server:
{"rData":{}}
Can some one guide me, in case of AJAX why $data value is not returning from server. If I disable ajax, and load normal page then value is received on client side and table rows are populated with data. Right now I have made it remarks in :
return view('securityquestionlist.index' /* ,['rData'=> $data]*/ );
Think you have to convert the data to array using toArray()
$rData = $data->toArray();
The send response using
return response()->json(['rData' => $rData]);