I am creating a code by which i can replicate the multiple rows of table and its related tables.
I am able to replicate parent table successfully, but while replicating the contents of child table it is modifying the values which i am unable to figure it out.
my controller code is
public function copyshowtime($cinema_id,$show_date)
{
$date=new Carbon($show_date);
$current_show_date=$date->format('Y-m-d');
$next_show_date=$date->addDay()->format('Y-m-d');
$movieshowtime=Movies_showtimes::with('showdata')->where([['cinema_id','=',$cinema_id],['show_date','=',$current_show_date]])->get();
foreach ($movieshowtime as $item)
{
$item->show_date=$next_show_date;
$item->show_id=NULL;
$newshowtime=$item->replicate();
$newshowtime->push();
foreach ($item->showdata as $sd)
{
$newshowdata = array(
'showdata_id' => NULL,
'show_id'=>$newshowtime->id,
'category_id'=>$sd->category_id,
'showdata_category'=>$sd->showdata_category,
'showdata_rate'=>$sd->showdata_rate
);
// print_r($newshowdata);
Movies_showdata::create($newshowdata);
}
}
}
When I am printing the complete array its showing proper data which i am trying to insert ,check below
But after inserting it into table I see this data in table, the values of show_id and showdata_rate is getting zero value and other columns getting correct data inserted.
I am unable to figure out this problem as I am nowhere changing the data to zero before inserting it into table.
Make sure show_id and showdata_rate are fillable in your Movies_showdata model:
class Movies_showdata extends Model {
protected $fillable = ['show_id', 'showdata_rate', ...other fillable fields ];
}
Related
I am facing issue regarding storing ids and display their values from two different table i have 1 table business_master and other table is page_master i have combine business_name column with page_url column which are available on page_master table if i add my business and page first time its successfully combine these two values as one business can have many pages and if i only add page 2nd time i am unable to see business_url with page_url column.
My Page Model:
class PageList extends Model
{
protected $table = 'page_master';
protected $fillable = ['business_id', 'page_url', 'page_name'];
public function business()
{
return $this->hasOne('App\Business','business_id');
}
}
and in my view:
<td>{{optional($value->business)->business_url}}.spikesales.io/{{$value->page_url}}</td>
This is my first time out if i add business_url and page_url
hussain.spikesales.io/house
and then if i add only page the out put is something like that
.spikesales.io/hello
one business can have many page it should attach business_ url also but i am unable to find solution:
Any help will be highly appreciated!
public function pageListHere()
{
$list = PageList::all();
return view('page-list',compact('list'));
}
You have to change your controller code like this:
$list = PageList::select('*');
$list->with(
array('business'=>function($query){
$query->select('*');
}));
$pageList = $list->get();
return view('page-list',compact('pageList'));
The varialbe $pageList will be having all the rows of that table along with related Business table rows.
I am trying to insert a two related objects via Eloquent and I get an Integrity constraint violation error on one of the tables. Here is what I am trying:
The representation of the relationship:
class DataParticipant extends Model
{
public function data_config()
{
return $this->hasOne('App\Models\DataConfig');
}
}
class DataConfig extends Model
{
public function data_participant()
{
return $this->belongsTo('App\Models\DataParticipant');
}
}
Then, I am trying the following:
$dataParticipant = new DataParticipant();
$dataParticipant->ip = // ip
$dataParticipant->code = // code
$dataParticipant->study_name = // study
// Prepare the config child.
$dataConfig = new DataConfig();
$dataConfig->config = // config via json_encode
// Store the child associated to the parent.
$dataParticipant->data_config()->save($dataConfig);
The error is on data_participant_id column, which belongs to the DataConfig model. My guess is that there is no id on which the two can be linked. I want to ask you:
How can I accomplish this without having to first save the DataParticipant then retrieve it from the database and then store the DataConfig on the resulted id?
First save the first model
$dataParticipant->save ();
Then save the second model
$dataParticipant->data_config()->save($dataConfig);
This won't retrieve the data from DB at all, and $dataParticipant id will be automatically set with the last insert id
I'm really confused why I keep getting a null value when retrieving data from my data model.
Here's my model:
class Supplier extends Eloquent {
public function purchase_orders()
{
return $this->hasMany('Purchase_order','supplier_id');
}
}
And my controller:
$purchase_orders = Supplier::find(1)->purchase_orders;
dd($purchase_orders);
This results to NULL.
My Purchase_order table's fields are:
('id','supplier_id','name','status','date')
And my Suppliers table's fields are:
('id','name','email', 'address')
By the help of #hayhorse I discovered that the one causing the issue was the underscore on the name of the class, to solve the issue i just renamed my class from purchase_orders to purchaseOrder and by that i can now user the proper syntax which is:
$purchase_orders = Supplier::find(1)->purchase_orders;
again credits to #hayhorse
I have to tables with these schema:
users(id, name, email)
user_details (id, user_id, physical_address, otherinfo)
I would like to display all contents of both tables in one grid using grocery crud
when i try to use set relation on the first table as shown: Note: I have reducted the part that does the rendering of the view;
$crud = new grocery_CRUD();
$crud->set_table('users');
$crud->set_relation('id', 'user_details', '{physical_address} + {otherinfo}');
the values of the id field as well as the referenced table don't appear in the grid, so it does not seem to work when using primary keys.
So I decided to start with the contents of the second table like so:
$crud = new grocery_CRUD();
$crud->set_table('user_details');
$crud->set_relation('user_id', 'users', '{name} + {email}');
This works, but the problem is that the values appear in one column in the grid. I would like to know how i can separate them to different columns and make them editable in separate input fields.
my way kinda ridiculous,
if i were really need the field to be separated, i might use callback_column then use active records to return the value using $row->user_id, and callback each record
just like:
return $this->some_model->get_name($row->user_id); for name field
and
return $this->some_model->get_name($row->user_id); for email field
but, as i tested with some dummy records, it can't be sorted, (i dont know if anyone could)
There is no direct way to show multiple columns but i found an work around for this problem.
here is how
set name column as follows.
$crud->columns('name','email'); // define all required columns
$crud->callback_column(unique_field_name('user_id'),array($this,'callback_set_user_name'));
function unique_field_name($field_name)
{
return 's'.substr(md5($field_name),0,8);
}
public function callback_set_user_name($value, $row)
{
$row->full_name = $row->s447d3092; // before getting to know s447d3092 key return json_encode($row); you will key pair with {name} + {email} ex: {"s447d3092":"shiva + shiv#gmail.com"}
return explode("+",$value)[0]; // here you will have only name
}
$crud->callback_column('email',array($this,'callback_set_email'));
public function callback_set_email($value, $row)
{
return explode("+",$row->full_name)[1];
}
I am 100% sure this will help you
Using saveAll() to save multiple records in CakePHP, I am able to save them successfully in a table. But the problem arises while retrieving the IDs of those saved rows. LastInsertID() returns only a single last ID here. How can I get all the last inserted IDs which I have inserted using saveAll()?
afterSave function is called after each individual save in a saveAll execution, so you could do:
In your AppModel
class AppModel extends Model {
var $inserted_ids = array();
function afterSave($created) {
if($created) {
$this->inserted_ids[] = $this->getInsertID();
}
return true;
}
}
You can place this code into any model and it should work fine. Then to return the IDs after the saveAll in your controller, you would do so like this:
if($this->Post->saveAll($posts)) {
$post_ids=$this->Post->inserted_ids; //contains insert_ids
}
Hope it helps