php - Laravel Mysql Table Joins -
i have 2 tables in database, 1 called ticket , 1 called status(status of ticket new or completed)...every time user adds ticket want status table updated ticket id , status message.
is way mysql table joins?
public function usersubmitticket() { $validator = validator::make(input::all(), ['subject' => 'required', 'message' => 'required']); //if 1 or more of text boxes dont contain data, validator fails , user returned backa redo form if ($validator->fails()) { return redirect::back()->withinput()->witherrors($validator->messages()); } $user = auth::user(); $userid = $user->id; $subject = input::get('subject'); $message = input::get('message'); $date = new datetime(); $ticket = ticket::insert(array('id' => null, 'date_in' => $date, 'originator_id' => $userid, 'assigned_to_id' => 0, 'date_out' => null, 'priority' => 3, 'subject' => $subject)); $ticketid = ticket::select('id')->limit(1)->get(); foreach($ticketid $id){ $status = status::insert(array('id' => $id, 'description' => 'new')); }
this 1 way tried, know doesn't work now.
you may try this:
class ticket extends eloquent { //... protected $guarded = array(); // check mass-assignment public function status() { return $this->hasone('status', 'ticket_id'); } } class status extends eloquent { //... protected $guarded = array(); // check mass-assignment }
in case status
table must contain ticket_id
field foreign key build relationship. so, if alright may use this:
// create new ticket $ticket = ticket::create(array(...)); // create new status $status = status::create(array('description' => 'new')); // save relationship $ticket->status()->save($status);
in case, i've assumed that, both of tables/models have primary key
set id
field.
Comments
Post a Comment