Save and Display POST request from external website in LARAVEL - laravel

I'm handling a POST request from an external website I am able to store the data but how do I display it on the blade.
Controller:
public function parse(Request $request)
{
$files = File::create([
'Data1' => $request->input('Data1'),
'Data2' => $request->input('Data2'),
'Data3' => $request->input('Data3'),
]);
return view('fileview');
}
Route:
Route::any('fileview', 'App\Http\Controllers\FilesController#Files')->name('parse');
Blade:
{{ $Data1 }}
EDIT:
I'm receiving this error from my returnURL but the data stores perfectly
Trying to get property 'profile' of non-object (View: D:\Systems\final\resources\views\partials\dash-sidenav.blade.php) {"exception":"[object] (Facade\\Ignition\\Exceptions\\ViewException(code: 0): Trying to get property 'profile' of non-object (View: D:\\Systems\\final\ esources\\views\\partials\\dash-sidenav.blade.php) at D:\\Systems\\final\ esources\\views/partials/dash-sidenav.blade.php:4)
dash-sidenav.blade.php
#if ((Auth::User()->profile) && Auth::user()->profile->avatar_status == 1)
<img src="{{ Auth::user()->profile->avatar }}" alt="{{ Auth::user()->name }}" class="user-avatar-nav">
#else
<img src="{{asset('images/mascot.jpg')}}" alt="" class="user-avatar-nav" >
#endif

Ty editing your controller and add ->with() like this:
public function parse(Request $request)
{
$files = File::create([
'Data1' => $request->input('Data1'),
'Data2' => $request->input('Data2'),
'Data3' => $request->input('Data3'),
]);
return view('fileview')->with('Data1', $request->input('Data1'));
}

Related

Why is my validation on my Laravel form failing and routing to a different page?

I'm using Laravel 5.4 and trying to submit a form and check if an input is numeric. But when I get to the validation it routes to a different page.If I remove the validation then everything works correctly.
This is my blade form:pin_verification.blade.php
#extends('layouts.master')
#section('title','Verify Order')
#section('extra_head_info')
<meta name="csrf-token" content="{{ csrf_token() }}">
#endsection
#section('content')
#if(count($errors) > 0)
<ul>
#foreach($errors->all() as $error)
<li class="alert alert-danger">{{$error}}</li>
#endforeach
</ul>
#endif
{{ Form::open(['action' => 'PinVerificationController#pinValidation','id'=>'pin_verification_form']) }}
We sent a text message to {{$clean_number}}. You should receive it within a few seconds.<br><br>
{{ Form::label('Pin Code', null, ['class' => 'control-label']) }}
{{ Form::text('pin_number', null,['placeholder' => 'Pin Code'])}}<br><br>
Enter a 4 digit pin you received by phone.
<br>
<br>
{{ Form::submit('Verify',['name'=>'validate'])}}
{{ Form::close() }}
#endsection
When I click my submit button on my pin_verification.blade.php form
I go to my PinVerificationController.php:
class PinVerificationController extends Controller
{
public function pinValidation(Request $request){
if($request->has('validate')){
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
return redirect("/test/create");
//return $this->validatePin($request);
}else{//choosing verification method
return $this->choosePinVerficationMethod($request);
}
}
public function init(){
...
}
public function choosePinVerficationMethod(Request $request){
...
}
}
My code goes into pinValidation function and into the first if statement but when it hits the
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
It routes to my init() function to a different controller, CheckoutController.php
If I remove my validation then my code works correctly and I get redirected to redirect("/test/create");
Why is this happening?
My routes are:
Route::get('/order/verify/{encrypted_key}', ['as'=>'pinverification','uses'=>'PinVerificationController#init']);
Route::get('/test/create', ['as'=>'orders_create', 'uses'=>'OrdersController#init']);
Route::post('/order/verify', ['as'=>'pinverification1', 'uses'=>'PinVerificationController#pinValidation']);
Route::get('/order/checkout/{product_id}', ['as'=>'checkout', 'uses'=>'CheckoutController#init']);
It's because you aren't actually checking the validation, you're just performing the method, so then naturally it just routes to /test/create as per the first return redirect instruction.
Try this instead:
public function pinValidation(Request $request){
if($request->has('validate')){
$validator = Validator::make($request->all(), [
'pin_number' => 'required|numeric'
]);
if ($validator->fails()) {
return redirect('test/create')
->withErrors($validator)
->withInput();
} else {
// Success criteria, validation passed.
}
} else {
$this->choosePinVerficationMethod($request);
}
}
The validate method, you have used here redirects you itself:
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
You should rather use:
$validator = Validator::make($request,[
'pin_number' => 'required|numeric'
]);
and then check for:
if ($validator->fails()) {
//redirect
};
Reference

Laravel guzzel result implement to blade laravel

i have problem while displaying json data in laravel blade, i request data using guzzel and this is my code:
public function index(){
$client = new Client();
$schedules = $client->get('45.112.125.25:5500/md_data/schedules/', [
'query' => [
'vCategory' => '129',
'vStartDate' => '2017-07-01',
'vEndDate' => '2017-09-31',
'vReadKey' => 850601165,
'vRows' => 10,
'vOffset' => 0
]
]);
// return $schedules->getBody();
return view('trainingList')->with('schedules', $schedules->getBody());
}
this result :
[{"f_training_schedule_id":324,"f_training_category_id":129,"f_training_category":"Workshop Business","f_city_id":216,"f_city_name":"Kota Jakarta Selatan","f_training_schedule_startdate":"2017-08-11T17:00:00.000Z","f_training_schedule_enddate":"2017-08-12T17:00:00.000Z","f_training_schedule_batch":1,"f_training_schedule_trainer":58,"f_training_schedule_address":"<!--StartFragment-->JL TB Simatupang, Cilandak, RT.3/RW.3, Cilandak Tim., Ps. Minggu<!--EndFragment-->\r\n\r\n<br>"}]
how to get specific data from the above results.
for example I want to get value from f_training_schedule_id
You need json_decode():
return view('trainingList')->with('schedules', json_decode($schedules->getBody(), true));
In your blade template
$schedules[0]['f_training_schedule_id'];
Foreach:
#foreach ($schedules as $schedule)
<p>{{ $schedule['f_training_schedule_id'] }}</p>
#endforeach

Passing variables to view

I think I might have discovered a bug. This is what I have until now.
Routes.php
Route::get('/{user}/{char_dynasty}/{char_name}/selectedok', array(
'as' => 'char-profile-get',
'uses' => 'CharacterController#getDropDownList'));
Route::post('/user/{user}/{char_dynasty}/{char_name}/selectedok', array(
'as' => 'char-profile-post',
'uses' => 'CharacterController#postDropDownList'));
CharacterController.php
class CharacterController extends BaseController{
public function getDropDownList($user, $char_dynasty, $char_name)
{
if(Auth::check())
{
$user = Auth::user();
return View::make('layout.notification', array(
'user' => $user,
'char_dynasty' => $char_dynasty,
'char_name' => $char_name));
}
else
{
return App::abort(404);
}
}
public function postDropDownList()
{
if (Auth::check())
{
$user = Auth::user();
$char_name = User::find($user->id)->characters()->where('char_name', '=', Input::get('choose'))->first();
return Redirect::route('char-profile-get', array(Session::get('theuser'),
$char_name->char_dynasty,
$char_name->char_name));
}
}
}
profile.blade.php (snippet)
<form action="{{ URL::route('char-profile-post') }}" method="post">
<select name="choose">
<option value="choose">Choose a character</option>
<option> {{ $c_name }} </option>
</select>
<input type="submit" value="Ok">
{{ Form::token() }}
</form>
The error says that $char_dynasty is undefined which is usually used
<option> {{ $char_dynasty }} </option>
I changed to populate the drop down list with another variable to be able to execute the database query $char_name from postDropDownList.
If I do in function getDropDownList, var_dump($char_dynasty); var_dump($char_name) I get the following
string 'Spirescu' (length=8)
string 'Ion' (length=3)
The point is that the parameters in getDropDownList are with the correct data, but are not transfered to the View. What am I doing wrong? I don't know how to access the variables in the View?
I have also tried return View::make('layout.notification', compact('user' , 'char_dynasty' , 'char_name' ));
or
$data = array(
'user' => $user,
'char_dynasty' => $char_dynasty,
'char_name' => $char_name);
return View::make('layout.notification', $data);
I receive the same error. or $data is not defined.

Laravel 4 password reminder: redirection issue

I'm using the Laravel 4 password reminder functionality, as described here: http://four.laravel.com/docs/security#password-reminders-and-reset. In order to generate the token, send the email and create de DB record in the password_reminder table, I use the standard code in my routes file :
Route::post('password/remind', function() {
$credentials = array('email' => Input::get('email'));
return Password::remind($credentials);
});
This code is suppose to send me back to my input form in case of any error (unknown email address for instance). Instead of that, I get a MethodNotAllowedHttpException. The reason is Laravel don't try to send me back to my form URL (which is /password/forgot): he tries to redirect me to /password/remind, in GET, and this route does not exist (of course) in my routes.php file.
I checked the code of the Illuminate\Auth\Reminders\PasswordBroker class, which is responsible of this redirection, and found out this method :
protected function makeErrorRedirect($reason = '')
{
if ($reason != '') $reason = 'reminders.'.$reason;
return $this->redirect->refresh()->with('error', true)->with('reason', $reason);
}
I replaced $this->redirect->refresh() by $this->redirect->back(), and everything is now working as excepted. But as I couldn't find any comment on this bug anywhere, I assume I'm doing something wrong… But I can't find what !
Here is my routes.php file:
Route::get('password/forgot', array('as' => 'forgot', 'uses' => 'SessionsController#forgot'));
Route::post('password/remind', function() {
$credentials = array('email' => Input::get('email'));
return Password::remind($credentials);
});
Route::get('password/reset/{token}', function($token) {
return View::make('sessions.reset')->with('token', $token);
});
Route::post('password/reset/{token}', array('as' => 'reset', 'uses' => 'SessionsController#reset'));
my SessionsController relevant code:
class SessionsController extends BaseController {
[...]
public function forgot() {
return View::make('sessions.forgot');
}
public function reset() {
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'password_confirmation' => Input::get('password_confirmation')
);
Input::flash();
return Password::reset($credentials, function($user, $password) {
$user->password = Hash::make($password);
$user->save();
return Redirect::to('home');
});
}
}
and finally my view code:
{{ Form::open(array('url' => 'password/remind', 'class' => 'form', 'role' => 'form', 'method'=>'post')) }}
<div class="form-group">
{{ Form::label('email', 'E-mail') }}
{{ Form::text('email', '', array('autocomplete'=>'off', 'class' => 'form-control')) }}
</div>
{{ Form::submit("Envoyer", array("class"=>"btn btn-primary")) }}
{{ Form::close() }}
If it is possible, I highly recommend you to upgrade to Laravel 4.1, because it comes with a more flexible (also easier to understand and to work with) solution for password remind/reset.
Check out this example with Laravel 4.1:
https://github.com/laracasts/Laravel-4.1-Password-Resets/blob/master/app/controllers/RemindersController.php

Detecting type of validation error in Laravel 4

I'm new to Laravel and the following works but it doesn't seem very 'Laravel' to me - I just need to detect which validation rule the message refers to (required,email,unique etc):
#if ($errors->has('email'))
{{ $errors->first('email') }}
#if (strpos($errors->first('email'), 'has already been taken'))
{{ HTML::link('password', 'Need a reminder?', array(), FALSE); }}
#endif
#endif
Any suggestions?
Thanks
In your controller, would would do something like this (depending on how it is configured)
Controller
public function store()
{
$validator = Validator::make(Input::all(), array(
'name' => 'Dayle',
'email' => 'required|min:5'
));
if ($validator->passes())
{
// Redirect to success page or something
}
return Redirect::back()
->withInput()
->withErrors($validator)
->withFailed($validator->failed())
}
then in your view
View
#if ($errors->has('email'))
The specific email rule that failed was: {{ $failed['email'] }}
#endif

Resources