I need help for this problem.
I need to store data in two tables. But I want them to have the same ID. i.e. I want to write data in table 1 and then in table 2 where I want the ID of table 1 to be equal to that of table 2
$tabela = new funcionario();
$tabela->nome = $request->nome;
$tabela->email = $request->email;
$tabela2 = new utilizador();
$tabela2->id = ID da tabela
$tabela2->Address = $request->Address ;
the result would be:
Table 1
ID - (auto increment) 10
name - Example
Email - Example#test.com
Table 2
ID - same as ID table 1 (10)
It is not a good approach, maybe you need the foreign key for your one to one relationship but you can add id to your model fillable or add it with the active record:
$tabela = new funcionario();
$tabela->nome = $request->nome;
$tabela->email = $request->email;
$tabela2 = new utilizador();
$tabela2->id = $tablea->id;
$tabela2->Address = $request->Address ;
Related
Many-to-many relationships
table names
table itens
table pivot_itens_names
table pivot_itens_names
id_name | id_item
1 1
2 1
3 8
4 5
I need to count how many times the id_item '1' is in the pivot_itens_names table
expected outcome: $total = 2
This works
$total = DB::select('SELECT COUNT(id_item) AS total
FROM pivot_itens_names WHERE id_item = ?',
[$idItem] );
Intended objective: do the count using eloquent with model
unsuccessful attempt
$idItem = 1;
$data = $Name::itens();
$total = $data->where('id_item', $idItem)->count();
EDITED
One solution is to create a model for the pivot table. I have doubts if this solution is a good practice.
$total = PivotItemName::where('id_item', $idItem)->count();
Add this method to the Name model:
public function items()
{
return $this->belongsToMany('App\Models\Item', 'pivot_items_names')->withPivot('id_item');
}
and then use count method to get count of them
I have a situation where I update a Postgres table column which is of type bigint[]. This array should have unique numbers inside it whenever an update query is fired.
The query is as given below
UPDATE book_shelf
SET book_id = book_id || array[CAST(:bookID AS BIGINT)], updated_at = now()
WHERE user_id = :userID AND shelf_name = :shelfName
When the above query is fired, it simply adds the number into the array which I don't want to happen. It should hold only unique values. Please help me.
You can check if it exists in the array before adding it:
UPDATE book_shelf
SET book_id = CASE WHEN CAST(:bookID AS BIGINT) = ANY(book_id) THEN book_id ELSE ARRAY_APPEND(book_id, CAST(:bookID AS BIGINT)) END, updated_at = now()
WHERE user_id = :userID AND shelf_name = :shelfName
Of course if updated_at should only be set if book_id is actually updated, then put the check in the WHERE clause so it's not updated unnecessarily:
UPDATE book_shelf
SET book_id = ARRAY_APPEND(book_id, CAST(:bookID AS BIGINT)), updated_at = now()
WHERE user_id = :userID
AND shelf_name = :shelfName
AND NOT CAST(:bookID AS BIGINT) = ANY(book_id)
I have two tables
tb_teachers
-----------
id
name
school_id
tb_class_to_teachers
--------------------
id
teacher_id
class_id
assigned_status
school_id
How to get records from tb_teachers if it(teacher_id and school_id) does not exist in another related table tb_class_to_teachers using laravel query builder
I assume you set up your relationship correctly, then u may use doesntHave
$teachers = App\Teachers::doesntHave('class')->get();
try this
$classToTeachers = DB::table('tb_class_to_teachers')->get();
if($classToTeachers->teacher_id == null && $classToTeachers->school_id == null)
{
$teacher = DB::table('tb_teachers')->get();
}
I have 2 tables SjohlLBzads_products & SjohlLBzads_products_meta connected by column (post_id) as foreign key.
How do I create queries on multiple tables ?
Controller function which I have right now only makes query to a single table (SjohlLBzads_products).
I would like to display shipping column in SjohlLBzads_products_meta together with other columns from the current table.
public function index(){
$this->load->library('lib_pagination');
$pg_config['sql'] = "SELECT * from SjohlLBzads_products";
$pg_config['per_page'] = 50;
$data = $this->lib_pagination->create_pagination($pg_config);
$this->load->view("product_listing", $data);
Thanks!
Use a JOIN statement
$pg_config['sql'] = "SELECT * FROM SjohlLBzads_products
JOIN johlLBzads_products_meta
ON johlLBzads_products.post_id = johlLBzads_products_meta.post_id ";
I'm having some trouble with my pivot table. I've recognized too late, that it is possible, that some pivot rows doesn't have unique values in my project, means I've to add an auto_increment ID field to my pivot table.
This is my structure:
Order.php
public function items()
{
return $this->belongsToMany('Item', 'orders_items', 'order_id', 'item_id')->withPivot(['single_price', 'hours', 'prov', 'nanny_id', 'vat', 'vat_perc', 'invoice_id','id']);
}
orders_items
id, order_id, item_id, nanny_id, hours
I've conntected Orders and Items through a pivot table ('orders_items)'. It is possible, that one order has 2 or more same items in the pivot table. So I've to add an unique ID to identify and update them.
Now I try to update a pivot row. Problem is, if I have 2 or more items, he updates them all, not only one. This is my update command:
$order = Order::find($orderId);
$items = $order->items()->whereNull('nanny_id');
$free_item = $items->first();
$free_item->pivot->nanny_id = 123;
$free_item->pivot->save();
With this command, he updates all pivot rows from the order. I know the problem: Laravel uses here the wrong identifiers (it uses order_id and item_id as defined in my belongsToMany relationship - and they aren't unique). For example, Laravel tries to execute this code on save():
UPDATE orders_items SET [...] WHERE order_id = 123 AND item_id = 2;
I want, that Laravel changes the query to this one:
UPDATE orders_items SET [...] WHERE order_id = 123 AND item_id = 2 AND id = 45;
// Edit
Okay, this solution works:
$free_item->pivot->where('id',$free_item->pivot->id)->update('nanny_id',123);
But is there an easier way, f.e. adding a custom pivot model that adds the id automatically to save() and update() methods?