#### Category : eloquent

I want to edit an invoice, which contains products (dynamically added fields with 3 dynamic dropdowns). So, the idea is I want to show the old products of the invoice with the possibility to add new ones. But I don’t know how to get the old values of the dynamic dropdowns. I did it only ..

Read more

I realized that laravel/eloquent does not save my model when I change a column with an object cast like this: $original = ‘{"test": null}’;$new = ‘{"test": false}’; The reason is that attributes that are casted as an object are evaluated like this (to find out if the record is dirty) in HasAttributes.php::R1577: } elseif ..

Read more

i’m developing a Laravel 8 application I need to take values from a table, count how many occurrences I find of anagrafica_soggetto.codiceFiscale by adding the values of the field withDB :: raw (‘sum (importoResiduo) as residuo’). My problem is that I need to group these results into six bands with where clauses. The where clauses ..

Read more

I am making an application with Laravel 8 and Livewire. In my reportResidui.blade.php view I have integrated a livewire component called report-residui-header-filter and which I use to filter (this component works fine). The problem occurs when I perform the query, I need to filter from a table going to group the data for the field ..

Read more

This is the raw query "SELECT * FROM re_customer WHERE LOWER(email) = ‘" . $input[’email’] . "’ AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1(‘" .$input[‘password’] . "’))))) OR password = ‘" . md5( $input[‘password’] ) . "’) AND status = ‘1’"; I tried$customer = DB::connection( ‘oc’ ) ->table( Customer::$customerTable ) ->where( DB::raw( ‘LOWER(email)’ ), .. Read more I have like this model: class Post extends Model { use HasFactory; protected$guarded = []; public function comments() { return $this->morphMany(Comment::class, ‘commentable’); } public function images() { return$this->morphMany(Image::class, ‘imageable’); } public function views() { return $this->morphMany(View::class, ‘viewable’); } public function category() { return$this->belongsTo(Category::class); } } With this query I’ll get posts: $posts .. Read more I have like this model: class Post extends Model { use HasFactory; protected$guarded = []; public function comments() { return $this->morphMany(Comment::class, ‘commentable’); } public function images() { return$this->morphMany(Image::class, ‘imageable’); } public function views() { return $this->morphMany(View::class, ‘viewable’); } public function category() { return$this->belongsTo(Category::class); } } With this query I’ll get posts: $posts .. Read more I’m trying this code$query = Parent::where(‘state’, 1) ->with(array(‘child’=> function ($q) use ($end_date) { $q->where(‘start_date’, ‘<=’,$end_date); })); $query->whereHas("child", function ($query) use ($filter) { if (isset($filter["id"]) && $filter["id"] != "") {$query->where("id", ‘=’, $filter["id"]); } }); then in Parent Models i have this code public function child() { return$this->hasOne(‘AppModelsChild’, ‘code’, ‘code’); } I want ..

Read more

Is there an equivalent query for this in eloquent? SELECT DISTINCT agent_code,ord_amount FROM orders Sourc..

Read more

I am trying to insert a new record in my SQL Server database, but it won’t insert because the identity is set as 0, which will lead to the duplication exception. I’ve created my table id as follow: id BIGINT NOT NULL PRIMARY KEY IDENTITY(1,1) Here is my model: class MyTable extends Model { const ..

Read more

I have two tables in play for this query. First one is forecasts this contains user’s forecast on several games, the most important fields are: user_id, game_id and userscore Then there’s games table which contains game_id, week and realscore I’m oversimplifying, but that’s the idea. In the games I have all the games that will ..

Read more

I’m trying to delete the old records before inserting new ones using Laravel. First I tried using delete() , then found the 404 not found error because I’m using a Controller. Changed to destroy() and now I’m facing the BadMethodCallException error BadMethodCallException Call to undefined method IlluminateDatabaseEloquentBuilder::destroy() My code: MyClass::where(‘field_id’, $id)->destroy(); I’m stuck, dunno how .. Read more Hard to describe this in the title so this image might clarify: I have a recursive view which outputs the above: @foreach($pages as $page) <option value="{{$page->id}}" @if($selected ==$page->id) selected @endif >@for($i=0;$i<$levels;$i++)↪@endfor {{$page->title}} (#{{$page->id}}) </option> @if($page->children->count()) @include(‘cms::admin.partials.page-selector-option’, [‘pages’ =>$page->children, ‘levels’ => $levels + 1]) @endif @endforeach Let’s say that a page has .. Read more Im having issue to update specific stock in my many to many relation pivot table. The code that i have right now:$product->sizes()->detach($product);$prodsize->products()->attach([ $product->id => [ ‘stock’ =>$vysledek ] ]); Picture of my table in mysql and my question is, how to select specific product from that table with specific size, and update ..

Read more

How can I display the last entry in my blade. I have this request: @foreach(($listContrat = AppStagiaire::join(‘carrieres’, ‘stagiaires.id’, ‘=’, ‘carrieres.stagiaire_id’) ->where(‘stagiaires.id’,$id) ->OrderBy(‘id’,’desc’) ->take(1) ->get()) as $key =>$car) I have this error message with this query SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘created_at’ in order clause is ambiguous (SQL: select * from stagiaires Source: ..

Read more

I’m trying to display data from a one-to-many relationship, Models: -Vehicle public function VehicleClient(){ return $this->belongsto(VehicleClient::class,’vehicle_clients_id’); } -VehicleClient public function Vehicle(){ return$this->hasMany(Vehicle::class,’id’); } Migrations -Vehicle Schema::create(‘vehicles’, function (Blueprint $table) {$table->id(); $table->string(‘color’);$table->timestamps(); }); -VehicleClient Schema::create(‘vehicle_clients’, function (Blueprint $table) {$table->id(); $table->foreignId(‘vehicles_id’)->constrained()->onUpdate(‘cascade’)->onDelete(‘cascade’);$table->timestamps(); }); Controller public function show($id) {$Vehicle = VehicleClient::where(‘clients_id’,$id)->get(); return view(‘clients.show’, .. Read more I have a query that gets a list of students and Iam using a foreach loop to iterate and get the student_id of each student. Inside the foreach loop I have another query that calculates the student marks. When calculating the student marks, i need the student_id which is from the first query Logic$students ..

Read more

I have 2 tables, one is items which contains title, quantity and price, it connects with another table call orders, so it’s one to many relationship. If i query order entries with items load as well how could i get the subtotal of each item, "qty x price = subtotal" and get the grand total ..

Read more

I am getting data from multiple tables in relation with each other. But in one of the relation I want to get userAnswers records by where(‘user_id, $userID). What is correct syntax for it public function survey_completed_show($userSurvey, $userID) {$userSurvey = UserSurvey::with(‘survey.questions.userAnswers’)->find($userSurvey); return view(‘surveys.conducted-answers’, compact(‘userSurvey’)); } I just want to get answers of the selected User, .. Read more I am building an employee management system and i want to prevent admin from deleting a department if any of its child departments have users. When i say delete i mean updating a deleted column in my departments table from 0(active) to 1(deleted). This is the code that i have now Models/Department public function users() .. Read more Hi I have table threads and questions with relation one-to-many. every message has foreign key thread_id. I want to check is last message in the thread is from user and if yes then count it, so it will count all unanswered threads by admin. +—-+————-+———+———-+——+ | id | thread_id | user_id | admin_id | text .. Read more I have a curriculum table and a table of content_texts, content_files, content_videos and quizzes. All of the tables are related to curriculum tables with one to many, where one curriculum has many content_texts, content_files, content_videos and quizzes. The problem is, when I fill in all the content_texts, content_files, content_videos and quizzes tables and I join .. Read more This is my index page where I want to get name by ids. if there is single id in Staff_id coloum than it show but if their is multiple id than it only show first one @foreach($assignments as $assignment) <tr> <td> <form action="{{ route(‘assignments.complete’,$assignment->id) }}" method="post"> @csrf @if ($assignment->done_at == null or$assignment->done_at = ..

Read more

Hello I am new on Laravel, the following SQL is verified working well as I expected, However, I can’t make it work in Laravel…… Might I have your help select *, SUM(CASE WHEN parts_trans.in_out = "In" THEN parts_trans.qty else -1 * parts_trans.qty END)as amount from parts_trans left join ( SELECT id, SUM(CASE WHEN in_out = ..

Read more

Hi i have two tables customers and charges That i want is to sort the third field that will be balance on customers foreach blade My blade : <th wire:click="sortBy(‘total_charges’)" >Charge </th> <th wire:click="sortBy(‘total_payments’)" >Payments</th> <th wire:click="sortBy(‘services_count’)">balance</th> <td>{{ $customer->total_charges}}&euro; </td> <td>{{$customer->total_payments}}&euro;</td> <td>{{ $customer->total_payments –$customer->total_charges}}&euro;</td> of course i have and other data inside foreach ..

Read more