e.g. a category that can have a parent_id which is another category
$theContract = DB::table('contract_tb')->leftJoin('book_tb', 'contract_tb.book_id', '=', 'book_tb.book_id')
->where('contract_tb.book_id','=',$theBooks[0]->book_id)
->where(function ($query) {
$query->where('contract_tb.start_date','<=',date("Y-m-d"))
->where('contract_tb.end_date','>=',date("Y-m-d"));
})
->where(function ($query) {
$query->where('contract_tb.big_clean_month','like','%'.date("F").'%')
->orWhere('contract_tb.small_clean_month','like','%'.date("F").'%');
})
->first();
$input = Input::all();
$input['my_checkbox' = Input::get('my_checkbox', 0);
and in the view:
<!-- parameters are: name, value, whether it's checked -->
{{ Input::checkbox('my_checkbox', '1', $item->is_enabled) }}
instead pass the input params to call in the third parameter like so:
$this->call('GET', 'my/url/here', $params);
Controller code:
$imprints_list = Imprint::get(array('id', 'title'));
$imprints[''] = 'Select';
foreach ($imprints_list as $imprint) {
$imprints[$imprint->id] = $imprint->title;
}
// Bind the array var to the view
return View::make('products.edit')
->with('imprint_array', $imprints)
View (Blade) code...
{{ Form::label('imprint_id', 'Imprint:') }}
{{ Form::select('imprint_id', $imprint_array, $item->imprint_id) }}
if(App::environment() !== 'local')
{
//
}
// in the controller
$my_select_items = MyModel::all()->lists('title', 'id');
// then pass it to the view
// then in the view
{{ Form::select('select_name', $my_select_items) }}
<div class="control-group">
{{ Form::label('data[package][filtered_items]', 'Item Blacklist:', ['class' => 'control-label']) }}
<div class="controls">
<select multiple="multiple" name="data[package][filtered_items]" class="chzn-select">
@foreach($items as $item)
{{-- if package template is set, select this option *if* it's currently blacklisted for this package template --}}
<option value="{{ $item->id }}" {{ isset($package) ? ($package->filteredItems->contains($item->id) ? 'selected="selected"' : '') : '' }} >
{{ $item->title }}
</option>
@endforeach
</select>
</div><!--controls-->
</div><!--control-group-->
#Laravel Accessors and Mutators
/**
* sets the EffectiveDate into Y-m-d format
*
* @param datetime
*/
public function setEffectiveDateAttribute($effective_date)
{
if ($effective_date) {
$this->attributes['effective_date'] = date('Y-m-d', (strtotime($effective_date)));
} else {
$this->attributes['effective_date'] = null;
}
}
/**
* returns the EffectiveDate into m/d/Y format
*
* @param datetime
*/
public function getEffectiveDateAttribute()
{
$tmpdate = $this->attributes['effective_date'];
if ($tmpdate == "0000-00-00" || $tmpdate == "") {
return "";
} else {
return date('m/d/Y', strtotime($tmpdate));
}
}
in the Dog model
class Dog extends Eloquent()
{
public function owner()
{
return $this->belongsTo('OwnerNamespace\OwnerEloquentModel');
}
}
the foreign key field owner_id is defined in the dogs_table table.
owners_table | ||
---|---|---|
id | dog_id (foreign key) | title, etc. |
dogs_table | |
---|---|
id | title, etc. |
in the Employee model
class Employee extends Eloquent
{
public function employer()
{
// tracking happens in the pivot table
return $this->belongsToMany('EmployerNamespace\EmployerEloquentModel');
}
}
in the Employer model
class Employer extends Eloquent
{
public function Employee()
{
// tracking happens in the pivot table
return $this->belongsToMany('EmployeeNamespace\EmployeeEloquentModel');
// You may override the conventional pivot *table_name* and foreign keys as follows
return $this->belongsToMany(
'EmployerNamespace\EmployerEloquentModel',
'employer_table_employee_table',
'employer_name',
'employee_name'
);
}
}
means that the foreign key fields employer_id and employee_id are defined in the employers_table_employees_table pivot table.
employees_table | |
---|---|
id | title, etc. |
employers_table_employees_table (pivot table) | ||
---|---|---|
id | employee_id (foreign key) | employer_id (foreign key) |
employers_table | |
---|---|
id | title, etc. |
in the Owner model
class Owner extends Eloquent
{
public function dog()
{
return $this->hasOne('DogNamespace\DogTableEloquentModel');
// or
return $this->hasMany('DogNamespace\DogTableEloquentModel');
// pass a second parameter if the foreign key is not an ID
reutrn $this->hasMany('DogNamespace\DogTableEloquentModel', 'owner_name');
}
}
the foreign key field owner_id is defined in the dogs_table table.
What's the difference? hasOne()
returns an individual model, hasMany()
returns a collection.
owners_table | |
---|---|
id | title, etc. |
dogs_table | ||
---|---|---|
id | owner_id (foreign key) | title, etc. |