i have table with field user_id (unique). i want set manually set it.but when i set it,JTable not insert data.
my $data array is:
$data['user_id']=500;
$data['name']='test';
$data['lastname']='test';
and my code in model is:
$table = $this->getTable();
if ($table->save($data) === true) {
return $table->user_id;}
and my table file is:
class UserproTableuser extends JTable
{
public function __construct(&$db)
{
parent::__construct('#__userpro_users', 'user_id', $db);
}
}
Try this:
$table->bind($data);
$table->save();
Related
I'm trying to fetch some data from a table called "category", but laravel is throwing the
"SQLSTATE[42S02]: Base table or view not found" error.
I've tried with other table names like "company" and it works perfectly. Both of these tables exist but one of them can not be found with the same code.
This throws the error:
public static function getCategories()
{
$categories = [];
$cat = DB::table('category')->get();
if (isset($cat)){
foreach ($cat as $category_name){
array_push($categories, $category_name);
}
return json_encode($categories);
}
return null;
}
This works as expected (same code except for the table name string):
public static function getCategories()
{
$categories = [];
$cat = DB::table('company')->get(); //table name changed
if (isset($cat)){
foreach ($cat as $category_name){
array_push($categories, $category_name);
}
return json_encode($categories);
}
return null;
}
The only difference between those two tables is table collation:
company: utf8_general_ci
category: utf8mb4_swedish_ci
Create a model if it you don't have one. Add the following into your category model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'category';
}
Controller
public static function getCategories()
{
$categories = [];
$cat = Category::all();
if (isset($cat)){
foreach ($cat as $category_name){
array_push($categories, $category_name);
}
return json_encode($categories);
}
return null;
}
You're currently using Query Builder, try use eloquent way.
Ok, I found out we had two separate database-servers with almost identical data in them. Both had databases and tables with the same names, but one was missing the category table.
Of course I was connected to the wrong server from laravel after all.
TL;DR: Simple user error in configuration.
I am working on a roll format for a laravel project.
I have successfully created the roll, and can update the roll status in the roll table which looks like this
|id|roll_id|member_id|status|created_at|updated_at|
One of my status is "Present/Voucher" using funds off a Voucher
I have the Voucher Table
|id|member_id|voucher|date|balance|created_at|updated_at|
When a user clicks on the present/voucher icon in the roll, the following is called in the controller
public function voucher($id)
{
$r = Roll::find($id);
if ($r != null)
{
$r->status = "V";
$r->save();
return redirect(action('RollController#index'))->with ('success', 'Member Paid with Vocuher');
}
return redirect(action('RollController#index'));
}
This works perfect, what I would like to know is how to insert a record into my Voucher table, while I can find the id record in the roll table, I need to pull that member_id so I can add this into the voucher table
For example if the Roll Record looks like this
|id|roll_id|member_id|Status|
|20|2|13|V|
I need to grab 13 are the Member_ID from roll record 20, so I can insert in the vouchers table
|id|member_id|voucher|date|balance
|2|13|Weekly Fees|currdate|-10
Please note the 10 is also stored in a settings table, so if I can grab from that table that would be great
Settings Table:
|id|Setting|Value|
|1|weekly Subs|10|
Thanks
You could first define model relationship.
Member
class Member extends Model {
public function vouchers()
{
return $this->hasMany('App\Voucher');
}
}
Roll
class Roll extends Model {
public function member()
{
return $this->belongsTo('App\Member');
}
}
Voucher
class Voucher extends Model {
public function member()
{
return $this->belongsTo('App\Member');
}
public function setting()
{
return $this->belongsTo('App\Setting');
}
}
Then you can execute fluently in controller, you should also use lockForUpdate and DB::transaction
public function voucher($id)
{
DB::transaction(function () use ($id) {
$roll = Roll::lockForUpdate()->find($id);
if ($roll != null) {
$roll->status = "V";
$roll->save();
$voucher = new Voucher();
$voucher->member_id = $roll->member_id;
...
// Correct me if I am wrong, form your table structure it looks like you linked table voucher and setting with 'value'
// I strongly recommend you to have setting_id as foreign key in table voucher
// So you could do
// $setting = Setting::where('Setting', 'weekly Subs')->where('Value', 10)->first();
// $voucher->setting_id = $setting->id;
$voucher->save();
return redirect(action('RollController#index'))->with ('success', 'Member Paid with Vocuher');
}
}
return redirect(action('RollController#index'));
}
I have relation contain pivot table and when I run code appear this problem
Column not found: 1054 Unknown column 'guest_show.show_date' in 'where clause' (SQL: select * from guests where guest_fname is null and guest_show.show_date between 05/20/2018 and 05/20/2018).
what is solve?
This is my model and function
class Guest extends Model
{
protected $primaryKey = 'guest_id';
protected $table = 'guests';
protected $fillable =
['guest_fname','guest_lname','profession','mobile','work_phone',
'current_job','previous_job','work_address','DOB','DD'];
public function programs()
{
return $this->belongsToMany(Program::class, 'guest_show', 'guest_id',
'program_id')
->withPivot('subject', 'show_date');
}
public function specialties()
{
return $this->belongsToMany(Specialization::class, 'guest_speciality',
'guest_id', 'speciality_id');
}
public static function filter()
{
// Search for a guest based on their name.
$guests=Guest::with('programs', 'specialties');
if (request()->has('guest_fname')) {
$guests->where('guest_fname', request()->input('guest_fname'));
}
// Search for a guest based on their programs.
if (request()->has('program_name')) {
$guests->where('program_name', request()->input('program_name'));
}
// Search for a guest based on their specialties.
if (request()->has('specialty_name')) {
$guests->where('specialty_name', request()-
>input('specialty_name'));
}
// Search for a guest based on their date.
$datefrom=request()->input('from');
$dateto = request()->input('to');
if (request()->has('from') || request()->has('to')) {
$guests->whereBetween('guest_show.show_date', [$datefrom, $dateto]);
}
return $guests->get();
}
Try in closure.
$guests=Guest::with('programs' => function($query) use ($datefrom,$dateto){
$query->whereBetween('guest_show.show_date', [$datefrom, $dateto]); })
, 'specialties');
P.S : I modified only the where between method.
I have 2 related tables and I need to filter the date of a table by month
My first table have the following fields: Id_agendamien_persona , Id_agendamiento, id_persona
The second table have the following fields: Id_agendamiento, fecha
so I need to filter fecha in the second table,
Mi controller
public function listar(){
$contactos = Contacto::pluck('completo','id_contacto');
$contacto_x_agendamientos = Contacto_x_Agendamiento::all();
array_add($contactos,0,"::Seleccione::");
return view('agendamiento.listar')->with(compact('contacto_x_agendamientos','contactos'));
}
My model
first table
class Contacto_x_Agendamiento extends Model
{
protected $table = "contacto_x_agendamiento";
protected $primaryKey = 'id_contacto_x_agendamiento';
const CREATED_AT = 'fecha_creado';
const UPDATED_AT = 'fecha_modificado';
public function Agendamiento(){
return $this->hasOne(Agendamiento::class,'id_agendamiento','id_agendamiento');
}
}
second table
class Agendamiento extends Model
{
protected $table = "agendamiento";
protected $primaryKey = 'id_agendamiento';
const CREATED_AT = 'fecha_creado';
const UPDATED_AT = 'fecha_modificado';
public function scopeMes($query){
return $query->whereMonth('fecha_agendar','=',date('m'))->get();
}
}
View
<td>{{$contacto_x_agendamiento->Agendamiento->Mes()->fecha_agendar}}</td>
error
Property [fecha_agendar] does not exist on this collection instance.
The issue here, is that you're trying to fetch the property of an Agendamiento model from a Collection that you're getting back as a result.
Here you have two choices. You either get just one model, by using ->first():
echo $contacto_x_agendamiento->Agendamiento->Mes()->first()->fecha_agendar;
Or you iterate the Collection and print each fecha_agendar property:
foreach ($contacto_x_agendamiento->Agendamiento->Mes()->get() as $agendamiento) {
echo $agendamiento->fecha_agendar;
}
On a side note, you should totally rethink your coding style/conventions. I'm fully aware it works, but having id_contacto_x_agendamiento as a primary id or a class name like Contacto_x_Agendamiento isn't best practice.
Having a mix of English and Spanish in classes/properties/variables could also be avoided, to improve other people's readability.
[i want to delete data from two table using two models but cant do . am using following code .help me to fix this problem]
public function delete_all_info_employee($id) {
// ************* Delete into Employee Table
$this->employee_model->_table_name = "tbl_employee"; // table name
$this->employee_model->_primary_key = "employee_id"; // $id
$this->employee_model->delete($id);
$this->expense_model->_table_name = "tbl_expense"; // table name
$this->expense_model->_primary_key = "expense_id"; // $id
$this->expense_model->delete($id);
$type = "success";}
1st check you already loaded database in codeigniter\application\config\autoload.php
$autoload['libraries'] = array('database');
your method of model
public function delete_all_info_employee($id) {
// ************* Delete into Employee Table
$this->db->where('employee_id', $id);
$this->db->delete('tbl_employee');
$this->db->where('expense_id', $id);
$this->db->delete('tbl_expense');
return "success";
}