Laravel Passing Data From One View to Another View - php

I am thinking of making a landing page from the home page, which will direct the guest to the register page. I thought of making two forms for sending data and two submit buttons in them, let's say reader and writer and according to the button they use to go to the register form page, I want to pass the profession string from the button in the landing page and then, place it into the register form in /auth/register.
{!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}
{!! Form::submit('Writer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}
{!! Form::open(array('url' => '/auth/register', 'profession' => 'reader')) !!}
{!! Form::submit('Reader', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
It is not directing me to the page app.com/auth/register. But it works when I directly type the link.
What I thought was using $profession in /auth/register/ and access the value and use it as a hidden field in the registeration form.
(using laravel 5.1)
Edit:
In view source:
<form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="writer"><input name="_token" type="hidden" value="dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL">
<input class="btn btn-warning" type="submit" value="Writer">
</form>
<form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="reader"><input name="_token" type="hidden" value="dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL">
<input class="btn btn-default" type="submit" value="Reader">
</form>
Edit 2:
{!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}
{!! link_to('/auth/register', 'Writer', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
I tried this instead. At least, now it is directing the page but I still can't access the data value of profession
Edit 3:
Routes:
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::get('/', function()
{
return view('pages.home');
});
and https://app.com/auth/register is working.

Here's a step by step walkthrough on how to implement it. I tested it. So it works. This is for 'writer', but you could replicate it as you had originally planned for other professions.
I assume you've registered the Laravel Collective package since you're using the curly braces and exclamation points.
Step 1:
In your landing page view, where you have the writer button, add a hidden field with the string 'writer'. Like this:
{!! Form::open(['route' => ['writer_path']]) !!}
{!! Form::hidden('profession', 'writer') !!}
{!! Form::submit('Writer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}
Not that in the open field we are using a named route ('writer_path').
Step 2:
Register a route and a controller on your routes.php file, like this:
Route::post('auth/register', [
'as' => 'writer_path',
'uses' => 'SampleController#displayForm'
]);
Step 3:
In your sample controller, you define the displayForm method.
Within that method you first obtain the value you passed from the landing page view.
If you don't know how to create a controler, you can do
php artisan make:controller SampleController
from the command line
Because the value arrives as an array, you have to obtain the string 'writer' from the array and then pass it to the new view (the view with the registration form for the writer).
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class SampleController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function displayForm()
{
$input = Input::get();
$profession = $input['profession'];
return view('writerregistration', ['profession' => $profession]);
}
}
Last Step:
In the new view which you will create as writerregistration.blade.php, you will display the form with the field you just passed ('profession') which contains the string 'writer'. Like this:
{!! Form::open() !!}
{!! Form::label('username', 'Username:') !!}
{!! Form::text('username', null, ['class' => 'form-control']) !!}
{!! Form::label('profession', 'Profession:') !!}
{!! Form::text('profession', $profession, ['class' => 'form-control']) !!}
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
{!! Form::label('passowrd', 'Password:') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
{!! Form::label('password_confirmation', 'Password Confirmation:') !!}
{!! Form::password('password_confirmation', ['class' => 'form-control']) !!}
{!! Form::submit('Sign Up', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Presto, you've populated the field in the registration form for the writer with the info on the hidden field that belonged to the writer button in the landing page.

Related

When method changed to GET route not working like POST method Laravel

I have a search bar in view:
{!! Form::open(['name' => 'myForm',
'method' => 'GET',
'action' => 'AreaController#search',
'files' => true,
'onsubmit' => "return validateForm()"])
!!}
{!! Form::submit('جستجو', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
And in route file, web.php:
Route::get('area/search/', 'AreaController#search')->name('area.search');
AreaController:
public function search(Request $request) {
return " it is working" ;
}
But when I click on the button, the browser is showing a blank page. When I use POST method it is working, but if I change to GET method, it is not working.
Thank you.
For Post Method, you need to add CSRF Token To A Form
{!! Form::open(['method' => 'POST']) !!} <--------- Change to POST method
{!! Form::token() !!} <----------- Add this line
{!! Form::submit('جستجو', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
Route::post('area/search/', 'areacontroller#search')->name('area.search');
OR
If you don't want to add CSRF Token To A Form, You can add that to the route file.
Attaching The CSRF Filter To A Route
Route::post('profile', array('before' => 'csrf', function()
{
//
}));
For More Details, you can refer to https://laravel.com/docs/4.2/html

Auto populate "Form::text" from "Form::model" after adding extra attribute(class) in Laravel

I am currently rendering and populating a form in laravel using the laravelcollective plugin. this is working as expected:
{!! Form::model($user, ['action' => 'user#updateUser']) !!}
<div class="form-group">
{!! Form::label('user_name', 'Name') !!}
{!! Form::text('user_name') !!}
</div>
<button class="btn btn-success" type="submit">Update</button>
{!! Form::close() !!}
The above code generates the form and populates the input field with the user's name.
If I want to add a class attribute to the form input like so:
{!! Form::text('user_name', '', ['class' => 'form-control']) !!}
It does not populate the input value because in theory I've set the default value (second parameter) to ''.
Is there a way of populating the value and adding a class without explicitly doing so like this:
{!! Form::text('user_name', $user->user_name, ['class' => 'form-control']) !!}
By doing the above it defeats the object of rendering the form via a model {!! Form::model($user, ['action' => 'user#updateUser']) !!} as I may as well parse the $user as a variable onto the template, which I don't want to do.
Then you can use null instead "" blank
{!! Form::text('user_name', null , ['class' => 'form-control']) !!}
You can also use old function
{!! Form::text('user_name', $user->user_name or old('user_name'), ['class' => 'form-control']) !!}
This $user->user_name or old('user_name') can be expressed as
If $user->user_name is exist then put value of $user->user_name otherwise check for old input which is "" blank first time and have some value if you redirect back if any error comes.
or simply use isset method or in php 7 use $user->user_name ?? ''
{!! Form::text('user_name', $user->user_name ?? '', ['class' => 'form-control']) !!}

2 button accessing different method in the same controller with the same route using Laravel

I have 2 buttons in my blade. I want to update the same database column with them, when I click button 1, it updates the column to 1, when I click the second button it updates the column to 2. I am using the same route to do this because I have to pass the "id" from the view to the controller.
buttons in the view:
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::submit('Accpet', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::submit('Send to Super Admin', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
Routes
Route::get('/notification_list/notification_application/{notification_id}', 'AdminController#approveNotification')->name('show.approve_notification_application');
Route::get('/notification_list/notification_application/{notification_id}', 'AdminController#sendNotificationToSuperAdmin')->name('show.approve_notification_application');
Controller
public function approveNotification($id){
$notification = Notification::find($id);
$notification->approved = '2';
$notification->save();
return redirect()->route('admin.notification_list');
}
public function sendNotificationToSuperAdmin($id){
$notification = Notification::find($id);
$notification->approved = '1';
$notification->save();
return redirect()->route('admin.notification_list');
}
I don't know how to do this. When I click any button, only the second route seems to work which means irrespective of which button I click, it always updates the table to with the value 1.
That's because you can't set two or more routes with same URL and method type. you can use same URL with other type like Route:get('hi') and Route::post('hi').
Back to your problem you can do something like this:
Buttons
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::hidden('type', 0) !!}
{!! Form::submit('Accpet', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::hidden('type', 1) !!}
{!! Form::submit('Send to Super Admin', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
Controller
public function approveNotification(Request $request, $id){
$notification = Notification::find($id);
$notification->approved = $request->input('type') == 1 ? 1 : 2;
$notification->save();
return redirect()->route('admin.notification_list');
}
don't forget to insert use Illuminate\Http\Request in the top of the file after namespace.
Routes
keep the first and delete the second one.
The reason to your problem :
in the routes file - you called 2 methods with the same name - thats why it arrive to the 2nd route ( the seconed name overwrite the first one );
How to solve it ?
First - delete one of the routes.
Then - add an hidden field in your form, so you can know later which button was clicked
After that - you will need to add an IF in your controller - according to $id
something like this :
if ($yourHiddenField == 1) {
... your code here...
} elseif ($yourHiddenField == 2 ) {
... your code here ...
}
( you will need to get the value of the hidden field first )

Input::hasFile() not working

I am trying to check if the image that i am trying to upload is getthing processed, but when i try to use de Input::hasFile('upimg') to check it out it doesnt do anything. This is my form:
{!! Form::open(array('route' => array('uploadimage'), 'method' => 'POST')) !!}
{!! csrf_field() !!}
<div class="form-group">
{!! Form::label('newimg', 'Choose Image to upload:', ['class' => 'control-label']) !!}
{!! Form::file('upimg', null) !!}
</div>
<div class="form-group">
{!! Form::label('directory', 'Choose directory:', ['class' => 'control-label']) !!}
{!! Form::select('newimage', [ '' => 'none'] + $imagesdir , '', ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Upload', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
And this is the code i am using to check the file:
public function doUpload()
{
if (Input::hasFile('upimg')){
echo 'uploaded';
};
}
As you can see i am trying to check:
{!! Form::file('upimg', null) !!}
If i am not wrong its suppose to give mi back the echo but nothing happens. Why is this not working? what am i doing wrong?
Be sure you have an 'file' => true option in your form definition like below:
{!! Form::open(['route' => ['uploadimage'], 'file' => true]) !!}
This option causes that your form will be render with enctype="multipart/form-data" option in HTML form tag. See documentation: HTML enctype
You don't have to add method => 'POST' to your definition of form, because Laravel 5 sents forms with POST method by default.
Please let me know if this solution will not work for you.
For checking files on server side you can use methods of Request object like in documentation of Laravel: Files in Laravel

Lravel 5.1 How to find database records through a from?

In the admin area of my app, I want to have a form by which an admin can find a project by its id and show the project details there. What is the best approach to implement this?
This is what I have tried:
//route
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
//form
{!! Form::open(['action' => 'AdminController#showProject', 'method' => 'get']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
//controller method
public function showProject(Request $request)
{
$project=Project::find($request->get('project_id'));
return view('admin.projects.showProject', compact('project'));
}
It almost worked but there is little problem. After retrieving the requested project, the ULR is like this:
admin/projects/%7Bproject_id%7D?project_id=5
I want it be like this one:
admin/projects/5
How can I solve this problem?
Create the following routes:
Route::get('admin/projects', 'AdminController#getProject');
Route::post('admin/projects', 'AdminController#postProject');
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
In your getProject function you return a view that show the form where the user can enter an ID. (The one you already have):
{!! Form::open(['action' => 'AdminController#postProject', 'method' => 'post']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
In your postProject function you just send a redirect to admin/project/{project_id} URL:
public function postProject(Request $request)
{
return redirect('admin/projects/' . $request->project_id);
}
In your showProject function you just retrieve the record and return a view with the information:
public function showProject($ProjectID)
{
$project=Project::find($ProjectID);
return view('admin.projects.showProject'),
->with('Project', compact('project'));
}
Try changing your controller method to
public function showProject($project_id)
{
$project=Project::find($project_id);
return view('admin.projects.showProject', compact('project'));
}
You don't need to use request on get route.

Categories