laravel-eloquent

Getting rank of row in OrderBy desc Eloquent query, How can i make this query work in laravel 5.5 eloquents?)

Deadly 提交于 2020-08-23 07:01:39
问题 I am trying to give my users a ranking number for my Laravel hiscores pagination table. This is the MySQL query I found working. I am trying to put make this work as a Laravel eloquent query. select @i := @i + 1 ranking, t.* from (select @i:=0) initvars, users t ORDER BY wins DESC; My Laravel eloquent query right now: $ranking = User::orderBy('wins', 'DESC')->paginate(10); Thanks in advance! 回答1: A bit of a late response but I figured it out. This is my solution. I First added this function

laravel Eloquent ORM delete() method

佐手、 提交于 2020-07-16 16:47:47
问题 Hi I am studying laravel. I use Eloquent ORM delete method but I get a different result.Not true or false but null. I set an resource route and there is a destroy method in UsersController. public function destroy($id){ $res=User::find($id)->delete(); if ($res){ $data=[ 'status'=>'1', 'msg'=>'success' ]; }else{ $data=[ 'status'=>'0', 'msg'=>'fail' ]; return response()->json($data); But I always get a response {"status":"0","msg":"failed"},the record in the database is deleted. Then I use dd(

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given

强颜欢笑 提交于 2020-05-14 19:51:25
问题 i am trying to post title and article in laravel rest api i am getting this error Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given, called in C:\xampp\htdocs\LaravelProject\cpapi\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 1440 This is my route/api.php file of post article data Route::post('articles', 'ArticleController@store'); Route::post('articles', function(Request $request) { return

Optimizing code with chunk or cursor in laravel

老子叫甜甜 提交于 2020-05-13 07:51:27
问题 I'm having Company Model and Contact Model defined in my Laravel 5.4 application, both of them have many to many relationship. So, for example contacts model has: public function company() { return $this ->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps(); } Now I've a data set where I want to pull all contacts data and there company details so I was using: public function getData() { $contacts = Contact::all(); foreach($contacts as $contact) {

Optimizing code with chunk or cursor in laravel

别等时光非礼了梦想. 提交于 2020-05-13 07:51:05
问题 I'm having Company Model and Contact Model defined in my Laravel 5.4 application, both of them have many to many relationship. So, for example contacts model has: public function company() { return $this ->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps(); } Now I've a data set where I want to pull all contacts data and there company details so I was using: public function getData() { $contacts = Contact::all(); foreach($contacts as $contact) {

Laravel 5.2 - Left Join DB::Raw not working?

被刻印的时光 ゝ 提交于 2020-05-09 05:58:49
问题 I have the following query where I'm trying to use DB::Raw() for the left join but I'm getting error: Missing argument 2 for Illuminate\Database\Query\Builder::leftJoin() This is my query: return $this->model->from('alerts as a') ->leftJoin(DB::Raw("locations as l on l.id = JSON_UNQUOTE(JSON_EXTRACT(a.criteria, '$.locationId'))")) ->leftJoin(DB::Raw("industries as i on find_in_set(i.id, JSON_UNQUOTE(JSON_EXTRACT(a.criteria, '$.industries')))")) ->where('user_id', '=', $userId) ->selectRaw("a

Laravel 5. Composite primary key. Unknown column 'id' in 'where clause'

不羁岁月 提交于 2020-04-18 06:04:31
问题 I don't use 'id' column in DB. Instead, I use a composite primary key user_id + tmdb_id . If I add new record like this: $movie = new Movie(); $movie->user_id = 1; $movie->tmdb_id = 2; $movie->ratio = 3; $movie->save(); it works fine! But if I try to edit an existing record like this: $movie = Movie::where([ 'user_id' => 1, 'tmdb_id' => 2, ])->first(); $movie->ratio = 4; $movie->save(); Then I have the error: Unknown column 'id' in 'where clause'. The migration file looks like this: public

Relationship not being passed to notification?

♀尐吖头ヾ 提交于 2020-03-06 04:38:26
问题 I created a notification that I am passing a model to: class NewMessage extends Notification implements ShouldQueue { use Queueable; protected $message; public function __construct(Message $message) { $this->message = $message; } public function via() { return ['database']; } public function toArray() { Log::info($this->message); return [ 'message_id' => $this->message->id, ]; } } And this is how I call the notification: $message = Message::where('user_id', Auth::user()->id) ->where('message

How can fetching huge records using Laravel and MySQL?

白昼怎懂夜的黑 提交于 2020-02-03 01:42:07
问题 I Need experts Suggestions and Solutions. We are developing job portal website here by handle around 1 million records. We are facing records fetching timeout errors. How can I handle those records using laravel and MySql? We are trying to follow steps: Increase the PHP execution time MySql Indexing Paginations 回答1: You should be chunking results when working with large data sets. This allows you to process smaller loads, reduces memory consumption and allows you to return data to the User

How can I add, delete and get a favorite from product with polymorphic relationship, in Laravel 5.6?

只愿长相守 提交于 2020-01-21 20:09:08
问题 My product model like this : <?php ... class Product extends Model { ... protected $fillable = ['name','photo','description',...]; public function favorites(){ return $this->morphMany(Favorite::class, 'favoritable'); } } My favorite model like this : <?php ... class Favorite extends Model { ... protected $fillable = ['user_id', 'favoritable_id', 'favoritable_type']; public function favoritable() { return $this->morphTo(); } } My eloquent query laravel to add, delete and get like this : public