eloquent

laravel hook into Eloquent pre and post save for every model

天大地大妈咪最大 提交于 2020-01-29 03:58:45
问题 I'm new to Laravel and ORM's in general. How could i hook into Eloquent to fire code before and after a save of any model? I know i can do the following for specific models but i'm looking at figuring out how to do this for every model. class Page extends Eloquent { public function save() { // before save code parent::save(); // after save code } } 回答1: You can create a BaseModel class that extends eloquent and then have all your models extend BaseModel. Here's an example: abstract class

How to eager load relationships on multiple levels?

回眸只為那壹抹淺笑 提交于 2020-01-25 16:49:07
问题 I have the following models: ProductA - ProductB - Feature - Option I have the following relations: ProductA belongsToMany ProductB ProductB belongsToMany Feature Feature belongsToMany Option When I want to see details of ProductA, (a post with ProductA id, that will be managed by a controller) I'd like to eager load all the relationships to pass a variable containing all the details to the view. Is it possible with a single Eloquent instruction? Something like this (I know it's not working):

Laravel pass parameters from controller to Model using 'With' clause

浪尽此生 提交于 2020-01-25 16:40:10
问题 I am new in Laravel, I want to pass the the $id from my controller in the model using with clause My model class Menucategory extends Model { protected $fillable = ['title', 'parent_id', 'restaurant_id']; // loads only direct children - 1 level public function children() { return $this->hasMany('App\Menucategory', 'parent_id'); } // recursive, loads all descendants public function childrenRecursive() { return $this->children()->with('childrenRecursive'); } } My Controller public function show

Laravel - Display data on page when textbox value is fetch in database

旧城冷巷雨未停 提交于 2020-01-25 08:31:07
问题 I have a page here which get all the value that has the same employee_no . here is my page. here in my page, see the textbox? that is the data that im fetching and that will be my basis when I fetch all the records that has the same number. so when i display that employee number, then all the data must be displayed. but on my case, no data is being displayed here take a look. whereas, the final output should be this. here is my view {!! Form::open(['action' => 'Admin\EmployeeFilemController

Laravel : Join within query using Eloquent

不想你离开。 提交于 2020-01-25 03:50:11
问题 I have a five tables in which i need to get data from all this. post,product,categories,attributes,post_attribute I have a query in my model that collect all post for a one product and it contains categories and attributes details . Query : Post::with(['product.categories.attributes'])->whereStatus("Active")->get(); Response: "PostDetails": [ { "id": 153, "user_id": 3, "product_id": 1, "demand_or_supply": "Demand", "description": "This is a post description three", "image_one": null, "image

How to get the max ID in a Join Query in Laravel 5 (Integrity constraint violation:)

微笑、不失礼 提交于 2020-01-24 15:25:31
问题 My query is like this: $deals=DB::table('leadsheet') ->join('Deal', 'leadsheet.leadcode', '=', 'Deal.leadcode') ->join('benefits', 'leadsheet.leadcode', '=', 'benefits.leadcode') ->join('delegatedealinfo', 'leadsheet.leadcode', '=', 'delegatedealinfo.leadcode') ->join('vipbooking', 'leadsheet.leadcode', '=', 'vipbooking.leadcode') ->where('id', DB::raw("(select max(`id`) from vipbooking)")) ->where('leadsheet.leadcat', '=','Delegates') ->get(); So I have following tables: 1.leadsheet --

Laravel hasOne through a pivot table

隐身守侯 提交于 2020-01-24 09:05:51
问题 So I have 2 models, User & Profile, the relationship is set up as follows: /** * User belongs to many Profile * * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ public function profiles() { return $this->belongsToMany('App\Models\Profile', 'user_profiles'); } I have 3 tables, users, profiles & user_profiles (the pivot table) I have a column in my users table called active_profile which gets populated with a profile id. How can I set up the relationship so that I can call

How to implement eloquent 'saving' event for all models , not individually

寵の児 提交于 2020-01-23 20:48:32
问题 Based on documentation of laravel eloquent event, all the eloquent event are triggered individually based on each model, is there any way to use 'creating' event or any other eloquent event to be triggered by all the Models For example , if any Models is created , event A is triggered 回答1: Listen for the Eloquent creating event that is fired. It is a 'string' event still not an object so you can do some ... wildcard matching here. This is the string of the events that are fired from Eloquent:

Returning the first model from a hasMany relationship in Laravel

孤者浪人 提交于 2020-01-23 01:21:18
问题 Is it possible to create a quick method to return the first model from a one-to-many relationship? Here is my code, from the model file: public function books() { return $this->hasMany('App\Models\Book'); } public function first_book() { return $this->book()->first(); } This is the error I'm getting: 'Call to undefined method Illuminate\Database\Query\Builder::addEagerConstraints()' The reason I want to use this is so that I can collect the first record using the with() method, for example:

Guide me implementing Oauth2 PHP server using thephpleague library

余生颓废 提交于 2020-01-23 01:14:09
问题 I am using Slim Framework With Eloquent ORM. Trying to implement https://github.com/thephpleague/oauth2-server but I am totally confused how to do this. After adding this with composer, I created database with sql file provided in this package. Now it is suggested to implement Storage interfaces. I don't wanna do this, So I just copied storage classes found in Example Folder. I guess they should work as I am using same database right? Also it is unclear how to initially seed the db. Here's my