Input::hasFile() not working - php

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

Related

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. Passing arguments through delete request

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");
}
}

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.

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