laravel 5.3 $request->submit - php

How to check is form was submitted?
view:
{!! Form::open(['method' => 'GET', 'action' => 'JobController#index']) !!}
<div class="form-group">
{!! Form::label('keywords','Keywords' ) !!}
{!! Form::text('keywords', null, array('class' => 'form-control')) !!}
</div>
{!! Form::submit('submit', array('class' => 'btn btn-primary form-control ')) !!}
{!! Form::close() !!}
Controller:
public function index(Request $request) {
// if(!empty($request->keywords)) {
//
// $search = Post::where("keywords","LIKE","%{$request->keywords}%")
// ->paginate(10);
// }
if(!empty($request->submit)) {
$search = Post::where("keywords","LIKE","%{$request->keywords}%")
->paginate(10);
}
}
$request->keywords is working fine. $request->submit shows nothing.
Is this constuction do not work in Laravel?
Thanks!

You can pass the name and value in the array as:
{!! Form::submit('submit', array('class' => 'btn btn-primary form-control', 'name'=>'submit', 'value'=>'save')) !!}
And use it in controller as:
if($request->submit == 'save') {
}

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

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

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'];

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 5 Populate a form from two different tables

I stumbled upon a problem while creating a CRUD application in Laravel 5. In the edit section of a product I have 2 inputs that are populated from 'products' table and I have a dropdown list that needs to be populated with data from the categories table. The problem is that I don't know how to aproch the dropdown list. I generated the Form::model for the two inputs but I'm stuck at the dropdown list.
Controller
public function edit($id)
{
$products_edit = products::find($id);
return View::make('products.edit')->with('products_edit', $products_edit);
}
View
{!! Form::model($products_edit, array('route' => array('products.update', $products_edit->id), 'method' => 'PUT')) !!}
<div class="form-group">
{!! Form::label('name', 'Nume') !!}
{!! Form::input('text', 'name', $products_edit->product_name, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('price', 'Cost') !!}
{!! Form::input('number', 'price', $products_edit->product_price, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('category', 'Categorie') !!} <br />
{!! Form::select('category', <!-- insert var here -->, array('class' => 'form-control') !!}
</div>
You can do this at your controller:
public function edit($id)
{
$products_edit = Product::findOrFail($id);
$categories = Category::lists('name', 'id');
return view('product.edit', compact('products_edit', 'categories'));
}
In the view you still make the form model bind:
{!! Form::model($products_edit, array('route' => array('products.update', $products_edit->id), 'method' => 'PUT')) !!}
And to create the category dropdown try this:
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
your can do thi controller:
public function edit($id)
{
$products_edit = Product::find($id);
$categories = Category::pluck('name', 'id');
return view('product.edit', compact('products_edit', 'categories'));
}

Categories