I’m working on a Laravel 9 project and have created a custom validation rule called ValidModelOwnership which should check that the field a user is trying to add is owned by a model based on some values passed to it. I’ve written the rule, but when debugging and outputting $model->toSql() the id is empty? What am I missing? My rule:
Tag: laravel
how join laravel tables to get non intersected data
in my Laravel app I have 3 tables : users, documents and type_documents, the user have multiple documents and document have one type_document I want select the types that are not used in documents table for the current user with eloquent I try with this, but it give me the used type_documents : I use Laravel version 8 Answer Have
Laravel 9 – implode() and whereIn
Hello i’m using query builder in my laravel app. I’m trying to use the implode() method from query builder. So for example i expect a string composed from : ‘ 2,3,4… ‘ I have to use this string in next query where i use the raw query IN (‘value’, ‘value’) I’m trying to wrap those IDs in to single quotes
How to use CASE WHEN IN in Laravel database query builder
I have users table and a followers table. I’m doing a search to get all the users with a specific keyword, but I want the ones which are followers of the current user to appear first. I can get all the followers id’s in a subquery, so my idea was to do something like this: I’ve been trying to get
retrieve rows based on parent relationship
This is the query: I am trying to get all the checklist items that have the same item_stock_id from the checklists that have the same ambulance_id. However, the query from above is not working, showing me this error: Below is the ChecklistItem model: And this is the Checklist model: Answer To constrain the eager load you need to specify the
SQLSTATE[22007]: Invalid datetime forma
I try to save some data that it brings me from my view, which is a table, but I don’t know why it throws me that error with the insert. result of insert this is my view: table of view this is my controller: Answer id_tipo_venta seems to be an empty string which is apparently not valid. You can try
How to use this laravel query builder for this SQL Query
I have this query: SELECT `topic_posts`.*, `users`.`id` AS `adminID`, `users`.`name` as `adminName` FROM `topic_posts` INNER JOIN `topics` ON `topics`.`id` = `topic_posts`.`topic` INNER JOIN `users` ON `users`.`id` = `topic_posts`.`player` WHERE `users`.`playerAdminLevel` > 0 AND `topics`.`type` = 0 GROUP BY (`topic_posts`.`id`) And I want to use in Laravel, something like: $result = DB::table(‘topic_posts`)->join..etc And also, to use ->paginate(20) How can I do
Laravel onDelete(‘cascade’) does not work
My Tables: I am getting following errors when I try to delete support_categories Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails Answer You just write foreign key in wrong order. Instead of: Write: or you can use casadeOnDelete():
Order by select count(*) and LIMIT is very slow
I have this query in my program, when I do some sorting with select count(*) field from the query, I dont know why, it very slow when running that query. The problem is when i do some ordering from posts_count, it run more slower than i do ordering with the other field. Here’s the query: select ‘tags’.*, (select count(*) from
How to select from two selected tables in Laravel Query Builder?
I want to convert my SQL to Laravel Query Builder version, however I can’t do that. Can anyone help me to solve this case or give me some advices, thank you. Original SQL that work fine in mysql server. Converted the original SQL to Laravel Query Builder, but it doesn’t work. Error: SQLSTATE[42S02]: Base table or view not found: 1146