I want to delete multiple rows from my post_tags table where post_id is given .
table structure post_tags is : post_id , tag_id , created_at , updated_at
I am able to do this operation with DB query builder , but i want to
delete multiple rows with single elequent command without using forloop
I don't know how to do this ?
You should try this:
$post_id = [1,2,3];
DB::table('post_tags')
->whereIn('id',$post_id)
->delete();
You can pass Model::destroy($ids) an array. Alternatively, loop the records you want to delete and call delete on each: $model->postTags->each->delete().
Related
I have a cartitem table I where there are two field named vendor_id and cookie_identifier I want to retrieve a specific cookie identifiers records group by vendor id..how can it possible. I write a query which is given bellow it does not work
$addTocartItems = CartItem::groupBy('vendor_id')
->where('cookie_identifier', $getCookieIdentifier)
->get();
I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well
I have two tables: requestgenerals and requestinformations.
The relationship between the 2 tables is:
requestinformations belongsTo requestgenerals
requestgenerals hasMany requestinformation.
Below are the tables:
requestgenerals table
and
requestinformations table
I tried the following: $requestgenerals = Requestgeneral::without('requestinformation')->get(); but I still get all the rows from the requestgenerals table instead of just two.Please assist
You should use this:
$requestgenerals = Requestgeneral::doesntHave('requestinformations')->get();
if you want to get all records that don't have related record in second table. Take a look at Eloquent documentation.
Use doesntHave for get data doestnt have requestinformations
$requestgenerals = Requestgeneral::doesntHave('requestinformations')->get();
For refernce refer this link
Example
There will be four tables, in which first table id is the primary key and for other tables it will be foreign key.How to insert data.
data should be related to the first table id only.
you can manage it by retrieve the id(Primary Key) of the stored ligne. Just like that
$primId = PrimModel::create([$datas]);
/* where datas are the informations you want to store
eg : $primId = PrimModel::create(['arg1' => 'val1', 'arg2'=> $val2, ...]); */
//Now you can store the others like that
ForeignModel::create(['primId' => $primId->id, ...]);
...
Hope this could help you.
Suppose I have just inserted row on table test as like Test::create($inputs);,which has auto increment primary key field id.How to get id of row that I have just inserted using laravel 4.2/5 ?
The following snippet:
$test = Test::create($inputs)
Will enable you to use $test->id.
You can get the inserted id by using the example
$inserted_id = Test::create($inputs)->id;
You can use the following line to fetch last insert id while save
after save the record you need to use $test->id;