Laravel 4 Input::old() not working - laravel-4

{"error":{"type":"ErrorException","message":"Undefined offset: 0"
MyController.php
Input::old() is empty. How to store some data in the input old? sorry for my english =D
return View::make('ajax.view')
->withInput()
->withErrors($validation)
->render();

It's works! =D
Input::flash();
return View::make('ajax.view')
->withInput(Input::all())
->withErrors($validation)
->render();

You need to pass over the data into the View:
return View::make('ajax.view')
->withInput(Input::all())
->withErrors($validation)
->render();

Related

Laravel: How to pass a collection to view?

The $result variable displays the json-string correctly. And when I try to send the result to the view, I get an error.
$accreditations = Accreditation::with(['country'])->get();
$result = AccreditationResource::collection($accreditations);
//return $result;
//{"data":[{"id":5,"user_id":1,"login":"Admin","country_id":"4","country":{"id":4,"name":"Austria"}}]}
return View::make('accred_table', $result->login);
//Error 500: Property [login] does not exist on this collection instance.
Help me figure this out
$result is an array. So, there is no login as the error said. If you return only one record from AccreditationResource, you can use
return view('accred_table', ['login' => $result[0]['login']]);
In blade, you will have $login.

How to redirect back to the previous page after saving data in Laravel?

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');

Laravel redirect route from get to resources

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.

How to display error with Session:get() and withErrors()?

I have stack of calls in controller:
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
The function withErrors takes array of errors.
How can I display there messages in template?
I tried:
{{Session::get('MessageBag')}}
So, the latest edition is:
$errors = $validator->messages(); // Here I get $error with fillied data
return redirect('tour/create')
->withErrors($errors)
->withInput();
In template I do:
{{count($errors)}}
It gives me zero
In view file access errors with $errors
{{$errors->first('MessageBag')}}
Try to print $errors print_r($errors). It is global variable for views.
Try this you can see whether the data is available
{{ dd(Request::session()) }}

Laravel 5.2 method to check is view exists?

Is there any method to check if view exists?
Like PHP file_exists, but using internal laravel method for convenience
i want to code like this:
if(__ifViewExist__($view)){
return view($view)->render();
}
return "Page tidak ditemukan";
Yes, an exists() method is available.
if(view()->exists($view)){
return view($view)->render();
}
return "Page tidak ditemukan";
Yes. Using the View facade.
// Return true when welcome.blade.php does not exist in theview folder
View::exists('welcome');
// Return false when login.blade.php does not exist in the view folder
View::exists('login');
try {
return view($view);
} catch (\Exception $e) {
return "Page tidak ditemukan";
}
will be more efficient way
In web.php, you need to type below given code:
Route::get('/Test',function(){
if(View::exists('View'))
{
return view('View.php',['name' => 'Your string']);
}
return "File not found";
});

Resources