Category : eloquent

I have relation like this: DB relation I have a code in my model that retrieves me just one parent: public function AllParents() { return $this->belongsToMany($this, ‘parent’, ‘product_id’, ‘parent_id’) ->select(‘parent’, ‘name’); } I get it in my controller like this: private function product(Product $product) { return $product->Product() ->with(‘AllParents’) ->get(); } Finally I need data like ..

Read more

I have a page that displays all the records from the ‘Receita’ table like this: public function mostrarTodos() { $receitas = Receita::all()->toArray(); $ingredientes = Ingrediente::all()->toArray(); return view(‘home’, compact(‘receitas’, ‘ingredientes’)); } on the top of the page there is a form with many checkboxes that will filter the results displayed on the front page: <div class="col-span-2 ..

Read more

Kind of a backstory, I inherited a Laravel project that initially used migrations and Eloquent to handle schema changes on it’s database, but through the course of development the original developer decided it would be better to just ditch to migration workflow and directly make the changes to the schema via sql/ phpmyadmin and in ..

Read more

I’m seeding the test database with fake users, posts and categories but for some reason when I seed the database with more than 15 records Laravel throws this error: IlluminateDatabaseQueryException SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: categories.name (SQL: insert into "categories" ("name", "slug", "updated_at", "created_at") values (id, quibusdam-nihil-dignissimos-vitae-sint-minus-est-nostrum, 2021-06-22 17:52:02, 2021-06-22 17:52:02)) If ..

Read more

Each Product can have several Category On each product page, I need to display 10 "related products". To that end, I would like to create a function on the Product model, that would return other products that have the most Category in common, and that would go like so: public function related_products() { return Product::with(‘categories’)->whereHas(‘categories’,function($query) ..

Read more

I am trying to fetch a grouped By list of all my Summaries with the according Sum of the total field. To visualise it it would look something like this ID. Code. Total. Created_at. Updated_at 1 61B5A4C5 1437.64 2021-06-22 11:18:09 2021-06-22 11:18:09 2 A9B5311C 711.44 2021-06-22 11:19:10 2021-06-22 11:19:10 3 1426CDC5 711.44 2021-06-22 11:19:27 2021-06-22 ..

Read more

I am running into this issue where I can’t retrieve the softdeleted data of the relation tables. My approach was to use the withTrashed method, but it gave me no luck whatsoever. This is the line of code: return response()->json($user->orders()->withTrashed()->with(‘order_details’, ‘order_details.product’, ‘address’)->get()); It like ignores what’s inside the with clause. How do I fix this ..

Read more

I have simple hasOne relationship and fixed list of displayed columns. If to put columns on paginate method, outputed relation data will be null. $model->with([‘item:id,title’])->paginate(5); // item filled correctly $model->with([‘item:id,title’])->paginate(5, [‘id’,’name’]); // item will be null How to make it work together? Sourc..

Read more

I am working with Laravel 8 to develop online ordering project and in this project, I want to show all the orders from orders table if the status of each order is set to awaiting, so here is my code: public function awaiting() { $orders = Order::with(‘user’)->where(‘status’, ‘awaiting’)->whereHas(‘user’)->latest()->paginate(20); return view(‘admin.orders.awaiting’, compact(‘orders’)); } But now the ..

Read more

So, in my application I have models: user, category, service(i.e subcategory) and userService. There exists many to many relationship between user and category, user and service. And one to many between category and service. // Tables Users : id, name, email, password Categories : id, name Services : id, name, category_id user_services: id, user_id, category_id, ..

Read more

i have a problem, i need to add the debt of a user controller: public function cliente($id){ // $nota = AppNota::find($id); //AquĆ­ valida si existe sino redirije al 404 $datos = [ ‘category_name’ => ‘datatable’, ‘page_name’ => ‘multiple_tables’, ‘registro’ => Registro::find($id), ]; $cliente = AppModelsUser::findOrFail($id); return view(‘cliente’, compact(‘cliente’))->with($datos); } blade.php <div class="row"> <div class="col"> <h4>Debt ..

Read more

I am trying to execute the following code, the error applies to the $waitingToBeShipped… section of the code. As you can see I am using an innerJoin. I want to retrieve all of the rows that have orders containing products that are still waiting to be shipped. public function destroyMany($ids) { $status = false; $ids ..

Read more

I have the following table structure as shown in the diagram: Briefly, it is composed of several many-to-many polymorphic relationships as described: many resources can have many sources and the pivot table sourceables contains catalog_number and lot_number information to make each row in the pivot table unique. Many resources could also come from the same ..

Read more

I am new to Laravel and trying to support an existing application that is in Laravel 5. I am trying to convert the following SQL to eloquent structure SELECT s.id, CONCAT(u.first_name, ‘ ‘, u.last_name) AS user_name, u.avatar_location AS user_img, s.employee_photo, d.name AS department, seg.name AS segment, s.survey_title, s.before_action, s.before_picture, s.action, s.action_date, s.after_action, s.after_picture, s.nominated, s.awarded, ..

Read more

Following query is ignoring the where(‘contact_id’,$id) causing to return results that does not belong to this contact. Why? Transaction::where(‘contact_id’,$id) ->with(‘allocations’) ->whereHas("allocations",function ($query){ $query->select(‘transaction_id’) ->havingRaw(‘transactions.amount > sum(amount)’) ->groupBy(‘transaction_id’); }) ->orDoesntHave("allocations") ->get(); if I remove the whereHas part, it is bringing back the correct result. But I need the whereHas part. ->whereHas("allocations",function ($query){ $query->select(‘transaction_id’) ->havingRaw(‘transactions.amount > sum(amount)’) ..

Read more

I’m developing an application for Pharmacy Company, Products have many Batches. When New Invoice Creation. They don’t enter the same product with the same batch number in the Invoice Item List. How to validate using laravel Rule. My Blade (Dynamic Row) : <tr class="item_row" id="item-row-0"> <td> <select class="form-control boxed" name="items[0][product_id]" id="product_id0" onchange="productattribute(0)" data-width="100%" required> <option ..

Read more

Here’s my JSON: [ { "ID": 1, "SOURCEID": 1, "TIMESTAMP": "2020-04-05 07:05:29", "VALUE": "30" }, { "ID": 4, "SOURCEID": 2, "TIMESTAMP": "2020-04-05 07:05:17", "VALUE": "40" }, { "ID": 3, "SOURCEID": 1, "TIMESTAMP": "2020-04-06 12:04:59", "VALUE": "35" }, { "ID": 5, "SOURCEID": 1, "TIMESTAMP": "2020-06-17 12:01:32", "VALUE": "1" }, { "ID": 6, "SOURCEID": 2, "TIMESTAMP": "2021-06-17 ..

Read more