Laravel 8 routing - laravel

I have a a function which gets all the data from the date inputted by the user and pass it to the table in view. In that table individual row of data retrieved from database has one anchor tag which carries id of each row in database to update the column in the database. All works fine but i'm having problem in redirecting to the table after update. i'm getting problem due redirecting to the page maturity_reqController/'whatever_ids' which doesnot exists but dont khow how to fix it.
My anchor Tag goes
Request
My Route goes
Route::get('maturity_reqController/{id}', [SettlementController::class,'maturity_reqController']);
my Controller goes
function maturity_reqController($id){
$forset = new Settlement();
$forset->org_document_id = $id;
$forset->save();
$data = Doc::find($id);
$data->status = "Maturity Requested";
$data->save();
return redirect('maturity_reqController');
}

You can try this
return redirect()->back();
for more details : laravel redirects

Add a name to your route like so:
Route::get(
'maturity_reqController/{id}',
[SettlementController::class,'maturity_reqController']
)->name('maturity.req');
Then whenever you need to link to that route use
Request
Edit: to redirect to that route:
return redirect()->route('maturity.req', ['id' => $items->id]);
More on Named Routes

Your Link
Request
web.php
Route::get('maturity_reqController/{id}', [SettlementController::class,'maturity_reqController'])->name('maturity_req');
Controller
function maturity_reqController($id) {
$forset = new Settlement();
$forset->org_document_id = $id;
$forset->save();
$data = Doc::find($id);
$data->status = "Maturity Requested";
$data->save();
return redirect()->route('maturity_req', ['id' => 'Your ID'])->with('success', 'Your Message');
}

Related

How to use parameter from function to create an URL? Laravel Routing

I'm sending an URL hashed and when i get it i have to show a view on Laravel, so i have those functions on the controller and also some routes:
This are my routes:
Route::post('/sendLink', 'Payment\PaymentController#getPaymentLink');
Route::get('/payment?hash={link}', 'Payment\PaymentController#show');
And this are the functions i have on my controller:
public function getPaymentLink (Request $request){
$budgetId = $request['url.com/payment/payment?hash'];
$link = Crypt::decryptString($budgetId);
Log::debug($link);
//here to the show view i wanna send the link with the id hashed, thats why i dont call show($link)
$view = $this->show($budgetId);
}
public function show($link) {
$config = [
'base_uri' => config('payment.base_uri'), ];
$client = new Client($config);
$banking_entity = $client->get('url')->getBody()->getContents();
$array = json_decode($banking_entity, true);
return view('payment.payment-data')->with('banking_entity', $array);
}
And this is getting a "Page not found" message error.
What i want to to is that when i the client clicks on the link i send him that has this format "url.com/payment/payment?hash=fjadshkfjahsdkfhasdkjha", trigger the getPaymentLink function so i can get de decrypt from that hash and also show him the view .
there is no need to ?hash={link} in get route
it's query params and it will received with $request
like:
$request->hash
// or
$request->get('hash')
You need to define route like this:
Route::get('/payment/{hash}', 'Payment\PaymentController#show');
You can now simply use it in your Controller method like below:
<?php
public function getPaymentLink (Request $request,$hash){
$budgetId = $hash;
// further code goes here
}

Laravel Routing Query

I've made some bespoke pages in my admin of my site and they as the first segment of the URL.
e.g
/property-hartlepool
I thought of adding a trap all route into my routes file :
Route::get('{any?}', 'PagesController#view');
The problem I have is it's totally overwriting my other routes, I guess that's the name of a trap all route. However I'd like it to skip if it can't find a match.
I had a route for admin
/admin
But now it throws a 404 Error...
My PagesController#view method is :
public function view(Request $request)
{
$route = $request->segment(1); // $request->path();
// get page content
$page = Page::where('route', $route)->firstOrFail();
// If not full width, get four latest properties
//$properties = Property::latest_properties_for_frontend();
//metadata
$meta = get_metadata($page);
//page is Temporary
return view('frontend.'.themeOptions().'.page', [
'route' => $route,
'meta' => $meta,
'page' => $page
]);
}
Is their a better way of doing this, I do have other routes that sit at "top" level too. e.g...
Route::get('/property/{property}/{propertyId}', 'PropertiesController#property');
declare your trap Route as the last route.
Route::get('/admin', 'AdminController#view');
...
...
Route::get('{any?}', 'PagesController#view');

i need an explanation about how this update code works (Laravel)

public function editCategory(Request $request,$id = null){//we pass the $id
if($request->isMethod('post')){
$data = $request->all();
Category::where(['id'=>$id])->update(['name'=>$data['category_name'],
'description'=>$data['description'],'url'=>$data['url']]);
return redirect('/admin/view-categories')->with('flash_message_success','Category Updated Successfully');
}
$categoryDetails = Category::where(['id'=>$id])->first(); return view('admin.categories.edit_category')->with(compact('categoryDetails'));
}
//this code is working in my controller
Simply there are two ways to invoke this action:
Request in HTTP POST method
if you make a request to the URL /admin/edit-category/12345 in HTTP POST method
then you make an update of the model called Category with id = $id (in my example
$id is 12345) and then you will be redirected in /admin/view-categories with a flash
message variable valorized with Category Updated Successfully
Request in HTTP GET method (or any other HTTP method)
if you make a request to the URL /admin/edit-category/12345 in HTTP GET method then the action responds with a view of the model Category with id = $id (in my example $id is 12345) and put the fields of the model in the view params.
ok i will explain to you this is edit fonction so it make modification of data stored in database
if($request->isMethod('post')){//if your data submitted to database
$data = $request->all();//recupration of all information
Category::where(['id'=>$id])->update(['name'=>$data['category_name'],
'description'=>$data['description'],'url'=>$data['url']]);//change description , categoryname and url
return redirect('/admin/view-categories')->with('flash_message_success','Category Updated Successfully')//and return seccess alert in your view;
}
$categoryDetails = Category::where(['id'=>$id])->first(); return view('admin.categories.edit_category')->with(compact('categoryDetails'));
public function editCategory(Request $request,$id = null){//we pass the $id
//check that the request is a POST request
if($request->isMethod('post')){
//Okay so it is, now store all request parameter in it's own variable
$data = $request->all();
//Access the Eloquent model Category, checks the id and updates
//accordingly
Category::where(['id'=>$id])->update(['name'=>$data['category_name'],
'description'=>$data['description'],'url'=>$data['url']]);
//Return redirect to view-categories with a success message
return redirect('/admin/view-categories')->with('flash_message_success','Category Updated Successfully');
}
//check the first id and return the view with the details, this would only be hit if the above if statement wasn't (wasn't a POST request for example)
$categoryDetails = Category::where(['id'=>$id])->first(); return view('admin.categories.edit_category')->with(compact('categoryDetails'));

Form submit going to the wrong route

I am saving data from a simple form in my Laravel project.
While submitting, it should go to the route that is predefined for store() method. I use such code:
{!! Form::open(['action' => 'PostsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
It goes to the route that is for index() method. Any help?
In store() method, I have such code:
$posts = new Post;
$posts->title = $request->input('title');
$posts->body = $request->input('body');
$posts->save();
return redirect('/');
My web.php contains:
Route::resource('/','PostsController');
Your code is correct bro.. The only reason you're going to index is because of the
return redirect('/'); in the store function... Check whether youdata is saved in the database or not...
Have you tested to see if this actually saves the data still? With Route resources, the route will be the same for both store and index methods, just a different HTTP method.
Maybe your code is working well & data saved in the database. You return redirect('/') it to your index() method, so you don't understand the difference. Check your database.

Laravel Redirect url from {id} to {id}/{name}

I am new in laravel framework now I'm working fully developed website using Laravel. I have changed blog url form {id} to {id}/{name} like www.example.com/news/203 to www.example.com/news/203/title. It's working fine. but i need to redirect if old url enter into current url opened from cache or something else.
Route::get('{id}/{name}', 'EventController#show')
->name('events-detail')
->where([
"id" => "[0-9]+"
]);
You can define another route in which you will find the model by id and use its title to redirect the user to the new route:
Route::get('{id}', function ($id) {
$model = Model::findOrFail($id);
return redirect()->route('events-detail', ['id' => $id, 'name' => $model->name]);
});
Note that you have to change Model with the class you use for this route.
Create 2 routes and add below code.
Route::get('{id}/{name}', function () {
//new URL as you want
return redirect()->route({id}/{name});
});
Route::get('{id}', function () {
//as you want for simple URL
});
I'm assuming the name portion is not really used at all, except for SEO/friendlier urls. If this is the case, just make the name parameter optional, and there will be no need for a redirect:
Route::get('{id}/{name?}', 'EventController#show')
->name('events-detail')
->where([
"id" => "[0-9]+"
]);
This route will match /203 and /203/news-title.

Resources