eloquent

Trying to get property of non-object (View: C:\xampp\htdocs\travel\resources\views\user\profile.blade.php)

久未见 提交于 2020-01-06 06:07:36
问题 before all that it was working perfectly fine but after i've updated the profile it began to show the error where there's only div present.profile.blade and the error is error 回答1: Instead of {{ $users->photo_id->file }} you should probably use: {{ $users->photo->file }} PS. And next time please put code into question and not put links to screenshots only. 来源: https://stackoverflow.com/questions/46673286/trying-to-get-property-of-non-object-view-c-xampp-htdocs-travel-resources-vie

Laravel eloquent - get the most recent row when using WhereIn function

倾然丶 夕夏残阳落幕 提交于 2020-01-05 17:37:46
问题 I currently have a function: Score::whereIn('site_id', $sites) ->where('type', 1) ->avg('score'); But at the moment, as you can see, it averages out amongst all results within the Scores table. I want to only average out against the most recent row of each $site_id (the table may not have all $site_id's in the results though). The table has a created_at column, which will determine which is the latest one by date/time. 回答1: Use orderBy('created_at', 'DESC') and limit your result with take(15)

Laravel eloquent - get the most recent row when using WhereIn function

妖精的绣舞 提交于 2020-01-05 17:37:11
问题 I currently have a function: Score::whereIn('site_id', $sites) ->where('type', 1) ->avg('score'); But at the moment, as you can see, it averages out amongst all results within the Scores table. I want to only average out against the most recent row of each $site_id (the table may not have all $site_id's in the results though). The table has a created_at column, which will determine which is the latest one by date/time. 回答1: Use orderBy('created_at', 'DESC') and limit your result with take(15)

Laravel 4 eloquent Mass assignment error in fillable

徘徊边缘 提交于 2020-01-05 10:31:45
问题 I'm currently studying eloquent of L4 and I encountered this mass assignment. I'd follow the instructions and I know you need to setup your $fillable to use the create method but I am still receiving a blank row in the database. here's my code: MODEL: class User extends Eloquent { protected $fillable = array('email','pwd','active','name'); } CONTROLLER: $user = new User; $add = array( 'name'=>Input::get('custname'), 'active'=>1, 'pwd'=>Hash::make(Input::get('pwd')), 'email'=>Input::get('email

Laravel 4 eloquent Mass assignment error in fillable

£可爱£侵袭症+ 提交于 2020-01-05 10:31:37
问题 I'm currently studying eloquent of L4 and I encountered this mass assignment. I'd follow the instructions and I know you need to setup your $fillable to use the create method but I am still receiving a blank row in the database. here's my code: MODEL: class User extends Eloquent { protected $fillable = array('email','pwd','active','name'); } CONTROLLER: $user = new User; $add = array( 'name'=>Input::get('custname'), 'active'=>1, 'pwd'=>Hash::make(Input::get('pwd')), 'email'=>Input::get('email

Laravel - Eloquent return only one value

淺唱寂寞╮ 提交于 2020-01-05 08:18:11
问题 How can I return one value from a belongsToMany relation. This is my relation: public function role() { return $this->belongsToMany('App\Role'); } Now when I want to access the Role Name I have to do the folowing: Auth::user()->role[0]->name But I just want to do Auth::user()->role But now my question is: "How can I do that?" 回答1: to do this you need add custom attribute to user model as the following: User Model: protected $appends = ['role']; // add new attribute to user model public

how can I use exists column with laravel model

本秂侑毒 提交于 2020-01-05 07:50:13
问题 I have a column in my database called exists , but when I try to use $model->exists Laravel checks if the record exists rater return the value of exists. Is their any way to tell Laravel not to do this on that particular model? 回答1: As suggested earlier, renaming your column would be a good idea, because it's a reserved keyword in most database servers. To answer your question though, you can use $model->getAttribute('exists') to get the value of a model attribute. Source 回答2: There's another

laravel wherebetween in relations not working

ⅰ亾dé卋堺 提交于 2020-01-05 07:18:16
问题 I have two tables in mysql, 1. Vehicles and 2.Locations i am using eloquent relations to define relation of the vehicle table to locations. here is how my vehicles modle looks. namespace App; use Illuminate\Database\Eloquent\Model; class VehicleModel extends Model { function locations(){ return $this->hasMany("App\locations", "vehicle_id", "id"); } } i am trying to run a query that can get me locations of specific vehicle between two given times. here is what i am trying to do: App

Eloquent search in two columns using like

你说的曾经没有我的故事 提交于 2020-01-05 07:01:25
问题 Hi guys can somebody help me I need to search for users in a table from an text input i did it if we search just by the name or by the last name but cant do it if we write both the name and the last name in the text input this is the code when we write just the name or the last name: $data = User::where('name', 'LIKE', '%'. $search .'%') ->orWhere('firstname', 'LIKE', '%'. $search .'%') ->get(); i think its something like this but it doesn't work :D first i did split the text given from the

Eloquent ORM aggregation vs MySql aggregation

邮差的信 提交于 2020-01-05 05:11:12
问题 I'm working on a Laravel 5.2 project and I'm using Eloquent models, but I'm not sure how to approach to aggregation features. Let's say I have Product and Price models and a hasMany relationship, so a product can have multiple prices. Now, in my Product model I'd like a function that will give me an average price of the product. I found 2 ways to get the same result: 1.) Eloquent aggregation public function avg_price() { return $this->prices()->avg('price'); } 2.) MySql aggregation public