eloquent

Only update field if form value exists

隐身守侯 提交于 2020-01-01 08:23:22
问题 I'm using Form Model Binding as such and updating my DB using the fill() and save() methods. {{ Form::model($account) }} {{ Form::text('name', null, array('class'=>'class')) }} {{ Form::text('email', null, array('class'=>'class')) }} {{ Form::password('password', array('class'=>'class')) }} {{ Form::password('password_confirmation', array('class'=>'class')) }} {{ Form::close() }} Which fires my editAccount controller method: $rules = array( 'name' => array('required'), 'email' => array(

Only update field if form value exists

浪尽此生 提交于 2020-01-01 08:23:08
问题 I'm using Form Model Binding as such and updating my DB using the fill() and save() methods. {{ Form::model($account) }} {{ Form::text('name', null, array('class'=>'class')) }} {{ Form::text('email', null, array('class'=>'class')) }} {{ Form::password('password', array('class'=>'class')) }} {{ Form::password('password_confirmation', array('class'=>'class')) }} {{ Form::close() }} Which fires my editAccount controller method: $rules = array( 'name' => array('required'), 'email' => array(

Laravel dynamic relationships - access model attributes on eager load

喜欢而已 提交于 2020-01-01 07:04:40
问题 I have an Eloquent relationship on my Laravel model which is dynamic - that is, the value of a particular database field determines which model will get loaded. I am able to load this relationship fine when I first instantiate the model instance and then reference the relation, but it does not work when I eager load that relationship. Specifically, I have a Product model. That product might or might not be a parent to another product. If the parent_id of the Product is set to 0 then that

Laravel - Bulk insert on duplicate key update large data set

£可爱£侵袭症+ 提交于 2020-01-01 06:16:24
问题 I have approx 80k records that I need to run an insert/update script on several times per day. INSERT INTO `my_rankings` (`id`, `rank`) VALUES (1,100),(2,99)(3,102)...(80000,3) ON DUPLICATE KEY UPDATE `rank` = values(`rank`); These records are currently in an array format: $rankings = [ ['id' => 1, 'rank' => 100], ['id' => 2, 'rank' => 99], ['id' => 3, 'rank' => 102], ... ['id' => 80000, 'rank' => 3], ] Is there a nice / performant way I can run the above mentioned update query? I have looked

Laravel 4.1: Eloquent Offset & Limit

天涯浪子 提交于 2020-01-01 04:32:06
问题 How to limit returned data from Eloquent? I tried with this: $data = Product::all()->take(4)->skip(3); And it return the error message: Call to undefined method Illuminate\Database\Eloquent\Collection::skip() It seems like eloquent don't support skip() ? So, how can I offset & limit the data from eloquent? Thank you. 回答1: You may try this (get 4 items from offset 3/4th): Product::take(4)->offset(3)->get(); Or this (get 5 items from 3rd row): Product::take(5)->skip(2)->get(); 回答2: laravel have

Disable eager relations

痞子三分冷 提交于 2020-01-01 04:07:08
问题 In my project I have many Eloquent models that have eager relations configured in class like this: protected $with = [ 'countries', 'roles' ]; But sometimes I need just old plain model without any relations. Can I somehow do: Model::noRelations()->all() Really don't wanna use query builder nor create another class just for few occasions. 回答1: If you have to set the $with property on your model rather than leaving it empty, you can manually override the relationships that need to be eager

Query for retrieving data records for a specific date range in Laravel 4

好久不见. 提交于 2020-01-01 03:44:28
问题 I have a table having record_date and corresponding amount field.I was trying to retrieve total amount grouping by the months of the date . I am new to laravel and in normal php i would have used the following mysql query --> SELECT MONTH(record_date),YEAR(record_date),SUM(amount) FROM table_name WHERE record_date > DATE_SUB(now(), INTERVAL 6 MONTH) GROUP BY YEAR(record_date), MONTH(record_date); This simply returns the total amount collected for last 6 months each grouped by the month and

How to make user, and roles relationship in laravel 5?

戏子无情 提交于 2020-01-01 03:38:10
问题 I have two tables : User -> id : name : role_id : ->refernces('id')->on('roles'); Roles -> id : role_name : access : I am trying to access roles details from user. My user model has : public function role(){ return $this->belongsTo('App\Role'); } My role model has : public function user(){ return $this->hasMany('App\User'); } When I try to do following : $user = User::find(1); $details = [ 'name' => $user->first_name, 'role' => $user->role->role_name ]; I get error : Trying to get property of

Laravel: Eloquent How to get property of model if column name contains a dash?

跟風遠走 提交于 2020-01-01 03:12:09
问题 I have the following in my root route: $user = User::all(); return $user->column-one; Which returns the exception Use of undefined constant one - assumed 'one' even though I do have a column called column-one from my users table. So how do I get column-one from my model? 回答1: After digging through the source code for the eloquent model I found the magic method __get and learned that it was just a wrapper for the public function getAttribute which takes a string thus I'm now able to retrieve

mysql join ON and AND to laravel eloquent

北城以北 提交于 2020-01-01 02:43:48
问题 I've been able to get the query result I need using the following raw sql: select `person`.`id`, `full_name`, count(actions.user_id) as total from `persons` left join `actions` on `actions`.`person_id` = `persons`.`id` and `actions`.`user_id` = $user where `type` = 'mp' group by `persons`.`id` But I haven't been able to get it working in eloquent yet. Based on some similar answers, I'd tried functions within ->where() or leftJoin() , but the count of each person's actions isn't yet being