eloquent

Laravel From Raw DB to Eloquent

核能气质少年 提交于 2019-12-24 11:24:52
问题 i m trying to change my laravel raw query to eloquent, since now i have got some basics of eloquent and i have tried making blogs in laravel. But some complexity has remain the same. First: this is my Raw SQL: select group_concat(DISTINCT q.sku SEPARATOR ", ") as sku, `sales_flat_order`.`status`, `sales_flat_order`.`increment_id`, `sales_flat_order`.`shipping_description`, `sales_flat_order`.`subtotal`, `sales_flat_order`.`customer_email`, `d`.`country_id`, `d`.`region`, `d`.`city`, `d`.

Laravel, make nested SQL query with inner join on Eloquent and Laravel's Models

吃可爱长大的小学妹 提交于 2019-12-24 10:45:21
问题 I have followng database: Simply speaking, Users have many shops, Shops have many products etc. I need to make this query with the help of Eloquent ORM: SELECT * FROM tmp.shops INNER JOIN (SELECT * FROM tmp.products WHERE tmp.products.shop_id IN (SELECT id FROM shops where user_id = 1)) as nested_query ON tmp.shops.id = nested_query.shop_id; I need to catch information about each product in user's shop and info about shop. About my Models. This is relation with Shop in User model /** *

Best way to check if a Model exists in a many-to-many relationship

非 Y 不嫁゛ 提交于 2019-12-24 09:24:11
问题 I have some data being posted to the server and am retrieving a Player based on that data (an id). I am using the following code: $player = Player::findOrFail($player_data['id']); However, I want to check that this Player belongs to a specific Team - a belongsToMany relationship. Is there a better way than something like: if (! count($player->team()->find($teamId))) { // exit early, form may have been 'hacked' abort(404); } ? team() and not teams() , even though its a many-to-many. 回答1: Use

Defining many-to-many bidirectional relations more eloquently

天涯浪子 提交于 2019-12-24 08:59:12
问题 I have implemented the relationship without Eloquent but I was wondering is there was a way to define this relationship in Eloquent so that my application can have more consistency. table User -id -other user attributes table friend_requests: -id -sender_id -reciever_id table friends -id -first -second The friendRequest relation has been easily implemented in the Eloquent but the problem lies in Friends. If I do this in the User model class: public function friends(){ return $this-

Laravel Eloquent - querying pivot table

寵の児 提交于 2019-12-24 08:57:00
问题 in my Laravel app I have three database tables called users, projects and roles. There is m:n relation between them so I have also pivot table called project_user_role. Pivot table contains user_id, project_id and role_id columns. See image for screenshot from MySQL Workbench. My User, Project and Role models got defined belongsToMany relation like that: //User model example public function projects() { return $this->belongsToMany('App\Library\Models\Project', 'project_user_role')->withPivot(

Eloquent save() method not working inside for loop

穿精又带淫゛_ 提交于 2019-12-24 08:22:05
问题 I have a table like given below and this table is going to be used for project images. id: primary key project_id: a column that has correlation with projects. order_num: represents image list order for specific project (eg. interior view of project, exterior view of construction project, a different aspect etc...) +---------+-------------+-----------+ | id | project_id | order_num | +---------+-------------+-----------+ | 1 | 15 | 0 | +---------+-------------+-----------+ | 2 | 15 | 0 | +---

Laravel Eloquent: How to select friend user model?

笑着哭i 提交于 2019-12-24 08:07:14
问题 I am trying to select all user objects of friends. A friend can either be the first user or the second user of the saved friendship. How do I do this using Eloquent? Here is what I've tried: $friends = Friend::select(array('users.id', 'users.name')) ->leftJoin('users', function($join) { $join->on('user_id_1', '=', 'users.id'); // $join->on('user_id_2', '=', 'users.id'); }) ->where( ['user_id_1' => $myID, 'accepted' => '1'] ) ->orWhere( ['user_id_2' => $myID, 'accepted' => '1'] )->get();" Any

With function not working to load has many relation data laravel Model Eloquent

笑着哭i 提交于 2019-12-24 07:58:31
问题 I have these two model. This phone model is a common model which can be used to save and fetch the phone value for a user, customer, Employee and so on. So the meta_value is used to save the id of related model and meta_key is used to determine the model name relation. /* Customer Model*/ namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Customer extends Model { /** * Get the Phone List. */ public function phones(){ return $this-

Laravel Eloquent collection attributes

妖精的绣舞 提交于 2019-12-24 07:06:26
问题 I'm developing a webapp with Laravel 5.2 but there is an issue that I can't solve. I have a model which extends Eloquent Model but when I try to output my database table with "where", for example \App\MyModel::where('id_table', 23)->first(); It return a collection with many informations not useful for me at this moment like 'guarded', 'keytype'... and my table's data are under 'attributes'. Following laravel's guide I see everybody use simply where like me and then output with $mytable-

Force Eloquent Models to output all timestamps as long

佐手、 提交于 2019-12-24 06:41:15
问题 I'm working on a backend using Eloquent Models. In order to set a default way to output dates, I'm looking for a way to tell the Eloquent Model to output its date attributes as a long (unix-timestamp). $user = User:find(id); echo $user->toJson(); // the current output { "id":1, "username":"root", "created_at":"2016-12-25 18:23:34", "updated_at":"2016-12-25 18:23:34" } // the desired output { "id":1, "username":"root", "created_at":1482690214, "updated_at":1482690214 } Is there a clean way to