eloquent

Laravel Eloquent no primary key

眉间皱痕 提交于 2020-04-30 16:35:27
问题 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

Flatten laravel nested relationship (parent to descendants)

﹥>﹥吖頭↗ 提交于 2020-04-30 14:08:19
问题 I have a parent children relationship on my model. What I want is to flatten all those collection including all those descendants of parent. [ { 'name': 'Parent', 'another_array' => [ { 'name': 'Another Array Name' } ], 'children': [ { 'name': 'First Child', 'address': 'lorem ipsum', 'contact_no': '92390', 'another_array' => [ { 'name': 'Another Array Name' } ], 'children': [ { 'name': 'First Descendant', 'address': 'lorem ipsum', 'contact_no': '92390', 'children': [ { 'name': 'Descendant

Flatten laravel nested relationship (parent to descendants)

眉间皱痕 提交于 2020-04-30 14:06:32
问题 I have a parent children relationship on my model. What I want is to flatten all those collection including all those descendants of parent. [ { 'name': 'Parent', 'another_array' => [ { 'name': 'Another Array Name' } ], 'children': [ { 'name': 'First Child', 'address': 'lorem ipsum', 'contact_no': '92390', 'another_array' => [ { 'name': 'Another Array Name' } ], 'children': [ { 'name': 'First Descendant', 'address': 'lorem ipsum', 'contact_no': '92390', 'children': [ { 'name': 'Descendant

Trying to get property 'likes' of non-object (View: /app/resources/views/products/productView.blade.php)

丶灬走出姿态 提交于 2020-04-30 11:06:32
问题 on my local server this is working and on hosting (when I push it on heroku) it gives me an error. I dont know where I am doing this wrong Trying to get property 'likes' of non-object (View: /app/resources/views/products/productView.blade.php) Query $findLikes = Comment::where('user_id', Auth::user()->id)->where('product_id', $id)->first(); View @if((Auth::check()) && ($findLikes->likes == '1')) <a href="/unlike-product/{{$p->id}}" id="myNumber" name="likes" value="0" onclick="myFunction()">

Method orderBy does not exist in Laravel Eloquent?

时间秒杀一切 提交于 2020-04-29 07:24:20
问题 I have a piece of code like this: $products = Product::all() if ($search_value) { $products = $products->where('name', 'LIKE', "%$search_value%"); } $products = $products->orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get(); I got the following error: BadMethodCallException in Macroable.php line 81: Method orderBy does not exist. I guess orderBy need to follow Product:: directly, but I can't save $products = Product:: , can I? Any suggestions? Thanks. 回答1: You're trying to

Method orderBy does not exist in Laravel Eloquent?

烂漫一生 提交于 2020-04-29 07:24:08
问题 I have a piece of code like this: $products = Product::all() if ($search_value) { $products = $products->where('name', 'LIKE', "%$search_value%"); } $products = $products->orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get(); I got the following error: BadMethodCallException in Macroable.php line 81: Method orderBy does not exist. I guess orderBy need to follow Product:: directly, but I can't save $products = Product:: , can I? Any suggestions? Thanks. 回答1: You're trying to

Multiple Foreign Key from the same table

旧巷老猫 提交于 2020-04-18 05:45:39
问题 I have a Laravel application with a MySQL database that has 3 tables: Publication , Comment and User . When I display a publication to user I call WS GET Publication By Id and WP GET Comment By Publication Id to display all comments related to that publication. I want to know if there's a way to avoid Calling WP GET User By Comment Id for each Comment, because when I display a comment I also need to display some information for user who commented. Can I add multiple user Foreign Keys in table

laravel eloquent relationship polymorphic

╄→гoц情女王★ 提交于 2020-04-17 20:37:12
问题 I have a polymorphic relationship, comments: (id, text, commentable_id, commentable_type ), images :(id, title) and videos :(id, title). videos table has a many to many relationship with matters table. I managed to recover the comments and their relationship such as the video but I cannot recover the title of the matter, passing from the comments table, after the videos table and then table of matters. below my request. class Comment extends Model { public function commentable() { return

Is it possible to work in a model with query builder using Laravel?

心不动则不痛 提交于 2020-04-17 20:36:11
问题 I would like to know if is it possible to work in a model using query builder to make a join between two tables, I don't want to use eloquent this is my model <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use DB; class Tbl_Perimetro extends Model { puplic function perimetros(){ $carteras = DB::table('tbl_perimetros') ->join('tbl_equipo_postventaatcs', 'tbl_equipo_postventaatcs.id', '=', 'tbl_perimetros.postventaatc_id') ->join('tbl_lista_carteras', 'tbl_equipo

Displaying pivot table data

柔情痞子 提交于 2020-04-16 04:02:14
问题 I am working on a library application, and I want to create a function, where the user can check out a book to a customer. So I have 2 tables, books and readers and I have created a pivot table, called book_reader as well to create the checkout method. However, I have some problems displaying the data from this table. I have read several articles about this, but they don't take in consideration that there is some data in the pivot table that contains new information and isn't just the joining