eloquent

Ardent + Laravel, auto hydrate relation

早过忘川 提交于 2019-12-23 04:08:37
问题 I use lavarel with ardent package. I have some problem when i want to update a row. I have 2 model Client and Address related by morphone relation. This relation work well, when i want to get a client this line return expected result : Client::with('address')->find($id); But i can't understand how to update a client with a clean solution. Somebody can answer to these questions : With Ardent how could you autoHydrate related model ? When you update some data, what is the best practice in

Laravel Eloquent Model::find doesn't work

倖福魔咒の 提交于 2019-12-23 04:04:06
问题 This don't work: $object = Model::find($id); This works: $object = Model::where('id', '=', $id)->first(); It doesn't make sense. Am I missing something? I'm using Laravel 5.2.36. 回答1: id needs to be a primary key, see: https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Model.html#method_find 回答2: I had the same problem. I sorted out it, using the function intval() to transform $id to an integer value: $id = intval($id); $object = Model::find($id); 回答3: public $primaryKey = 'id'; make it

Laravel Eloquent One to Many relationship

╄→гoц情女王★ 提交于 2019-12-23 03:15:39
问题 I have a Restaurants table and an Offers table. One restaurant may have multiple Offers. I am trying to create the relation between Restaurant - Offers using hasMany() method. Table structure : 1) restaurant id restaurant name 2) offers offer_id restaurant_ID offer_price Code : In the Restaurant Model in am doing something like this <?php namespace App; use Illuminate\Database\Eloquent\Model; class Restaurant extends Model { public function offer(){ return $this->hasMany('Offer'); } } and in

Query foreign key data from Blade template

我与影子孤独终老i 提交于 2019-12-23 03:09:39
问题 I have two models: MenuCategory and MenuItem , I want to display MenuItem data on my blade page along with its MenuCategory. I know its possible to do this by adding it to the return data in my controller however I would like to do it leveraging Eloquent instead, however I receive errors. Here are my codes: MenuCategory model public function items() { return $this->hasMany('App\MenuItem'); } MenuItem model public function category() { return $this->belongsTo('App\MenuCategory'); } Controller

Laravel Many to many - Unexpected result set on ->select()

回眸只為那壹抹淺笑 提交于 2019-12-23 02:53:20
问题 I wonder if anyone can help, as I've hit a wall and still learning Laravel ORM. Can anyone explain why, when I run: public function locationTags(){ return $this->hasMany('App\UserHasLocationTags', 'user_id') ->join('location_tags AS lt', 'lt.id', '=', 'location_tag_id'); } I get this result set: (snipped...) { "id": 1, "created_at": "2015-05-13 13:04:56", "updated_at": "2015-05-13 13:04:56", "email": "REMOVED", "firstname": "REMOVED", "lastname": "REMOVED", "location_id": 0, "deleted_at":

Laravel - get last row using whereHas

会有一股神秘感。 提交于 2019-12-23 02:42:23
问题 I am trying to get the time "created_at" for the last user activity, I have the model User , and UserActivity . I want to get the last user activity and check if the last activity of this user is 3 days to send notification, User.php <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { public function activites() { return $this->hasMany(Activty::class); } } Activity.php <?php namespace App; use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; class

Laravel: how to set connection for many to many relationship?

最后都变了- 提交于 2019-12-23 02:29:07
问题 I have the models class User extends Model { protected $connection = 'mysql'; public function areas() { return $this->belongsToMany(Area::class, 'user_areas'); } } class Area extends Model { protected $connection = 'oracle'; } the user_areas table is searched on oracle, but this are on mysql. How I can indicate the connection for the pivot table? I found this partial solution and it has worked class User extends Model { protected $connection = 'mysql' public function areas() { $instance = new

Laravel update password passes validation but doesn't update record

◇◆丶佛笑我妖孽 提交于 2019-12-23 02:28:30
问题 Hi I am using Laravel and I am using a couple of packages for user auth and roles which are Zizaco Confide and I want to update the users password firstly they should input there current password a new password and confirm the password and they should match. Now the validation works but the users password isn't updated in the database. My code is below: public function updatePassword($id) { $rules = array( 'now_password' => 'required', 'password' => 'min:5|confirmed|different:now_password',

How to do the following with doesn't have or other optimized way in laravel

断了今生、忘了曾经 提交于 2019-12-23 01:52:34
问题 I have a thread which gave the answer but later on I found that I was getting limitations: how to get list of users who are not under belongsToMany relation under a table in laravel? so creating a new thread where I do have an answer but now how can I optimize the same with any prebuild functions like doesntHave or something entirely else. below is the code which gives me the list of users who are under a group and not assigned any task. one group can have multiple tasks so only users where

Laravel 4, how to access reverse one-to-many relation?

心不动则不痛 提交于 2019-12-22 18:31:42
问题 Code: <?php class Catering extends \Eloquent { protected $table = 'catering'; public $timestamps = FALSE; public function offers() { return $this->hasMany('Offer', 'cid'); } } class Offer extends \Eloquent { protected $table = 'catering_offer'; public $timestamps = FALSE; public function catering() { return $this->belongsTo('Catering'); } } I am able to do $offers = Catering::find(1)->offers; but , the inverse is not working: $catering = Offer::find(1)->catering; is always returning NULL.