I have a select dropdown using laravel form helper like so:
{!! Form::label('job_id', 'Job Titlle', ['class' => ' control-label']) !!}
{!! Form::select('job_id', $jobs, null, ['class' => 'form-control', 'placeholder' => 'Select']) !!}
The problem is this causes a foreign key constraint when I try to update the model
. I think an empty string is passed and I don't lnow how to stop that from happening.
please help Thanks!
1) Validate the data before sending it off to the database; something like this in your controller will do the trick:
class JobController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'job_id' => 'required'
]);
// Store in the database because data is valid
}
}
2) If job_id is not really required, you can avoid the foreign key contraint failure by specifying the column as nullable
In Database table make column null
ALTER mytable MODIFY mycolumn varchar(255) null;
This did the trick for me.
if(isset($request['fk_id']) && empty($request['fk_id'])) {
$request['fk_id'] = null;
}
Related
I have a Problem with relations in Laravel / with Backpack for Laravel.
This program is for creating menues for my dad´s restaurant.
I have these tables:
- dishes (Table with the Names of the Dishes)
-- id (auto inc)
-- menuname (Name of the Dish)
- weeklymenues
-- id (auto inc)
-- start_date (Monday of the selected week)
-- end_date (Friday of the selected week)
-- menu_monday (There should be the id of the dish)
-- menu_tuesday (...)
-- menu_wednesday (...)
.....
How can i do that correctly?
In the CRUD Controller i am setting the Field:
$this->crud->addField([
'label' => "Monday",
'type' => 'select2',
'name' => 'menu_mondy', // the db column for the foreign key
'entity' => 'menu', // the method that defines the relationship in your Model
'attribute' => 'menuname', // foreign key attribute that is shown to user
'model' => "App\Models\Menu" // foreign key model
]);
And in the menues model i have set this relation:
public function menu() {
return $this->belongsTo('\App\Models\Menu');
}
Everytime I want to save the CRUD, the program wants to save something in the dishes table:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'dish_id' in 'field list' (SQL: update `weeklymenues` set `dish_id` = 1, `weeklymenues`.`updated_at` = 2019-06-25 14:13:14 where `id` = 15)
What am I doing wrong? How can I set the relations correct?
Thanks in advance!!
Are "dishes" (from the table definition) and "Menu" (from the code) actually the same thing?
In your field definition, you set the model to be the "menu" class, shouldn't it be the "dishes" class?
I would have used the relationship type
CRUD::addField([
'label' => 'Monday',
'type' => 'relationship',
'name' => 'menu', // the method that defines the relationship in your Model
'attribute' => 'menuname', // foreign key attribute that is shown to user
'model' => \App\Models\dishes::class, // foreign key model
'placeholder' => 'Select a dish for Monday'
]);
---
-c:\xampp\htdocs\bpwebsite\app\Models\weeklymenues.php
---
protected $fillable = ['start_date', 'end_date', 'menu_monday']
---
I am using dropdown menu to select the site as:
{{ Form::select('site', $sites, null, ['class' => 'form-control', 'placeholder' => 'Select a Site']) }}
{{ Form::label('site','site*') }}
I am storing the selected site in the database as site_id.
As this returns the index of the selected item from the list, so if the first item is selected the site_id would be stored as 0 in the database.
The problem here is, site_id is a foreign_key in my database and this is causing errors while matching it with the column:id of the sites table. As the id column generated by laravel migration scaffolding starts from 1 and the site_id returned by Form::select starts from 0.
Is there anyway that the index returned from Form::select would start from 1?
Or is there any other way to solve the problem?
When you're getting the sites from DB, keep original IDs to solve the problem:
$sites = Site::pluck('name', 'id');
This code will generate a list of sites' names with real IDs:
[1 => 'Site name 1', 2 => 'Site name 2']
You have many solutions
First, just verify with the validation, site must not be 0 since your controller
use Illuminate\Validation\Rule;
/** ... */
$this->validate($request, [
'site'=>['required',Rule::notIn([0])]
])
You can also check where you save Site model :
DB::transaction(function() use($request){
/** mapping of site attribute */
if($site->save())
{
}
}
You can also check since client side via JavaScript
{{ Form::select('site', $sites, null, ['id'=>'sites', 'class' => 'form-control', 'placeholder' => 'Select a Site']) }}
{{ Form::label('site','site*') }}
$("form input['type=submit']").click(function(e){
e.preventDefault();
if($('#sites').val() == 0)
{
alert('Please, select a site');
return;
}else{
$(this).submit();
}
})
if you want to inset name without id you can use $sites = Site::pluck('name', 'name'); I hope this will work.
I'm trying to set more than one attribute in a select2 type field.
For Example, I would like to show first_name and last_name as the label of my select values:
$this->crud->addField([ // Select
'label' => 'Employer',
'type' => 'select2',
'name' => 'employer_id', // the db column for the foreign key
'entity' => 'employer', // the method that defines the relationship in your Model
'attribute' => 'first_name', // foreign key attribute that is shown to user
'model' => 'App\Models\Employer' // foreign key model
], 'update/create/both');
Is there any suggestion?
Thanks.
You can with a getter:
public function getIdentNameAttribute()
{
return "{$this->ident} {$this->name}";
}
Then in your controller use:
"attribute" => "IdentName", // foreign key attribute that is shown to user
I have a select box with an array of data to populate it like this:
{!! Form::select('language',$languageArray,'null', ['id'=>'language','multiple',]) !!}
I am passing $languageArray with view , and is simply an array of values like ['A','B','C']...
Now while fetching the selected values i am getting numeric value of selected options. Is there a way to change the values from numeric indexes to Text written in option. i did it using an associative array as second argument like this:
['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..']
But it creates a long list and view seems to be overloaded with data is there a better way for achieving below output ???
<select name="language">
<option value="english" ">English</option>
<option value="french">French</option>
....
I suggest you to use Config values ,
Create a file like languages.php in config folder.
<?php
return ['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..'
'german' => Lang::get('languages.german')
];
View :
{!! Form::select('language',Config::get('languages'),'null', ['id'=>'language','multiple',]) !!}
As you can see, in this way you can use config values in another view too and support multi language(its important too.. look at 'german')
Last Option is creating your own Form macro for example,
Form::macro('slugiySelect', function($name , $list , $selected = null , $options = [])
{
$options = [];
foreach($list as $row)
$options[Str::slug($row)] = $row;
return Form::select($name , $options , $selected , $options);
});
In this macro your options array is slugify with Laravel string function and uses as a key , I kept value as yours. You can do it with your way.
i have the following models: Student,Form,FormsStream.
the student model have foreign keys to both models. Student.form_id is a foreignKey to the form table,Student.stream_id is foreign to the FormsStream table. i already have entries for both forms and formsStreams.
to add the student Form and Stream i select both from respective drop-down list. i already created the drop-down list.
my Problem.
previously i was able to save/edit student form and stream but after making those fields foreign keys to be selected i cant save new or edit those fields. i use cakePHP 2.5.4.
however i can do that in SQL.
i cant seem to figure out the problem with the select lists.
my method to get the forms from the database
public function loadStudentForm() {
$this->loadModel('Student'); //load the associated models
$this->loadModel('Form');
$forms = $this->Form->find('list',array('fields' => 'form_name')); //get the forms from the database
$this->set('forms',$forms);
}
method to get streams from database
public function loadStreams() {
$this->loadModel('FormsStream');
$streams = $this->FormsStream->find('list',array('fields' => 'stream_name'));
$this->set('streams',$streams);
}
student add method
public function addStudent() {
if ($this->request->is('post')) {
$this->loadStudentForm();
$this->loadStreams();
$this->Student->create();
if ($this->Student->save($this->request->data)) {
$this->Session->setFlash(__('New student record added to the database.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The student could not be added.Please ensure all fields are correctly entered'));
}
}
}
the extract from add.ctp for selecting student form and stream
echo $this->Form->input('forms',array('label' => 'Form'));
echo $this->Form->input('streams',array('label' => 'Class stream'));
so far printing contents of validateErrors shows this:
array(
'Student' => array(
'form_id' => array(
(int) 0 => 'You must select the form'
),
'stream_id' => array(
(int) 0 => 'You must choose a stream'
)
),
'Form' => array(),
'FormsStream' => array()
)
i've even tried to intercept the request in burpsuite and i can see the form and stream id are passed.just dont get why they are empty in the above array
_method=POST&data%5BStudent%5D%5Badmission_no%5D=1002&data%5BStudent%5D%5Bfirst_name%5D=peter&data%5BStudent%5D%5Bmiddle_name%5D=per&data%5BStudent%5D%5Blast_name%5D=pee&data%5BStudent%5D%5Bgender%5D=Male&data%5BStudent%5D%5Bdate_of_birth%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bdate_of_birth%5D%5Bday%5D=05&data%5BStudent%5D%5Bdate_of_birth%5D%5Byear%5D=2014&data%5BStudent%5D%5Bjoin_date%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bjoin_date%5D%5Bday%5D=05&data%5BStudent%5D%5Bjoin_date%5D%5Byear%5D=2014&data%5BStudent%5D%5Bforms%5D=1&data%5BStudent%5D%5Bsstrong texttreams%5D=1
The input must be form_id and stream_id. Even if you set them with plural form cakephp recognize it in the following form.
echo $this->Form->input('form_id',array('label' => 'Form'));
echo $this->Form->input('stream_id',array('label' => 'Class stream'));