Laravel - insert data into a table that contains id of other tables - sql

i have a table in my database( table case ) that contains case type id which is a foreign key to the table (case_type). when users fill this table they dont know the case type id they only know the category of the case. so what i want to do is when users fill the case table they write the case type category and based on that it match the category with the case id since i have a separate table and submit that id in to the case table.
so far what i have tried :
from vue i send the category selected from a dropdown by the user to this controller function
public function getIdOfCategoryByName(Request $request){
$name = $request->input('name');
$getidbyname=case_type::where('category',$name)->get('id');
return $getidbyname;
}
this is where i dont know how to insert the id that i got back to this function
public function createcase(){
$request->validate([
'title' => 'required',
'description' => 'required',
'caseTypeId' => 'required',
'amountreceived' => 'required',
'amoutneeded' =>'required',
'uid' => 'required',
'visible' => 'required',
'image' => 'required',
'cityId' => 'required'
]);
waitingdonations::create($request->post());
return response()->json([
'message' => 'waiting donation successfull',
]);
}
if my question is unclear please check this website https://towardsdatascience.com/sql-insert-values-with-joined-ids-from-another-table-83ff7f149296

Related

I want to show the user name that user has booked to slot using laravel

I want to show user name that user has booked to slot on calender and i have already made relation how can i show username please help me thanks.
Booking Model
public function users(){
return $this->hasOne('App\Models\User','id','user_id');
}
Controller
public function getBookingSlot(Request $request){
$userBookings = Booking::with('users')->where('room_id',$request->room_id)->where('booking_holding_status',1)->get();
foreach($userBookings as $booking){
$events [] = [
'id' => $booking->id,
'calendarId' => $booking->id,
'title' => 'user name',
'category' => 'time',
'dueDateClass' => '',
'bgColor' => "#e6515f",
'color' => "#ffffff",
'isPending' => 'false',
'isFocused' =>'false',
'start' => $booking->start_datetime,
'end' => $booking->end_datetime,
];
}
return \Response::json([
'events' => $events ?? null
]);
}
$booking->users[0]->username;
booking object contains booking details in it along with the object of users which it selects from the user table... I am not sure about hasOne() relationship. I have used it with belongsToMany().
so you get the user object from the booking object than for the first user [0] get its username.
If this does not work then the booking object may have only 1 user object, not an array of objects.. then access it like this...
$booking->users->username;

Pardot API: Add new prospect to certain list

$prospectData = array(
'user_key' => $user_key,
'api_key' => $api_key,
'first_name' => $firstName,
'last_name' => $lastName,
'city' => $city,
'state' => $state,
'comments' => $comments
);
callPardotApi('https://pi.pardot.com/api/prospect/version/4/do/create/email/'.$email, $prospectData);
I'm able to create a new prospect with a form that I have. It inserts all the data I supplied it (name, city, state, etc), but I need to also add this prospect to a list.
I tried adding to my $prospectData things like list => '1234' or "list_id" => '1234', but that doesn't seem to be working.
Is this possible to do? I know I can assign a prospect to a list via another api route using their ID, but I need this prospect to be added immediately upon form submit
Well it's not exactly ideal, but I had to do a new api call after creating the user.
$addprospect = callPardotApi('https://pi.pardot.com/api/prospect/version/4/do/create/email/'.$email, $prospectData);
$addprospectxml = new SimpleXMLElement($addprospect);
$id = $addprospectxml->prospect->id;
$listData = array(
'user_key' => $user_key,
'api_key' => $api_key,
'list_32106' => "1"
);
$updateProspect = callPardotApi('https://pi.pardot.com/api/prospect/version/4/do/update/id/'.(String)$id[0], $listData);
When creating a prospect, it will return XML with the newly crated prospect's ID. that ID can be used in a new api update call where you can set the list.

how to save id of one table to another table in codeigniter?

i want to save id of one table to another table how should i write the model and controller.
For example : I have a database like this
tbl1: (id,content,time,date,category)
tbl2: (id,tbl1_id,category_detail)
$tbl1Data = array(
'id' => '',
'content' => 'Hi, content will be here',
'time' => date("H:i:s"),
'date' => date("Y-m-d"),
'category' => 'Electronics'
);
$this->db->insert('tbl1',$postData);
$recordId $this->db->insert_id();
$tbl2Data = array(
'id' => '',
'tbl1_id' => $recordId,
'category_detail' => "alskjdflkajsdlk falsjdflka lsdkfj as",
);
$this->db->insert('tbl1',$postData);

Yii1 CGridView - SUM of Custom Column Value in footer

I want Sum of a custom column value in footer
GridView Code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'booking-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'name'=>'name',
'header'=>'User Name',
'value'=>'$data->booking_id',
'filter'=>false,
'footer'=>'Total',
'footerHtmlOptions'=>array('class'=>'grid-footer'),
),
array(
'name'=>'id',
'header'=>'User Fee',
'value'=> array($model,'GetUserFee'),
'filter'=>false,
'footer'=>'' . $model->getTotal($model->search()->getData(), 'id'),
'footerHtmlOptions'=>array('class'=>'grid-footer'),
),
),
));
Get Total
public function getTotal($records,$colName){
$total = 0.0;
if(count($records) > 0){
foreach ($records as $record) {
$total += $record->$colName;
}
}
return number_format($total,2);
}
Second column value are calculate user fee.
In footer sum of User Fee values get the wrong sum. It gives the sum of user ids. and id is table column name.
How can I get the sum of User fee column values
Instead of using a literal representation of id, try using the $data variable created by CGridView, which actually IS the current record from the $model currently being processed in each CGridView iteration.
Your code would then be:
array(
'name'=>'id',
'header' => 'User Fee',
'type' => 'raw',
'value' => '$data->id',
'filter' => false,
'footer' => '$model->getTotal($data)',
'footerHtmlOptions' => array('class'=>'grid-footer'),
),
Notice the value of the type attribute is set to raw.
You can even use a PHP function and pass it the $data variable, like so:
array(
'name'=>'id',
'header' => 'User Fee',
'type' => 'raw',
'value' => function ($data) { ... handle $data how you like ... }
'filter' => false,
'footer' => '$model->getTotal($data)',
'footerHtmlOptions' => array('class'=>'grid-footer'),
),
For more information check out special variables in Yii

how I can display information on the Cgridview according to the logged user in yii?

I have this:
State:
id
data1
Company:
id
data1
state_fk
Planning
id
data1
company_fk
users roles in the system will be the name of a state,what I want is, according to the user roles, when he accesses to GridView Planning index i need that he only see whose having Companies whose states match with the user role. sorry for my English and thanks
Try this,
Add this in cgrid,
array (
'header' => 'Companies',
'value' => '$data->searchcompanies',
'type' => 'html', ),
and add this function in planning model,
public function getsearchcompanies(){
$companies = Companies::model()->findAllByAttributes(array('state_id'=>$stateid));
foreach($companies as $company){
$companynames[]=$company->company_name;
}
$regionname=implode('<br/>',$regionnames) ;
return $regionname;
}
you can use 'visible' in CGridView widget :
array(
'name' => 'company',
'value' => '$data->searchcompanies',
'visible' => Yii::app()->user->isAdmin,
),