eloquent

Using COUNT() function in Eloquent ORM (outside Laravel)

喜你入骨 提交于 2020-01-11 12:44:04
问题 I'm trying to replicate an SQL query with Eloquent ORM: SELECT DISTINCT name, COUNT(name) AS total FROM tags WHERE question_id IS NOT NULL GROUP BY name ORDER BY name ASC; Here is my attempt in Eloquent ORM: $query = Tag::query(); $query->orderBy('name', 'asc'); ->groupBy('name'); ->select( array('count(name) as total') ); ->whereNotNull('question_id'); However, this gives me the following error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count(name)' in 'field list' (SQL: SELECT

Laravel : Eloquent Skip records

纵然是瞬间 提交于 2020-01-11 11:56:07
问题 How did i get my users but start only at a certain position ? I get the 50 first with : $users = User::orderBy('xp', 'DESC')->take(50)->get(); But now I want the 50 after this, so the 50th to 100th records. Thanks 回答1: You can use skip() Laravel eloqunt provides nice way to achieve this. $users = User::orderBy('xp', 'DESC')->skip(50)->take(50)->get(); Hope this helps. 来源: https://stackoverflow.com/questions/46693044/laravel-eloquent-skip-records

Laravel changes created_at on update

浪子不回头ぞ 提交于 2020-01-10 08:23:06
问题 I found this answer on the subject, but it doesn't work for me. So, I make an entry in the database: // Write lead to database $lead = Lead::create($lead_data); And the timestamps look like this, which is good: | 2016-01-08 10:34:15 | 2016-01-08 10:34:15 | But then I make a request to an external server, and I need to update the row: $lead->user_id = $response['user_id']; $lead->broker_id = $response['broker_id']; $lead->save(); and the created_at field gets changed: | 2016-01-08 04:34:17 |

Group_concat - laravel eloquent

半世苍凉 提交于 2020-01-09 11:18:06
问题 please I want to use group_concat in a query using eloquent and not raw queries. here is the code which i tried to execute and did't work for me: commands::join('products', 'products.id', '=','commands.idproduct') ->select('commands.username','**group_concat(products.name)**') ->group by ('commands. username') ->get(); Thanks in advance :) 回答1: I just used use DB; and in my query I used DB::raw('group_concat(products.name)') !! 回答2: This worked for me $list = TableName::where('user_id', 'user

How to set a default attribute value for a Laravel / Eloquent model?

风格不统一 提交于 2020-01-09 02:17:09
问题 If I try declaring a property, like this: public $quantity = 9; ...it doesn't work, because it is not considered an "attribute", but merely a property of the model class. Not only this, but also I am blocking access to the actually real and existent "quantity" attribute. What should I do, then? 回答1: An update to this... @j-bruni submitted a proposal and Laravel 4.0.x is now supporting using the following: protected $attributes = array( 'subject' => 'A Post' ); Which will automatically set

get max value for each related model with eloquent in laravel

佐手、 提交于 2020-01-07 06:25:28
问题 I have 2 model: class Message extends Eloquent { public function conversation(){ return $this->belongsTo('Conversation', 'conversationId'); } public function sender(){ return $this->belongsTo('User', 'senderId')->select('id', 'userName'); } public function receiver(){ return $this->belongsTo('User', 'receiverId')->select('id', 'userName'); } } class DeskConversation extends Eloquent { } What I want is to take all the last messages of each conversation (those included in a given array)... in

Laravel - inserting foreign keys causes Integrity constraint violation

梦想与她 提交于 2020-01-07 04:44:06
问题 I have two related models Domain and Server . I'm trying to insert data to my tables using a form. here is my store function : public function store(Request $request, Domain $domain, Server $server) { $domain->create($request->all()); $server->domain()->associate($domain); $server->save(); $server->create($request->only(['srv_hostname','srv_ip','srv_port'])); return redirect()->route('domains.index'); } the table servers has a FK domain_id that points to the PK domain.id Once I submit my form

How to sort an Eloquent subquery

為{幸葍}努か 提交于 2020-01-07 02:45:29
问题 I have two tables conected: Team and member. The models are connected by a n:m relationship and in my team views I will make a foreach loop to get the members of said team like this: @foreach( $team->teammember as $member ) {{ $member->firstname }} {{ $member->lastname }} @endforeach So far everything is great and working, my issue is, how do I get the members list sorted by lastname? In my controller I'm not getting the members, since the connection is done via the model, I can only sort the

How does Laravel morph to many work?

寵の児 提交于 2020-01-06 19:55:54
问题 I have a morph-to-many relation in my app. items can have hours and other tables can also have hours. So I have a method for declaring a relation between Items and Hours: public function hours() { return $this->morphMany( HoursetItemHourTable::class, 'owner_type', 'owner_type', 'owner_id' ); } And this methods works, but I don't understand why. Why do I have to write 'owner_type' twice as a parameter? 回答1: Short answer is: you don't. Actually, the only reason the method is working as you have

Laravel Raw query paginate; raw query to eloquent object

橙三吉。 提交于 2020-01-06 13:11:09
问题 select '0000-00-00' as date, 'Opening Balance' as narration, (SELECT debit FROM `account_sub_journal` where id=1)as debit, (SELECT credit FROM `account_sub_journal` where id=1)as credit, '0' as transaction_entry_id,'0' as account_sub_journal_id UNION SELECT * FROM `ledgertransactions` where account_sub_journal_id = 1 and `date` BETWEEN '2014-04-01' and '2014-04-10' I currently do this as a static function in model. I couldn't paginate this since laravel says it is not an object public static