eloquent

What is the difference between BelongsTo And HasOne in Laravel

不打扰是莪最后的温柔 提交于 2019-12-29 18:39:09
问题 Can any body tell me what is the main difference between the BelongsTo and HasOne relationship in eloquent . 回答1: BelongsTo is a inverse of HasOne. We can define the inverse of a hasOne relationship using the belongsTo method. Take simple example with User and Phone models. I'm giving hasOne relation from User to Phone. class User extends Model { /** * Get the phone record associated with the user. */ public function phone() { return $this->hasOne('App\Phone'); } } Using this relation, I'm

Model Inheritance in Laravel

江枫思渺然 提交于 2019-12-29 14:16:18
问题 i'm currently working with Laravel and I'm struggling with the fact that every model needs to extend from Eloquent and I'm not sure how to implement a model Hierarchy (with different tables) For Example: Let's say I have an abstract model Tool , and then a Model Hammer and a Model Screwdriver that extend from Tool. Now, Tool would extend Eloquent ... BUT, there is NO Table for Tools, there is a table for Hammers and another Table for Screwdrivers, because they have different attributes. How

Eloquent attach/detach/sync fires any event?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-29 13:19:47
问题 I have a laravel project, and I need to make some calculations immediately after I save a model and attach some data to it. Is there any event that is triggered in laravel after calling attach (or detach/sync)? 回答1: Steps to solve your problem: Create custom BelongsToMany relation In BelongsToMany custom relation override attach, detach, sync and updateExistingPivot methods In overriden method dispatch desired events. Override belongsToMany() method in Model and return your custom relation

Eloquent attach/detach/sync fires any event?

谁说胖子不能爱 提交于 2019-12-29 13:19:10
问题 I have a laravel project, and I need to make some calculations immediately after I save a model and attach some data to it. Is there any event that is triggered in laravel after calling attach (or detach/sync)? 回答1: Steps to solve your problem: Create custom BelongsToMany relation In BelongsToMany custom relation override attach, detach, sync and updateExistingPivot methods In overriden method dispatch desired events. Override belongsToMany() method in Model and return your custom relation

Eloquent attach/detach/sync fires any event?

空扰寡人 提交于 2019-12-29 13:19:03
问题 I have a laravel project, and I need to make some calculations immediately after I save a model and attach some data to it. Is there any event that is triggered in laravel after calling attach (or detach/sync)? 回答1: Steps to solve your problem: Create custom BelongsToMany relation In BelongsToMany custom relation override attach, detach, sync and updateExistingPivot methods In overriden method dispatch desired events. Override belongsToMany() method in Model and return your custom relation

Laravel 5 using OR condition with BETWEEN

醉酒当歌 提交于 2019-12-29 06:29:14
问题 Hi can anyone help me building below query in laravel Eloquent i am really confuse in using OR condition with between SELECT * FROM tbl WHERE existing_start BETWEEN $newSTart AND $newEnd OR $newStart BETWEEN existing_start AND existing_end i tried using like whereBetween('existing_start',[$newSTart,$newEnd]) but have no idea how to use OR 回答1: There is an orWhereBetween method available from the Query Builder, but it is undocumented in the Query Builder Documentation. You can however find it

Laravel 5 using OR condition with BETWEEN

喜你入骨 提交于 2019-12-29 06:28:05
问题 Hi can anyone help me building below query in laravel Eloquent i am really confuse in using OR condition with between SELECT * FROM tbl WHERE existing_start BETWEEN $newSTart AND $newEnd OR $newStart BETWEEN existing_start AND existing_end i tried using like whereBetween('existing_start',[$newSTart,$newEnd]) but have no idea how to use OR 回答1: There is an orWhereBetween method available from the Query Builder, but it is undocumented in the Query Builder Documentation. You can however find it

Extending Eloquent Models in Laravel (use different tables)

扶醉桌前 提交于 2019-12-29 03:39:33
问题 I’m building a Laravel application that involves tracking different types of leads. For example, there are Refinance leads and Purchase leads . Since the leads share a lot of information and functionality, but not all, my thinking was to create a Lead class, which extends Laravel’s Model class, and then a RefinanceLead class, which extends the Lead class. So I’d have: class Lead extends Model { // shared lead stuff } class RefinanceLead extends Lead { // stuff specific to refinance leads } My

Why is creating Foreign Key in Laravel 5.8 failing?

点点圈 提交于 2019-12-28 23:05:21
问题 The migration script below was running smoothly in an older version of Laravel but I added it to my fresh Laravel 5.8 and ran the script. I'm getting Error: foreign key was not formed correctly Evaluation Migration: public function up() { Schema::create('evaluation', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); }); } Users Migration:

Laravel Eloquent Sum of relation's column

冷暖自知 提交于 2019-12-28 05:01:07
问题 I've been working on a shoppingcart application and now I've come to the following issue.. There is a User, a Product and a Cart object. - The Cart table only contains the following columns: "id", "user_id", "product_id" and timestamps. - The UserModel "hasMany" Carts (because a user can store multiple products). - The CartModel "belongsTo" a User and CartModel "hasMany" Products. Now to calculate the total products I can just call: Auth::user()->cart()->count() . My question is: How can I