I want to get this query using query builder:
SELECT *,
( SELECT sum(vendor_quantity)
from inventory
WHERE product_id = products.id
) as qty from products
I'm stuck with this part
(SELECT sum(vendor_quantity) from inventory where product_id = products.id)
I can do it using raw query but I want to know if there is a way to do it in query builder.
My Table Schema for products:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_type',50);
$table->string('product_name',255);
$table->string('internal_reference',255);
$table->string('barcode',255);
$table->decimal('sale_price', 10, 2);
$table->decimal('cost', 10, 2);
$table->decimal('weight', 10, 2);
$table->decimal('volume', 10, 2);
$table->integer('added_by')->unsigned();
$table->timestamps();
});
// Foreign Keys
Schema::table('products', function(Blueprint $table) {
$table->foreign('added_by')->references('id')->on('users');
});
Stocks Table:
Schema::create('stocks', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('vendor')->unsigned();
$table->string('vendor_product_code',255);
$table->string('vendor_product_name',255);
$table->integer('vendor_quantity');
$table->decimal('vendor_price', 10, 2);
$table->date('vendor_produce');
$table->date('vendor_expiry');
$table->integer('added_by')->unsigned();
$table->timestamps();
});
// Foreign Keys
Schema::table('stocks', function(Blueprint $table) {
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('vendor')->references('id')->on('customers');
$table->foreign('added_by')->references('id')->on('users');
});
can you just add what exactly do you need as output? Like what do you plan to throw at your view so I can give you the eloquent setup. From the migration above it looks like you're missing some tables like "inventory".
In any way - you first need to setup eloquent relationships between your models. For the two above, something like this:
class Stock extends Model{
public function product(){
return $this->belongsTo(Product::class);
}
}
and
class Product extends Model{
public function stock(){
return $this->hasMany(Stock::class);
}
}
Now, that sum of yours has me confused a bit... since vendor_quantity is a column in your stocks table... Do you need to get all the products and the corresponding foreign key values from the stocks table and then sum all the values in the vendor_quantity? If that's the case do something like this:
$products = Products::with('stock')->get();
This will return the eloquent collection with all your products AND foreign key values from the stock table. Since you have the values from the related table you can just iterate through each of those an add it to a variable or just append it to the initial object for passing to your view. For example
$products = Product::with('stock')->get();
foreach ($products as $key => $product){
$vendorQuantitySum = $product->stock->sum('vendor_quantity');
$products[$key]->vendorQuantity = $vendorQuantitySum;
}
Now, when you pass $products to your view, you can easily get the sum in your view like so:
{{ $product->vendorQuantity }}
I just tested it on my Laravel install and it seems to work :)
Related
I have two tables rta_list and rta_reg_company. I have a column count in rta_list table.
Schema::create('rta_list', function (Blueprint $table) {
$table->increments('rta_id');
$table->string('rta_name');
$table->string('rta_address');
$table->string('rta_phone');
$table->string('rta_email')->unique();
$table->integer('count');
$table->timestamps();
});
Schema::create('rta_reg_company', function (Blueprint $table) {
$table->increments('company_id');
$table->integer('rta_id')->unsigned();
$table->foreign('rta_id')
->references('id')
->on('rta_lists')
->onDelete('cascade');
$table->string('company_name');
$table->string('company_isin');
$table->string('company_script');
$table->string('company_address');
$table->string('company_phone');
$table->string('company_email');
$table->timestamps();
});
I want to increase the value of count of rta_list table by rta_id when I add new RTA Registered Company. For eg: if i add one company then, The value must be added in rta_reg_company table and count must be 1 in count column in rta_list table.
Also also how to show the count value by rta_id in view..
Help Needed.....
you can use laravel Observer to raise an event on a model creation.then you can update as below on model Observer:
public function created(Company $company)
{
DB::table('rta_list')->increment('count');
}
I have next pivot table:
Schema::create('coach_user', function(Blueprint $table)
{
$table->integer('coach_id')->unsigned()->index();
$table->foreign('coach_id')->references('id')->on('coaches')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->tinyInteger('rank');
});
In User.php:
public function coaches()
{
return $this->belongsToMany(\App\Coach::class)->withPivot('rank');
}
How I can receive coaches of user with some rank? Something like this:
$user->coaches->where('rank',1)->get().
use wherePivot() to filter the results returned by belongsToMany.
$user->coaches()->wherePivot('rank',1)->get();
Use wherePivot for pivot columns and relation as method:
$user->coaches()->wherePivot('rank',1)->get().
I am using Laravel 5.4
I have 3 models: Order, OrderLine and Product.
Order hasMany() OrderLines
OrderLine hasOne() Product via the product_id in the OrderLine model (I have properly indexed this, at least I think!)
My requirement is to retrieve all Orders and OrderLines where the Product is for a certain brand name.
Here is my eloquent query. I know the query works but it seems to infinitely run when put on a large dataset (circa 10,000 Orders, 12,000 OrderLines/Products)
$orders = Order::whereBetween('order_date', [$this->start_date,$this->end_date])
->whereHas('lines', function ($q1){
$q1->whereHas('product', function ($q2){
$q2->where('brand', 'Brands>SanDisk');
});
})->with('lines')->with('lines.product')->get()->toArray();
This produces the following SQL when debugging via toSql() method.
select
*
from `orders`
where
`order_date` between ? and ?
and
exists (select * from `order_lines` where `orders`.`id` =`order_lines`.`order_id`
and
exists (select * from `products` where `order_lines`.`product_id` = `products`.`id` and `brand` = ?))
My 3 migrations to create the tables are as follows (I have removed anything except keys for simplicity):
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
});
Schema::create('order_lines', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id');
$table->integer('order_id');
});
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
});
I then added the following index:
Schema::table('order_lines', function (Blueprint $table) {
$table->integer('product_id')->unsigned()->change();
$table->foreign('product_id')->references('id')->on('products');
});
Results of EXPLAIN syntax as follows:
1 PRIMARY orders ALL 91886 Using where
2 DEPENDENT SUBQUERY order_lines ALL 93166 Using where
3 DEPENDENT SUBQUERY products eq_ref PRIMARY PRIMARY 4 mymemory_main.order_lines.product_id 1 Using where
Try this:
mpyw/eloquent-has-by-non-dependent-subquery: Convert has() and whereHas() constraints to non-dependent subqueries.
mpyw/eloquent-has-by-join: Convert has() and whereHas() constraints to join() ones for single-result relations.
$orders = Order::query()
->whereBetween('order_date', [$this->start_date, $this->end_date])
->hasByNonDependentSubquery('lines.product', null, function ($q) {
$q->where('brand', 'Brands>SanDisk');
})
->with('lines.product')
->get()
->toArray();
That's all. Happy Eloquent Life!
I have 2 models and both they aren't using the ID from the table, but the field internal_id. So i customized my pivot schema but i got stuck on connecting them. Im getting the error:
General error: 1215 Cannot add foreign key constraint (SQL: alter table `seoshop_category_product` add constraint seoshop_category_product_category_id_foreign foreign key
(`category_id`) references `seoshop_categories` (`internal_id`) on delete cascade)
The code for the migration is:
Schema::create('seoshop_category_product', function(Blueprint $table)
{
$table->increments('id');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('internal_id')->on('seoshop_categories')->onDelete('cascade');
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('internal_id')->on('seoshop_products')->onDelete('cascade');
$table->timestamps();
});
Both fields as seoshop_products.internal_id as seoshop_categories.internal_id are existing, column types are both int(11).
Can someone tell me what is going wrong?
Migrations for the tables seoshop_categories and seoshop_products
//seoshop_products
public function up()
{
Schema::create('seoshop_products', function(Blueprint $table)
{
$table->increments('id');
$table->integer('shop_id');
$table->integer('internal_id')->signed()->index();
$table->integer('internal_variant_id');
$table->string('visible');
$table->string('tags');
$table->timestamps();
});
}
//Table seoshop_categories
public function up()
{
Schema::create('seoshop_categories', function(Blueprint $table)
{
$table->increments('id');
$table->integer('internal_id')->signed()->index();
$table->datetime('seoshop_created_at');
$table->datetime('seoshop_updated_at');
$table->text('full_description');
$table->timestamps();
});
}
Okay so now i've create my table, and its working as how it should. I need to get my product with categories (many-2-many). So i use
SEOshopProduct::find(1)->with('categories')->get();
After a dd() the categories are empty and i've looked into my query how it is called:
[8] array(3) {
["query"] "select `seoshop_categories`.*, `seoshop_category_product`.`product_id` as `pivot_product_id`, `seoshop_category_product`.`category_id` as `pivot_category_id` from `seoshop_categories` inner join `seoshop_category_product` on `seoshop_categories`.`id` = `seoshop_category_product`.`category_id` where `seoshop_category_product`.`product_id` in (?)"
["bindings"] array(1) {
[0] 8
}
["time"] 0.37
}
The internal_id's of both products and categories is greater then 10.000 and i dont see it back in the query.
My models:
Product:
public function categories(){
return $this->belongsToMany('SEOshopCategory', 'seoshop_category_product', 'product_id', 'category_id');
}
Categories:
public function products(){
return $this->belongsToMany('SEOshopCategory', 'seoshop_category_product', 'category_id', 'product_id');
}
To setup the foreign key constraint, the field definitions need to match exactly. In this case, however, the seoshop_category_product.category_id field is defined as an UNSIGNED INT, but the referenced seoshop_categories.internal_id field is defined as a SIGNED INT. The same is true for the foreign key for your products.
So, you can either update the internal_id fields on your categories and products tables to be unsigned, or you can update your foreign key fields on your pivot table to be signed.
You can tell Laravel what the local and foreign keys are when you define the relationship in your model...
class Product extends Eloquent
{
public function categories() {
return $this->hasMany('Category', 'internal_id', 'id');
}
}
class Category extends Eloquent
{
public function products() {
return $this->hasMany('Product', 'internal_id', 'id');
}
}
I have a table column which holds an array of subject ids selected by the user. There is another table for these subjects and their values. I need to return the values corresponding to the ids saved in the subjects column. To make it more clear suppose that a user have chosen 5 subjects out of 34 subjects and the corresponding ids are saved in the subjects column as a string like this: 2,5,11,21,23
Each of these numbers corresponds to the id of a subject in the subjects table.
//This is the subjects table
public function up()
{
Schema::create('subjects', function (Blueprint $table) {
$table->increments('id');
$table->string('subject', 20);
$table->timestamps();
});
}
//and this is the user_info table
public function up()
{
Schema::create('user_info', function (Blueprint $table) {
...
$table->string('subjects');
...
});
}
How can I return an array of subject values to a view?
// Find whichever user
$user = \App\User::find(1);
// Convert the string of subjects from a string to an array
$subjectIds = explode(',', $user->subjects);
// Load all subjects which match the ids within the subjectIds array
$subjects = \App\Subjects::whereIn($subjectIds)->get();
// Do something with the subjects
foreach($subjects as $subject) {
// Output the subject name
var_dump($subject->name);
}
After some searching around I found that maybe the best solution for my problem was to use the Many to Many relationship. So I removed the subjectscolumn from user_info table. Then I created the pivot table subject_user to save the id of user and their subjects ids in this table.
This is the pivot table schema:
Schema::create('subject_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->index();
$table->integer('subject_id')->index();
$table->timestamps();
});
Then in the User and Subject models I established the proper many to many relationship as follows:
//User Model
public function subjects()
{
return $this->belongsToMany('App\Subject')->withTimestamps();
}
//Subject Model
public function users()
{
return $this->belongsToMany('App\User');
}
Finally in the controller i used the attach() and sync() methods to populate or update the pivot table.
//In store method
$user->subjects()->attach($subjects);
//In update method
$user->subjects()->sync($subjects);
The difference between attach and syn is described here.