i'm trying to redirect this action method in my controller but i couldn't make it work. the action will goes to posts Resources #show
here is the controller.
public function readbyid(Request $request){
if(isset($request->data['id'])){
return redirect()->action(
'PostController#show', ['id' => $request->data['id']]
);
}else{
return redirect()->back();
}
}
Route:
Route::get('markread/{id}', 'NotifyController#readbyid' );
For optional parameters, use '?'
Route::get('markread/{id?}', 'NotifyController#readbyid');
Use $request->id instead $request->data['id']
you are not getting any value $request->data['id'] like this thats why it goes to the else part.
Related
I am trying to redirect back to some page like my pricing page after login in laravel but it redirect back to previous pages even the ones that I dont want it to redirect back to.
public function login()
{
Session::put('url.intended',URL::previous());
return view('login');
}
public function loginPost()
{
if ($this->auth->attempt(array('email' => Input::get('email'), 'password' => Input::get('password')))){
return Redirect::to(Session::get('url.intended'));
}
return back();
}
You don't need to build login logic by yourself in laravel unless you want to , laravel provide pre-built auth scaffolding which you can use !
But what you are asking to redirect back for that you should use
return back();
At the ending of your function not at the ending of your class.
if ($this->auth->attempt(array('email' => Input::get('email'), 'password' => Input::get('password')))){
if(!empty($request->session()->get('url.intended'))){
return redirect($request->session()->get('url.intended'));
}else{
return redirect()->route('dashboard');
}
}
Im new to laravel.
I'm creating this project and here, after i save my data, i want to return to my previous state.
public function storeData(Request $request){
// dd($request->all());
$this->validate($request,[
'Province'=>'required',
'District' => 'required|max:100|min:3',
'Electrorate' => 'required|max:100|min:3',
'PollingBooth' => 'required|max:100|min:3',
'issueTitle' => 'required|max:160|min:3',
'issueDetails'=>'required|max:500|min:30',
'issueCategory'=>'required'
]);
$pollingbooth = new pollingbooth;
$pollingbooth->Province=$request->Province;
$pollingbooth->District=$request->District;
$pollingbooth->Electrorate=$request->Electrorate;
$pollingbooth->PollingBooth=$request->PollingBooth;
$pollingbooth->issueTitle=$request->issueTitle;
$pollingbooth->issueDetails=$request->issueDetails;
$pollingbooth->issueCategory=$request->issueCategory;
$pollingbooth->save();
$pollingBoothNames=pollingbooth::all();
// dd ($pollingBoothNames);
return view('issues')->with('pollingBoothNamez',$pollingBoothNames);
// return redirect()->back();
}
above is my code. i tried
return view('issues')->with('pollingBoothNamez',$pollingBoothNames)->redirect()->back();
but no luck. what should i do here?
Im having the same problem with updating a "Issue" as well.
This will work :
return redirect()->back()->with(''pollingBoothNamez', $pollingBoothNames);
You can simply call the route like this return redirect('home/dashboard');
return redirect()->back();
or
return Redirect::to('your-route-url');
I want to test back() function in laravel but I am not sure how to do that .
For e.g
public function destroy($id)
{
$query = Query::findorfail($id);
$query->delete();
return back()->with('success','Query deleted successfully');
}
In the above method the url is redirected back . But Can I create a test so that I can assert back()
I have checked the TestResponse class. A function like assertRedirectBack is not yet provided.
I think assertRedirect is gonna work:
$response = $this->delete(...);
$response->assertRedirect($uri);
The previous url could be stored in session. I am not sure about that. So please try this:
$response->assertRedirect(session()->previousUrl());
Try with return redirect()->back();
Use TestCase's from (from MakesHttpRequests trait)
$response = $this
->from('{route}') // without this would redirect to '/'
->delete('/asset/{id}');
$response->assertRedirect('{route}');
my route:
Route::get('page/{key_id_fk}', 'PagesApiController#show');
my function:
public function show($key_id_fk)
{
$sub=DefintionDetails::find($key_id_fk);
// $main=Definition::where([['type','=',1],['available','=',1],['id_definition','=',$sub->id_def]])->get();
return response()->json($sub , 200);
}
on post man route is page?key_id_fk=1 give error 404 not found key in data base but didn't read.
You should be accessing page/1 rather than page?key_id_fk=1 as you are not using parameter queries in your request url.
Your route format is page/$key_id_fk.
In the route file:
Route::get('page/{key_id_fk}', 'PagesApiController#show');
In the controller:
public function show($key_id_fk){
$sub = DefintionDetails::find($key_id_fk);
if($sub){
return response()->json(['success' => true, 'sub' => $sub]);
} else {
return response()->json(['success' => false, 'error_message' => 'No data found!']);
}
}
Your postman route:
http://example.com/page/1
You are setting key_id_fk as http://example.com/page/1 in route and passing parameter as http://example.com/page?key_id_fk=1 difference is first one is URL route data and second is GET parameter data to get data from URL route you have this public function show($key_id_fk) and to get data from GET parameter public function show(Request $request) and $request->key_id_fk.
so change URL to this http://example.com/page/1 format
or
change getting method in the controller to public function show(Request $request) and $request->key_id_fk
I want user to get calculated result. I had to redirect localhost:8000/question/33 to localhost:8000/caclulate/some_payload
I have no idea how to do this.
class QuestionController extends Controller
public function index($id)
if(question->id == 33)
#Here I want to invoke calculate($payload)
return view('quiz.index', [
'payload' => $payload,
'question' => $question,
]);
}
public function calculate($payload)
{
return view('quiz.result', [
...
]);
}
I've tried $this->calculate($payload) and calculate().
If I understand right, you want call controller method from another controller method.
You could try it this way:
$request = Request::create('calculate/some_payload', 'GET', $params);
return Route::dispatch($request)->getContent();
For more info see Jason Lewis answer here: Consuming my own Laravel API
Are you expecting Request::json () to provide form input? Or, are you looking for Input::all() form input?
I've solved, my problem with:
return Redirect::action('QuestionController#calculate',
array('payload' => $payload));
But it generates address with parameter
http://localhost:8000/calculate?payload=%242y%2410%24V1OqC7rwp1w1rPXYQ42lRO9lJYQvmXB3nScw6D0EVeFR7VUz0QyCS
And I want to use$_POST instead of $_GET, so I need to invoke method with $_POST and catch $payload in method calculate. How can I do this?