Category : eloquent

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

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

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

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 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 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

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