eloquent

Laravel 4 and Eloquent ORM - How to select the last 5 rows of a table

China☆狼群 提交于 2019-12-22 08:39:25
问题 I'm trying to figure out how to select the last 5 rows of a table to display on a home screen when there might be gaps in the ids. The database this is for has 1,000s of rows and I don't want to have to call all of them to take the last 5 every time I go to my app's home screen. The problem is that rows sometimes are deleted in the database for various reasons so, for example, if the last row's id is 4023 the second to last row's id might be 4020, so I can't just use the length and count

Laravel + Jenssegers\Mongodb: 'WhereHas' and 'Has' returns empty collection

余生颓废 提交于 2019-12-22 08:06:24
问题 I'm mainly working on two models right now, Form and Notification , and a many-to-many relationship is set up and working for most Eloquent commands, except for whereHas and has . Both just return an empty array, [] . It seems like the developer has had trouble with getting this to work in the past, but seems to have solved it here. Here's a sample of what I have so far, and what I've tried: Form.php class Form extends Eloquent { protected $connection = 'mongodb'; public function

Adding Setters and Getters to Laravel Model

倾然丶 夕夏残阳落幕 提交于 2019-12-22 07:09:46
问题 If I want an Eloquent Model class to have setters and getters for the sake of implementing an interface does the following approach make sense or is there a 'laravel' approach to the problem class MyClass extends Model implements someContract { public function setFoo($value) { parent::__set('foo', $value); return $this; } public function getFoo() { return parent::__get('foo'); } } 回答1: You are probably looking for accessors (getters) and mutators (setters). Example of an accessor (getter) in

Using a Laravel model method in an Eloquent query

天大地大妈咪最大 提交于 2019-12-22 07:09:02
问题 This is for Laravel 5.2. I have a method defined as follows in my Users model: public function name() { return "$this->name_first $this->name_last"; } I'm trying to figure out how to use that as part of a query, but it seems like it isn't possible for a somewhat obvious reason: the database doesn't know anything about the method and that makes perfect sense. However, the concept of what I'm trying to achieve makes sense in certain contexts, so I'm trying to see if there's a way to accomplish

Laravel: create a dynamic property

喜欢而已 提交于 2019-12-22 06:49:10
问题 I have a decent-size codebase built at this point. I have a couple of tables with matching Eloquent models that are storing addresses in the form ### Street, City, ST, xzipx . These are represented in the database by a single varchar field called address . Now here's my issue. I want to add a new feature that allows items to be compared by whether they are in the same city, state, etc. The way to do this as my database is currently configured is to tokenize the address string. This is fine,

Laravel won't let me migrate a table because it already exists

喜欢而已 提交于 2019-12-22 05:41:14
问题 I am trying to use Laravel Migration to create SQL tables but it won't let me. Here is the error: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'mytable' already exists Here is my code: Schema::create('mytable', function (Blueprint $table) { $table->increments('id'); $table->foreign('othertable_id') ->references('id')->on('othertable') ->onDelete('cascade'); $table->string('variable'); $table->timestamps(); }); } { Schema::drop('mytable'); } I have also checked if other

Self Join in Eloquent

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 05:36:16
问题 How would you write a self join in eloquent? Would I need to define the relationship on the model? Here's my statement: SELECT t2.title FROM products t1, products t2 WHERE t1.id = $id AND t2.color_id = t1.color_id AND t2.id != $id 回答1: You can simply define a relation to itself. public function parent() { return $this->belongsTo(self::class, 'color_id'); } public function children() { return $this->hasMany(self::class, 'color_id'); } 来源: https://stackoverflow.com/questions/30592793/self-join

Eloquent/Laravel Three-way Many-to-Many Relationship

落花浮王杯 提交于 2019-12-22 05:36:12
问题 I'm brand new to Laravel and Eloquent (and I have minimal experience with ORM in general). Let's say I have three database tables: Widgets Actions Users I have modelled one junction table that has columns for: widget_id action_id user_id timestamp The point of the junction is to keep a log of interactions--take a timestamp every time a user performs an action on a widget . I see how to model a simple many-to-many relationship using Eloquent and have this working fine, but I'm not sure how to

Laravel belongsTo Relationship

故事扮演 提交于 2019-12-22 05:08:12
问题 Ok, I am a little confused with the belongsTo relationship for Models. I have a Feeds model that extends Eloguent. I have created a relationship function called User. public function user(){ return $this->belongsTo('User'); // and I also tried return $this->belongsTo('User', 'user_id'); } On the view I am trying to do : @foreach($feeds as $feed) {{$feed->user()->first_name}} {{$feed->user()->last_name}} @endforeach but I am getting this error Undefined property: Illuminate\Database\Eloquent

Laravel 5.4 - 'updateOrCreate' method. Check If record was 'updated' or 'created'

只谈情不闲聊 提交于 2019-12-22 04:58:18
问题 Using the method updateOrCreate , is there a way to know which if a record was updated or created? UPDATE: I need to use this feature to return 201 , for created record, or 204 for updated record. Thank you. 回答1: Since updateOrCreate returns the model instance. https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Builder.php#L374 You can check that a record was recently created using: $instance->wasRecentlyCreated https://github.com/laravel/framework/blob/5.3/src