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;
}
Related
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
I'm using LaravelCollective form to delete an employee:
{!! Form::open(['method' => 'DELETE', 'action' => ['EmployeesController#destroy', $employee->id, $company->id]]) !!}
<div class="form-group">
{!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}
and want to pass 2 arguments: employee id and company id.
My route:
Route::delete('/employees/{employee}/{company}', 'EmployeesController#destroy');
My controller function:
public function destroy($id, $companyId)
{
Employee::find($id)->delete();
if($companyId == 0)
return redirect('/employees');
else
return redirect('/companies/' . $companyId . "/edit");
}
I'm getting an error that I'm passing only 1 parameter. Where is the problem?
Try this instead:
// named route
Route::delete('employees/{employee_id}/{catetory_id}', 'EmployeesController#destroy')->name('employees.destroy');
// form using named route
{!! Form::open(['method' => 'DELETE', 'route' => ['employees.destroy', $employee->id, $company->id]]) !!}
<div class="form-group">
{!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}
You have to send the params in an array, try something like this.
{!! Form::open(['method' => 'DELETE', 'action' => ['EmployeesController#destroy', [$employee->id, $company->id] ]]) !!}
<div class="form-group">
{!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}
The variable name should match the route parameter. Try something like this in your controller:
public function destroy($employee, $company)
{
Employee::find($employee)->delete();
if($company == 0)
return redirect('/employees');
else
return redirect('/companies/' . $company . "/edit");
}
}
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 )
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
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.