eloquent

Laravel - 1066 Not unique table/alias on a relationship

懵懂的女人 提交于 2020-05-15 22:44:16
问题 I'm trying to create a simple relationship between tables : - attribute_type - id name - category - id name description So I created a pivot table to link them : - attribute_type_category - attribute_type_id category_id There is the model relationships : On AttributeType.php public function category() { return $this->belongsToMany('App\AttributeTypeCategory', 'attribute_type_category', 'attribute_type_id', 'category_id'); } On AttributeTypeCategory.php public function category() { return

Laravel - 1066 Not unique table/alias on a relationship

三世轮回 提交于 2020-05-15 22:44:05
问题 I'm trying to create a simple relationship between tables : - attribute_type - id name - category - id name description So I created a pivot table to link them : - attribute_type_category - attribute_type_id category_id There is the model relationships : On AttributeType.php public function category() { return $this->belongsToMany('App\AttributeTypeCategory', 'attribute_type_category', 'attribute_type_id', 'category_id'); } On AttributeTypeCategory.php public function category() { 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 eloquent get the latest row of related table

冷暖自知 提交于 2020-05-13 06:06:24
问题 I have two tables, books , and chapters . One book has many chapters. Book model: public function chapters() { return $this->hasMany(Chapter::class); } Chapter model: public function book() { return $this->belongsTo(Book::class); } I want to get book list with their own latest chapter using single query like this: $books = Book::with(['authors', 'categories', 'chapters' => function($q) { $q->orderBy('updated_at', 'desc')->first(); }]->get(); But it doesn't work. Chapters return an empty array

How to call Stored Procedure with Eloquent (Laravel)?

血红的双手。 提交于 2020-05-11 17:47:45
问题 There is no standard procedure mentioned in documentation that how to call a stored procedure in MYSQL in laravel with Eloquent syntax. I mean is there any way to call stored procedure in mysql with help of eloquent syntax like $result = StoredProcedures::my_custom_procedure(param1, param2); return view('welcome')->with('result',$result); Is there any way to call and fetch results from stored procedures with pure eloquent syntax(purely eloquent way). Thanks. Small examples are highly

Laravel Eloquent - Get one Row

橙三吉。 提交于 2020-05-07 09:56:02
问题 This might be a simple question, but I cannot figure this out. I am trying to get a user by email using: $user = User::whereEmail($email)->get(); But this is returning an array (of dimension 1) of $users. So If I want to get the name, I have to do $user[0]['first_name'] . I tried using limit(1) or take(1) , or even using ->toArray() but there was no difference. What am I doing wrong? 回答1: Simply use this: $user = User::whereEmail($email)->first(); 回答2: You can do this too Before you use this

Laravel Eloquent - Get one Row

青春壹個敷衍的年華 提交于 2020-05-07 09:53:00
问题 This might be a simple question, but I cannot figure this out. I am trying to get a user by email using: $user = User::whereEmail($email)->get(); But this is returning an array (of dimension 1) of $users. So If I want to get the name, I have to do $user[0]['first_name'] . I tried using limit(1) or take(1) , or even using ->toArray() but there was no difference. What am I doing wrong? 回答1: Simply use this: $user = User::whereEmail($email)->first(); 回答2: You can do this too Before you use this

Laravel Eloquent no primary key

北城余情 提交于 2020-04-30 16:36:06
问题 I would like to update an old table that have a composite primary key. In order to generate a full update query such as: UPDATE foos SET (...) WHERE pk1 = ? AND pk2 = ?; Instead Eloquent tries to do the following which does massive update: UPDATE foos SET (...) WHERE pk1 = ?; I set the primary key attribute to null as seen on Google: class Foo extends Model { protected $guarded = []; protected $primaryKey = null; public $incrementing = false; public $timestamps = false; } But I get this error

Laravel Eloquent no primary key

▼魔方 西西 提交于 2020-04-30 16:35:35
问题 I would like to update an old table that have a composite primary key. In order to generate a full update query such as: UPDATE foos SET (...) WHERE pk1 = ? AND pk2 = ?; Instead Eloquent tries to do the following which does massive update: UPDATE foos SET (...) WHERE pk1 = ?; I set the primary key attribute to null as seen on Google: class Foo extends Model { protected $guarded = []; protected $primaryKey = null; public $incrementing = false; public $timestamps = false; } But I get this error