laravel-eloquent

Laravel 5 eloquent - add child field to parent model

房东的猫 提交于 2019-12-11 18:09:51
问题 I have a user model which has a child relationship called teacher. How can I add a field from the related teacher model to the dataset returned of the parent user model? I would like the user model returned to have a structure like below: user.firstname user.lastname user.teacher.address I have tried using the following and variations thereof with no success: $query->select('firstname', 'lastname') ->addSelect( \DB::raw('teachers.address AS address') ); user.php model: use Notifiable; use

How to generate a query to db if several “where” and “update” are added in the loop?

萝らか妹 提交于 2019-12-11 16:47:16
问题 I have a method with four incoming arrays. The first two arrays $whereColumns[] and $updateColumns[] contain columns names. The third column $whereFilters[] contains filters for where methods. And the last one $updateFilters[] contains values for updating. How can I generate only one search query with several where methods and several updated columns? Now I know only how create a query with several where methods in a loop. for($i=0; $<count($whereColumns); $i++){ $query->where($whereColumns[

How to display groups of notices by dates one by one using Laravel 5?

夙愿已清 提交于 2019-12-11 13:15:00
问题 So if im having a notices table where i store title, body and date . Now i want to display it in the below format 2018-01-09 Title Body abc abc123 pqr pqr123 2018-01-10 Title Body mno mno123 pqr pqr123 So I have input for start date and end date which gets all notices between those dates. $notices = App\Notice::whereBetween('date', [$request->start_date, $request->end_date])->oldest('date')->get(); But now i dont know how to filter data by date and print one group of notices ex. 2018-01-09

Laravel add dynamic input fields

。_饼干妹妹 提交于 2019-12-11 07:23:40
问题 I want to insert dynamic fields into DB. I'm using the following code but it does not work as I expect. <html> <input id="reporting" type="text" value="salman" name="reporting[]"> <input id="reporting" type="text" value="ankur" name="reporting[]"> </html> <?php $report = Input::get('reporting'); for($i=0; $i<=count($report);$i++) { $news = new Reporting(); $news->user_id = 1; $news->reporting = $report; $news->save(); } ?> expected result: user_id || reporting 1 Salman 1 Ankur Can you guys

Laravel Eloquent rollback does not work, for queries separated by PHP functions

ぐ巨炮叔叔 提交于 2019-12-11 06:35:47
问题 I separate the model queries on a trait class, because I hate to read a long block of codes with the model queries, and because I find it convenient if I reused the same function. But I found a problem once an error occur. I was trying to run rollback function from eloquent once an error occur but, unfortunately, rollback wont work as I am expecting. Am I doing it wrong? Are there any other ways to implement this? try { DB::beginTransaction(); // UserDetails $userdetailsID = $this-

Copying million rows of data from one database to another on laravel

余生长醉 提交于 2019-12-11 04:03:45
问题 Using Laravel Eloquent, i'm copying 7Million rows of data from one table on my old Mysql database and putting these rows on different tables on my new Mysql database. The problem is that it took almost one day to perform this and i need to re-perform this action for almost 80M of rows. I'm using chunk of 1000 data at a time. Is there any way to do it more efficiently?? Here my code: DB::connection('oldDataBase')->table('tableToCopy')->chunk(1000, function ($AllData){ foreach ($AllData as

How to get multiple joined data in one Laravel DB Function

江枫思渺然 提交于 2019-12-11 02:29:46
问题 Update 3 (Solved but not optimal) There is a way to hack the result: public function loadRouteData($day, $week_nr, $year){ $route_data = Facturatie_Invoice::with('customer', 'invoice_lines') ->whereHas('route_manager', function ($query) use ($week_nr, $year) { $query->where(['week_nr' => $week_nr, 'year' => $year]); }) ->where('day', $day) ->get(); foreach($route_data as $rd){ for($i = 0; $i < count($rd->invoice_lines); $i++){ $product = DB::table('facturatie_product')->where('id', $rd-

How can I use “not like” on laravel mongodb?

☆樱花仙子☆ 提交于 2019-12-11 01:37:10
问题 I see here : https://github.com/jenssegers/laravel-mongodb I try : $user = Comment::where('body', 'like', '%spam%')->get(); It works But when I try : $user = Comment::where('body', 'not like', '%spam%')->get(); It does not work Seems the library not support not like Whether there are any people who know how to circumvent this? 回答1: I believe like is converted to regex anyway, so you can do it as not regexp : $user = Comment::where('body', 'not regexp', '/spam/i'))->get(); 来源: https:/

Laravel Mongo Many To Many relation wherehas not working

北慕城南 提交于 2019-12-08 19:30:08
问题 I have two mongo documents that are related to each other in a many to many relationship. One is called Lawyer and the other LawCase. My Lawyer model has: public function cases() { return $this->belongsToMany('App\LawCase'); } My LawCase model has: public function lawyers() { return $this->belongsToMany('App\Lawyer'); } All I am trying to do is find lawyers that have a certain category of law cases. $lawyers = App\Lawyer::whereHas('cases', function($q){ $q->where('category', '=', 'DUI'); })-

Laravel - select with relationship with one query

折月煮酒 提交于 2019-12-08 04:09:13
问题 I have a Person model. Each person may have zero or more cars: class Person extends Model { public function cars() { return $this->hasMany('App\Car'); } } I wish to select and display all persons who have a ford with one running query. So i tried this: $persons = Person::whereHas('cars', function ($query) { $query->where('mark', 'ford'); })->get(); foreach ($persons as $person) { foreach($person->cars()->get() as $car) { print $person->name . " has a " . $car->mark . $car->model } } The