Getting a data from a table to insert to another database table - php

I have this table that contains data from the database table 'patient'.
<td>
<a href="{{ URL::to('/addvisit/'.$patient->pID) }}">
<img src="images/visit.png" style="width:30px; height:30px;">
</a>
</td>
when the circle icon is clicked it will redirect to a page with a form that will be inserted in a database table 'visit'.
below is the form:
{!! Form::model($patient, ['url'=>'visit/' . $patient->pID, 'method' => 'PATCH']) !!}
<div class="form-group">
{!! Form::label('fname', 'Full Name',['class' => 'control-label'])!!}
{!! Form::text('pName',null,['class' => 'form-control']) !!}
{!! Form::label('address', 'Address',['class' => 'control-label'])!!}
{!! Form::text('pAddress',null, ['class' => 'form-control']) !!}
{!! Form::submit('SUBMIT',['class' => 'form-control btn btn-success']) !!}
</div>
{!! Form::close() !!}
how can I add the patient ID from the 'patient' table to be set as a foreign key in the 'visit' table.

Pass it again to the view with the form through controller and store it on a hidden input

Related

Laravel update how to update data in index

The problem is i want to skip that edit page. it works as fine. but i wanna edit my data in index view.
i tried this but i took this error
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" value="#foreach ($choices as $choice){{ $choice->question_number }}#endforeach" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Trying to get property of non-object (View: C:\wamp64\www\zainsurgalt\resources\views\choices\index.blade.php)
index view
<td>Edit</td>
edit view
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
controller update
public function update(Request $request,Choice $choice){
Choice::where('id', $choice->id)->update([
'question_number' => $request->input('number')
]);
return redirect()->route('choices.index');
}
All time when you get object property you must be check object exist or not
#if (!empty($duplicate->topic))
<td>Edit</td>
#endIf
also
#if (!empty($choice))
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
#endIf
and finally you use
#foreach ($choices as $choice){{ $choice->question_number }}#endforeach
change $choice to another name for example $_choice , for not confusing above used $choice

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']) !!}

How to style Form

I have a form in Laravel. I want to change the width of the form, because I want to put a other form next to it.
It looks like this
Which codes I should use in app.css? Any Suggestions? I tried codes in app.css but, maybe I should give a ID to form?
{!! Form::open(['method' => 'POST', 'action' => 'BusinessController#buy_energy']) !!}
<img src="images/foods/1.png" width="80px" height="80px"><br>
{!! Form::label('number', 'Ekmek(1 adt = 7 TL): ') !!}
{!! Form::number('number', 'value', ['min'=>1, 'max'=>10 ]) !!}
{!! Form::submit('Satın Al', ['class'=>'btn btn-primary']) !!}
In your blade add in the form the 'class' just like you add the action and the method:
{!! Form::open(['method' => 'POST', 'action' => 'BusinessController#buy_energy', 'class' => 'myForm']) !!}
<img src="images/foods/1.png" width="80px" height="80px"><br>
{!! Form::label('number', 'Ekmek(1 adt = 7 TL): ') !!}
{!! Form::number('number', 'value', ['min'=>1, 'max'=>10 ]) !!}
{!! Form::submit('Satın Al', ['class'=>'btn btn-primary']) !!}
In your Css customize the myForm class:
.myForm{
with: 400px;
}

Laravel inserting "null" into database from form input.

This is a strange problem i've run into. I have a form that takes in First, Last, Title, Email, and Phone which takes that data and inputs into a ContactPerson table I have set up in a database.
When I fill out the form, and hit submit, only the Title, Email, and Phone number get inserted correctly into the database. The first and last names get inserted as NULL.
Here's some of my code that I have using PHPstorm and Laravel 2.5.3
My Create a New Contact view:
<h1 style="text-align:center;">Add A New Contact Person</h1>
<hr/>
{!! Form::open(['url' => '/create']) !!}
<span style="text-align:center;align-content:right;display:block; margin-right:auto;margin-left:auto;">
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('Title', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('email', 'Email: ') !!}
{!! Form::text('Email', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('phone', 'Phone: ') !!}
{!! Form::text('Phone', null, ['class' => 'form']) !!}
</div>
{!! Form::submit('Submit', ['class' => 'btn btn-primary form']) !!}
</span>
{!! Form::close() !!}
Here is my controller called ResourceController I have created:
class ResourceController extends Controller
{
public function resource()
{
$contacts = ContactPerson::all();
return view('pages.resource', compact('contacts'));
}
public function create()
{
return view('pages.create');
}
public function store(Requests\CreateNewContactRequest $request)
{
ContactPerson::create($request->all());
return redirect('resource');
}
}
Here's the validation I have set up in the CreateNewContactRequest class for the form:
public function rules()
{
return [
'First' => 'required|min:2',
'Last' => 'required|min:1',
'Title' => 'required|min:2',
'Email' => 'required|Email|unique:users',
'Phone' => 'required|numeric|min:9'
];
}
Here's what it looks like when I fill out/submit the form.
After hitting submit it redirects to the database dump:
the view of the database after insertion:
Looking at your dump details, your field names need to match your form names or visa versa, so your form names need to change to say:
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First_Name', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last_Name', null, ['class' => 'form']) !!}
</div>
EG for field name First_Name your input needs to be named the same name="First_Name"
Also making sure your fillable details are also correct
protected $fillable = ['First_Name','Last_Name','Title','Email','Phone'];

Laravel Passing Data From One View to Another View

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.

Categories