I'm writing a few cods around Laravel's form builder.
Here is my Controller:
public function create()
{
$departments = Department::all('name');
return view('door.project.create') ->with('departments',$departments);
}
On the create.blade.php, I get stuck.
The selectbox's option would show out with JSON array,
like:
{"name": "Sale"}
And the select box codes would be down here:
<div class="form-group">
{!! Form::Label('deparment_name', 'Department:') !!}
{!! Form::select('deparment_name', $departments, null, ['mutiple' => 'multiple']) !!}
</div>
May I ask how do I fix it?
Instead of:
$departments = Department::all('name');
you should use:
$departments = Department::pluck('name','id');
to get in select valid list.
Related
Hi I'm using laravel forms.
{!! Form::text('product_name', null, array('class' => 'form-control date_pick')) !!}
Is there a way to set this default input from controller?. Think we can do it with Flash but I couldn't find an example. I want to take vlues from a model and prepopulate.
$products = Products::all();
It would be great if someone know how to do this. What is the easiest way to do it?
This is done with a basic edit route where you simply use:
In ModelController you would call the edit page:
public function edit(ModelName $model) {
return view('name.of.the.blade.view', compact('model')); // if using compact then without dollar symbol
}
In blade view simply make the form with all the input fields you have for that model:
{!! Form::model($model, ['method' => 'PATCH', 'route' => ['model.update', $model->id],]) !!}
Now all the form fields will have the values from that model.
{!! Form::close() !!}
And the edit and update route (inside routes/web.php) would be like this:
Route::get('/model/{model}/edit', 'ModelController#edit')->name('model.edit');
Route::patch('/model/{model}', 'ModelController#update')->name('model.update');
I'm looking to fetch unique values from a table in my database and pass the company name into my view as options in a drop down list within a form.
The function in my controller looks like this:
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->select('company')->get()->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
And my view looks like this:
<div class="form-group">
#foreach ($companies as $company)
{{ Form::label('Select Company')}}
{{ Form::select('companies', $company->company, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
#endforeach
</div>
I'm getting the following error:
Invalid argument supplied for foreach()
If I try {{dd($companies}} I can see an array is being passed through to the view OK. This array looks like this:
array:353[
0 => {#274 ▼
+"company": "Example company name"
}......
]
Ditching the loop and reverting back to:
<div class="form-group">
{{ Form::label('Select Company')}}
{{ Form::select('companies', $companies, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
</div>
causes another error to be thrown which states:
htmlspecialchars() expects parameter 1 to be string, object given
Where am I going wrong here?
In your controller
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->pluck('company')->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
Fllow as per below
your index function must be like below
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->pluck('company')->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
your view must me like below
<div class="form-group">
{{ Form::label('Select Company')}}
{{ Form::select('companies', $companies, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
</div>
let me know if any
YOu can create a select Box like this:
{{ Form::select('age', ['Under 18', '19 to 30', 'Over 30']) }}
Lets say I want to have a select box of all my users (saved in the DB). How can I do that? I've got a User-Model.
In your controller
use App\Entities\User;
public function getUsers(User $user)
{
$allUsers = $user->lists('name', 'id');
return view('users.view',compact('allUsers'));
}
In your blade
{!! Form::select('users[]', $allUsers,null , ['multiple'=>'multiple', 'class' => 'form-control']) !!}
I'm beginner in Laravel and web development and I have a silly question, I'm building a system that lists the user financial transactions. In the homepage is listed all the transactions made by the user. I put two fields up there to make the date filter of the transactions, but I don't know how to proceed this filter in Laravel.
I'm using datepicker and it is working fine, I also know that the validation system allows me to use Laravel date: after and date: before, but I don't know how and in which method in the controller send these dates. Here's my dates form code:
{!! Form::open(['route' => 'transactions.index']) !!}
{!! Form::label('data_inicio', 'De: ') !!}
{!! Form::input('date', 'data_inicio', null, ['class' => 'datepicker', 'data-date-format' => 'dd/mm/yy']) !!}
{!! Form::label('data_fim', 'Até: ') !!}
{!! Form::input('date', 'data_fim', null, ['class' => 'datepicker', 'data-date-format' => 'dd/mm/yy']) !!}
{!! Form::submit('Enviar') !!}
{!! Form::close() !!}
The method in the controller which calls the view of the homepage is the index, here's the index code:
public function index()
{
$transactions = Auth::user()->transactions;
return view('transactions.index', ['transactions' => $transactions]);
}
Here is how I show the data in the view:
#foreach( $transactions as $transaction )
<tr>
<td>{!! date('d-m-Y', strtotime($transaction->created_at)) !!}</td>
<td>{!! $transaction->title !!}</td>
<td>{!! $transaction->amount !!}</td>
</tr>
#endforeach
I need to send these dates I got to this same method (index) when the Pesquisar button was clicked to be able to seek in the database only the transactions of the proposed date, the problem is I do not know do this.
I tried to send these dates as parameter request to the index method, modifying it like this:
public function index(Request $request)
I got a Column Not Found error when I click in the submit, but I'm not using this request in any query in the method.
I really think that it has a simple way to do it and I appreciate any help!!
Here's my model class:
class Transaction extends Model {
protected $table = 'transactions';
protected $guarded = [];
public function users()
{
return $this->belongsTo('App\User');
}
}
Try this to obtain the dates in your controller:
$data_inicio = Input::get('data_inicio');
$data_fim = Input::get('data_fim');
Then you can use the dates to query your Transaction relation (assuming this a one to many relationship and is configured propperly)
$transactions = Transaction::whereBetween('created_at',[$data_inicio, $data_fim])->where('user_id',Auth::id())->get();
I have a simple database application that records contacts (name, address, etc.). I am trying to draw on various other tables to generate value lists for the forms. Then I would like to have all form values entered by the user to be saved into the appropriate tables on submit.
For example, I have the people table which includes the name and address. I have a title table with the different possible values for the title (i.e. Mr., Mrs., Ms. Dr.). Upon filling in the contact form, I would like to generate the values for the title field in the form from the title table. I am trying to do this one model for Contact that includes separate classes for the people table and the title table.
In my ContactController.php I have:
class ContactController extends Controller {
public function index()
{
$people = Contact::all();
// return main homepage for Contacts section
return view('pages.contacts.home', compact('people'));
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
$title = Contact::lists('title', 'id');
return view('pages.contacts.create');
}
In my Contact.php model I have the following:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model {
protected $table = 'people';
protected $fillable = [
'title_id',
'name_first',
'name_middle',
'name_last',
'date_birth',
'date_death',
'bio',
'created_at',
'modified_at'
];
public function title() {
return $this->belongsTo('Title', 'title_id', 'id');
}
}
class Title extends Model {
protected $table = 'title';
protected $fillable = [
'title',
'created_at',
'modified_at'
];
public function contacts() {
return $this->hasMany('Contact', 'title_id', 'id');
}
}
In the form I have the following:
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::select('title', $title) !!}
</div>
<div class="form-group">
{!! Form::label('name_first', 'First Name: ') !!}
{!! Form::text('name_first', null, ['class' => 'form-control']) !!}
{!! Form::label('name_middle', 'Middle Name: ') !!}
{!! Form::text('name_middle', null, ['class' => 'form-control']) !!}
{!! Form::label('name_last', 'Last Name: ') !!}
{!! Form::text('name_last', null, ['class' => 'form-control']) !!}
{!! Form::label('name_nick', 'Nickname: ') !!}
{!! Form::text('name_nick', null, ['class' => 'form-control']) !!}
</div>
I am getting an error saying the variable title is undefined. I cannot determine why the value list is not being returned. Are my relationships returned incorrectly?
You don't pass the $title variable to your view.
In your create method in the ContactController, assign the $title variable to your view:
public function create()
{
$title = Contact::lists('title', 'id');
return view('pages.contacts.create')->with('title', $title);
}
See http://laravel.com/docs/5.0/views for more documentation.