how insert the same id to other table? [duplicate] - laravel

This question already has answers here:
How to get last inserted id through save() method in laravel
(6 answers)
Closed 2 years ago.
I have a table and it is like auto increment, I tried to do something like this: in an order table you insert for example your id, product name, value, and delivery address, and I needed it in another table called the order id and insert it in the order details.
using this method
$order = new order;
$order->id_user = 1; //$iduser;
$order->name_destain = $request->name;
$order->state = 1;
$order->total = $request->total;
$order->created_at = Carbon::now();;
$order->updated_at = Carbon::now();;
$order->save();

You can do it like, I assume that the name of other table is order_details and its model is OrderDetail.php
$order = new order;
$orderDetailObj = new OrderDetail; // or you can insert it in existing one OrderDetail::find($id); here $id is the particular (primary key)id of OrderDetail
$order->id_user = 1; //$iduser;
$order->name_destain = $request->name;
$order->state = 1;
$order->total = $request->total;
$order->created_at = Carbon::now();;
$order->updated_at = Carbon::now();;
if($order->save()){
$orderDetailObj->order_id = $order->id_user
$orderDetailObj->update();
return true;
}

Related

laravel controller how to insert an id without auto increment?

$category = new HmsBbrCategory;
$category->category_name = $request->input('category_name');
$category->category_description = $request->input('category_description');
$category->save();
$category->category_id = $category->id;
$category->save();
I have this table, only the id is set to not null. I need to insert the column WITHOUT using auto increment, what do I add to my function?
I was thinking to count the id and just to add +1 so that the numbers are in squence:
$category->id = id()->count()+1;
This line is not counting though
fetch last id from db
$categoryId=HmsBbrCategory::orderByDesc('id')->first();
$autoIncId=$categoryId->id+1;
$category = new HmsBbrCategory;
$category->category_name = $request->input('category_name');
$category->category_description = $request->input('category_description');
$category->id=$autoIncId;
$category->category_id =$autoIncId;
$category->save();

How to delete a collection (one to many relation) in Propel?

I have one to many relationship model on my database. For example, one property has more than one price:
property 1 => $200, $300
property 2 => $200, $350
etc.
This how to create property :
$prices = array();
$property = new Property();
$property->setName('property 3');
$data_prices = array(200,300,400);
foreach ($data_prices as $price {
$prc = new Price();
$prc->setPrice($price);
$prices[] = $prc; #object collection
}
$property->setPrices($prices);
$property->save();
#codes above create new records on table property and prices (related) and property id = 1
But when I try to update :
$prices = array();
$property = PropertyQuery::create()->findOneByPropertyId(1);
#create new prices
$data_prices = array(10,20,30);
foreach ($data_prices as $price) {
$prc = new Price();
$prc->setPrice($price);
$prices[] = $prc; #object collection
}
$property->setPrices($prices);
$property->save();
instead of deleting old price records or updating them, Propel creates new records. I want to delete old price records because I don't need them, or, if I can update the old with the new price it would be great.
How to do this in Propel?
Declaring the Price objects as new does just that—creates new Price objects. You need to either call the Price objects from the Property object or load the Price objects inside your foreach loop. Since I don't know the structure of your Price table, I can only give you the former solution:
$prices = array();
$property = PropertyQuery::create()->findOneByPropertyId(1);
$prices = $property->getPrices();
$data_prices = array(10,20,30);
for ($i = 0; $i < count($data_prices); $i++) {
if (!isset($prices[$i])) {
$prc = new Price();
$prc->setPrice($price);
$prc->save();
$prices[] = $prc;
} else {
$prc = $prices[$i];
$prc->setPrice($data_prices[$i]);
$prc->save();
}
}
$property->save();

Laravel Copy record and duplicate with new values

How can i copy a record and save it with a different value for 1 or more field?
for example:
get----> first_name, last_name, civil_status, work
i want to copy the first name, and last name then insert it with a new civil status and work.
You could use the replicate method of model like this:
// Retrieve the first task
$task = Task::first();
$newTask = $task->replicate();
$newTask->project_id = 16; // the new project_id
$newTask->save();
You could use replicate method. This will create a new object with the same values except the primary key, and the timestamps. After that you can save your model:
$task = Task::find(1);
$new = $task->replicate();
If you want you could change a property
$new->project = $otherProject;
and then
$new->save();
if you want the new ID, easy:
$newID = $new->id;
You can use replicate() and then update that row:
Model::find(1)->replicate()->save();
$model = Model::find(1);
$model->project_id = $new_project_id;
$model->save();
also you can do like this.
$selected = Model::find(1);
$copy = $selected->replicate()->fill(
[
'project_id' => $new_project_id,
]
);
$copy->save();

Laravel how to get a Model by two parameters

Hi I'm new to Laravel and got stuck in a Problem.
I have a model Contact
class Contact extends Eloquent {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'contacts';
}
The table have these fields:
user_id (int)
contact_id (int)
...
these two fields represent the primary key.
In the ContactsController I have the function store in wich I create or update the database:
public function store()
{
switch (Input::get('type')){
case 'make_contact_request':
$user = User::where('email', '=', Input::get('login'));
$request_self = new Contact;
$request_contact = new Contact;
$request_self->user_id = Auth::user()->id;
$request_self->contact_id = $user->id;
$request_self->status = 2;
$request_self->message = Input::get('message');
$request_contact->user_id = $user->id;
$request_contact->contact_id = Auth::user()->id;
$request_contact->status = 1;
$request_contact->message = Input::get('message');
$request_self->save();
$request_contact->save();
break;
case 'answer_contact_request':
$request_self = Contact::where('user_id', '=',Input::get('contact_id'))->where('contact_id', '=', Auth::user()->id)->first();
//$request_self = Contact::whereRaw('user_id = '.Input::get('contact_id').' AND contact_id = '.Auth::user()->id.' ');
$request_contact = Contact::whereRaw('user_id = '.Auth::user()->id.' AND contact_id = '.Input::get('contact_id').' ');
$request_self->status = 3;
$request_contact->status = 3;
$request_self->save();
$request_contact->save();
break;
}
}
I tried two different ways to get the Contact Object for the request_self Object and I get the following error:
message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `contacts` set `status` = 3, `updated_at` = 2014-08-02 16:16:56 where `id` is null)"
for the request_contact Object it throws a fatal error (don't get the description) and close the session.
At the end I am at the beginning of laravel so I hope the solution is pretty easy to find :) but I dont even really know for what to search.
Update:
At the end I fixed the Problem with the update function.
case 'answer_contact_request':
$request_self = Contact::where('user_id', '=',Input::get('contact_id'))->where('contact_id', '=', Auth::user()->id)->update(array('status' => 3));
$request_contact = Contact::where('user_id', '=', Auth::user()->id)->where('contact_id', '=', Input::get('contact_id'))->update(array('status' => 3));
break;
I think you can add
public function scopeComposite($query, $user_id, $contact_id)
{
return $query->where('user_id', '=', $user_id)->where('contact_id', '=', $contact_id);
}
and then you can get the contact with:
$request_self = Contact::composite(Input::get('contact_id'), Auth::user()->id)->get();
source: http://laravel.com/docs/eloquent#query-scopes
I'm not sure you can make it like this.
There is a way to make sure it works:
add a column id ( auto increment, primary ) and make the group ( contact_id, user_id ) unique and you can use query scopes and id based

get the last row in the db

What is the correct way to retrieve the last row, sorted desc by id, in CodeIgniter?
I've tried select_max but I want the whole row.
function getLastPagination(){
$data = array();
$this->db->query("SELECT id,sort FROM pagination GROUP BY id DESC LIMIT 1");
$query = $this->db->get('pagination');
$data = $query->row_array();
return $data;
}
$data['allpag'] = $this->Model_cats->getLastPagination();
$data['pagin'] = $data['allpag']['sort'];
$per_page = $data['pagin'];
Here I am getting the first value, but I want the last row.
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
Try this.............
function getLastPagination(){
$query ="select * from pagination order by id DESC limit 1";
$res = $this->db->query($query);
if($res->num_rows() > 0) {
return $res->result("array");
}
return array();
}
}
In controller function you need to do following things.......
$allpag = $this->Model_cats->getLastPagination();
$per_page = $allpag[0]['sort'];
$query = $this->db->query("SELECT * FROM pagination ORDER BY id DESC LIMIT 1")->row_array();
Should work?

Resources