eloquent

Displaying pivot table data

安稳与你 提交于 2020-04-16 04:00:05
问题 I am working on a library application, and I want to create a function, where the user can check out a book to a customer. So I have 2 tables, books and readers and I have created a pivot table, called book_reader as well to create the checkout method. However, I have some problems displaying the data from this table. I have read several articles about this, but they don't take in consideration that there is some data in the pivot table that contains new information and isn't just the joining

How to optimize query statement in Laravel 7?

我只是一个虾纸丫 提交于 2020-04-14 06:13:29
问题 I show code in the screenshot, because I want to show you guys my line 43 The actual code is here : public function index() { $inputs = Request::all(); $interval = ''; if(array_key_exists('interval', $inputs)){ $interval = $inputs['interval']; } switch ($interval) { case 'day': $visitors = Visitor::where('created_at', '>', now()->today())->paginate(20);; break; case 'week': $visitors = Visitor::where('created_at', '>', now()->subMonth())->paginate(20);; break; case 'month': $visitors =

How to get all rows (soft deleted too) from a table in Laravel?

浪子不回头ぞ 提交于 2020-04-07 16:57:35
问题 To get all rows from a table, I have to use Model::all() but (from good reason) this doesn't gives me back the soft deleted rows. Is there a way I can accomplish this with Eloquent? 回答1: To also get soft deleted models $trashedAndNotTrashed = Model::withTrashed()->get(); Only soft deleted models in your results $onlySoftDeleted = Model::onlyTrashed()->get(); 回答2: Use this to get all record Model::withTrashed()->get(); Use this to get record of particular id Property::withTrashed()->find($list

Laravel 5: Handle multiple connections and testing

旧巷老猫 提交于 2020-04-06 04:45:27
问题 I have a Laravel 5.4 app which has models pointing to different database connections. For example, I have User pointing to a MySQL database and then Company pointing to a PostgreSQL database (using the $connection variable). Now, when I run PHPUnit I'd like the $connection variable to be replaced by what's specified in the phpunit.xml file, which is a SQLite in memory type of database. How is that achievable? 回答1: Most answers are changing production code, which I don't like. Since

Laravel Dynamic Fillable in Models

Deadly 提交于 2020-04-05 15:25:09
问题 Got stuck in a issue with laravel 5.2. Following is the error during eloquent create operation(post call), Mass Assignment Exception in Model.php 453: column_name Following are the prerequisites, which are to be taken into consideration: Fillables in model are filled in a dynamic manner by the following code: public function __construct() { $this->fillable(\Schema::getColumnListing($this->getTable())) } Following are the methods which are debugged till now: Before insertion, in controller,

Cross-Reference Array elements bei named index - PHP/Laravel/Lumen

折月煮酒 提交于 2020-03-25 18:21:45
问题 TL;DR: I want to use data from a 1-dimensional array of arbitrary size, created by userinput, and fill its values into the appropriate fields of a 2-dimensional array of arbitrary size, created via query from the Database. I have a webapplication where the user can access the DBs data both in read-mode and write-mode. The DB records accessible to him are determined by the departments he belongs to. The records are organized in a DB structure where a coretable contains data visible to ALL

Cross-Reference Array elements bei named index - PHP/Laravel/Lumen

心不动则不痛 提交于 2020-03-25 18:21:05
问题 TL;DR: I want to use data from a 1-dimensional array of arbitrary size, created by userinput, and fill its values into the appropriate fields of a 2-dimensional array of arbitrary size, created via query from the Database. I have a webapplication where the user can access the DBs data both in read-mode and write-mode. The DB records accessible to him are determined by the departments he belongs to. The records are organized in a DB structure where a coretable contains data visible to ALL

How to join two tables with a pivot table using Laravel?

淺唱寂寞╮ 提交于 2020-03-25 16:00:49
问题 I have these three tables: tbl_lista_contactabilidad tbl_equipo_postventaatc users ------------------------- ----------------------- ----- id id id usuarios_id asesor_id name tbl_lista_contactabilidad.usuarios_id should be related with tbl_equipo_postventaatc.asesor_id . asesor_id should be the "pivot" between tbl_lista_contactabilidad.usuarios_id and users.id to make the relation. I want to make this relation so I tried to do this relation in this way (I will put only the relation of the

How to join two tables with a pivot table using Laravel?

拟墨画扇 提交于 2020-03-25 16:00:26
问题 I have these three tables: tbl_lista_contactabilidad tbl_equipo_postventaatc users ------------------------- ----------------------- ----- id id id usuarios_id asesor_id name tbl_lista_contactabilidad.usuarios_id should be related with tbl_equipo_postventaatc.asesor_id . asesor_id should be the "pivot" between tbl_lista_contactabilidad.usuarios_id and users.id to make the relation. I want to make this relation so I tried to do this relation in this way (I will put only the relation of the

Eager Loading on laravel query when nested 3 deep

被刻印的时光 ゝ 提交于 2020-03-25 12:32:29
问题 I have Jobs and Projects in my Laravel site and there is a relationship between them. I'm trying to reduce the number of queries made (and be mindful of N+1 problem). I do this with eager loading in my controller when outputting my JobItems. How can I do this when I nest: JobItems > Project > JobItems. There is one instance where this happens and I am getting duplicate queries. I'm not sure how to eager load. I'll outline the situation: Each JobItem has a column for hours. In my project I SUM