eloquent

Subtract 2 Columns from different tables

泪湿孤枕 提交于 2021-01-29 08:47:57
问题 I'm trying to build a query where 2 columns from different tables get subtracted. This is what I tried: DB::connection('lab_inv')->where('tab2'.'Amount_Run', '=', 'tab1'.'Amount')->selectraw('tab1.Amount - tab2.Amount_Run'); The first table has the value ‘Amount’ which is unique for each id, the second table is bound by a foreign key to the id of the first table and has the parameter ‘Amount_Run’ which is also different depending on the id. Amount = Amount – Amount_run. Any Idea what I’m

How to update image with PUT method in Laravel REST API?

耗尽温柔 提交于 2021-01-29 08:31:09
问题 I am trying to build a REST API with Laravel where users need to update their image. But, If I use PUT method in postman it does not update the image or store the image in designated folder. If I use POST instead it does update the image and also saves in the folder. But it does not save the file in database as I customize the name to store in DB. What could be the reason. Help is really necessary. TIA. Update Code public function update(Request $request, $id) { $found = Partner::find($id);

How to make a Migration file based on a model in Laravel

天大地大妈咪最大 提交于 2021-01-29 07:13:20
问题 What if I have some model, with non-default features (like soft delete or custom id name) and want to create a Migration file from it, which would have all these properties in it? So here is my model: class Test extends Model { use SoftDeletes; protected $primaryKey = 'test_id'; protected $table = 'my_flights'; protected $dates = ['deleted_at']; } And I want my Migration file to be based on it. But when I use command php artisan make:migration create_test(s)_table (I tried both test and tests

Laravel conditionally add global scopes after method boot

好久不见. 提交于 2021-01-29 06:50:15
问题 Is it possible to add a global scope after the model has already passed the constructor? I would like to catch an event (for example creating ) and attach a global scope only if the model is creating. So far I've done: Event::listen('eloquent.creating*', function ($event, $model) { /** * @var Model $eloquentModel */ $eloquentModel = $model[0]; $eloquentModel->addGlobalScope(new AuthorizationScope($event)); }); The scope constructor is called, but the apply method from it is never fired

Laravel - Filter a collection by a dynamic field

可紊 提交于 2021-01-29 05:43:53
问题 The model has 3 fields that are attributes of the model and the 4th field is a dynamic field that is called "fullTitle" [ { "id": 1, "hours": 2000, "car_id": 3, "fullTitle": "Test", } } <?php namespace App; use App\Car; class VenderModel extends Model { use TranslationTrait; protected $guarded = ['*']; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = [ 'fullTitle' ]; /** * Accessor for dynamic property fullTitle to use as selector name. * @param

Invalid input syntax for type timestamp: “NOW() - INTERVAL '12 hours'”

人盡茶涼 提交于 2021-01-29 05:31:38
问题 I'm trying to make a query based on a field ``time` to get the last week data: Measure::where('time', '>', "NOW() - INTERVAL '12 hours'")->get(); but I can't make it work. I get this message: SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type timestamp: "NOW() - INTERVAL '12 hours'" (SQL: select * from "measures" where "time" > NOW() - INTERVAL '12 hours') Thing is I get this condition from official docs of timescaleDB, supposed to be postgres compatible: SELECT

How to add fields to a model via eloquent in Laravel?

不问归期 提交于 2021-01-29 04:56:44
问题 My question is if it is possible to add all the fields directly to a new model via Eloquent. I guess it would be something like php artisan make:model MyModel --fields=? However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated. Thanks in advance 回答1: If you mean table's column by fields then: Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides,

Sum of rows and groupby using eloquent

笑着哭i 提交于 2021-01-28 22:28:32
问题 Having the following columns: category, rating, num_users, date_voted /*number of users*/ How do I get the rating grouped by category as the sum of the number of users over a given range of dates using eloquent Category::groupBy('num_users') //returns the categories Example of the Main table data -------------------------------- category| rating| num_users| date_voted --------------------------------------- Action |5 |2 | 12-12-2015 Comedy |2 |2 | 12-12-2015 Fantasy |3 |2 | 12-12-2015 Action

Get all posts and check if user liked each post

て烟熏妆下的殇ゞ 提交于 2021-01-28 21:55:11
问题 I'm trying to create an Eloquent query that fetches all posts and checks if the user liked each of those posts. I have the following models: Post.php class Post extends Eloquent { /** * The database table used by the model. * * @var string */ protected $table = 'posts'; public function user() { return $this->belongsTo('User'); } public function likes() { return $this->hasMany('Like'); } } Like.php : class Like extends Eloquent { /** * The database table used by the model. * * @var string */

Route is redirecting to wrong page

杀马特。学长 韩版系。学妹 提交于 2021-01-28 21:13:20
问题 I'm working on Laravel 5 and I've set up the routes for all the pages. I have a dashboard that has certain icons which when clicked redirects to different pages. Now, this particular icon "Total Applications" is when clicked doesn't redirect to the page associated with it, i.e., total.blade.php but instead redirects to another page candidate.blade.php. I'm new to Laravel. I want that when I click the "Total Applications" page it should redirect to the page designated for it. I'm sharing the