laravel-eloquent

Sorting a Laravel Collection by Many to Many Pivot Table Column

时光怂恿深爱的人放手 提交于 2021-02-10 12:45:03
问题 I'm looking at displaying a list of asset s belonging to a product in an order defined by order_column on the pivot table product_asset . In this case, the sortBy function has no effect. No errors are thrown, but it returns the collection array in the same order no matter what. Here's what I have laid out currently: Database: Schema::create('product_asset', function (Blueprint $table) { $table->integer('product_id')->unsigned()->index(); $table->foreign('product_id')->references('id')->on(

Display in VIEW of One to One relation in Laravel Relationship

白昼怎懂夜的黑 提交于 2021-02-10 11:52:35
问题 I am here again with a trouble understanding the correct way of doing Laravel Relationships I have this User Model public function concessionaire() { return $this->hasOne('App\Concessionaire', 'meternum', 'meternum'); } and Concessionaire Model public function user() { return $this->belongsTO('App\User', 'meternum', 'meternum'); } But when I try to display it in my view. the concessionaire data fields does not display.. In my Controller I have this $dataUser = User::where('usertype', '=',

Looping through collections -Laravel

耗尽温柔 提交于 2021-02-05 08:58:26
问题 I was trying to loop through a collection based on the key What I am trying to accomplish here is to group each company based on the alphabet in my view. How do I loop through this?? $results = $companies->sortBy('name')->groupBy(function ($item, $key) { return substr($item['name'], 0, 1); }); dump($results); Controller 回答1: As an alternative to @msonowal's answer you can also use each(): $results->each(function ($collection, $alphabet) { dump($alphabet, $collection); }); However, if you're

Looping through collections -Laravel

旧时模样 提交于 2021-02-05 08:58:06
问题 I was trying to loop through a collection based on the key What I am trying to accomplish here is to group each company based on the alphabet in my view. How do I loop through this?? $results = $companies->sortBy('name')->groupBy(function ($item, $key) { return substr($item['name'], 0, 1); }); dump($results); Controller 回答1: As an alternative to @msonowal's answer you can also use each(): $results->each(function ($collection, $alphabet) { dump($alphabet, $collection); }); However, if you're

laravel eloquent relation one to many returns null

烂漫一生 提交于 2021-02-04 21:14:25
问题 The product data always return null when i get the incoming_goods data (belongsTo). Here is my Product Model: <?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Product extends Model { use SoftDeletes; protected $guarded = [ 'id', 'created_at', 'updated_at', 'deleted_at', ]; public function transaction_details() { return $this->hasMany('App\Transaction_detail'); } public function incoming_goods() { return $this->hasMany('App

how to retrive data from multiple table in laravel eloquent

自古美人都是妖i 提交于 2021-01-27 06:47:55
问题 Model relation --------------------- language.php ---- public function attributeDetail() { return $this->hasMany(AttributeDetail::class, 'language_id'); } attribute.php ---- public function attributeDetail() { return $this->hasMany(AttributeDetail::class, 'attribute_id'); } attributeDetail.php ---- public function language() { return $this->belongsTo(Language::class); } public function attribute() { return $this->belongsTo(Attribute::class); } I want to show the json object like this {

how to retrive data from multiple table in laravel eloquent

穿精又带淫゛_ 提交于 2021-01-27 06:46:36
问题 Model relation --------------------- language.php ---- public function attributeDetail() { return $this->hasMany(AttributeDetail::class, 'language_id'); } attribute.php ---- public function attributeDetail() { return $this->hasMany(AttributeDetail::class, 'attribute_id'); } attributeDetail.php ---- public function language() { return $this->belongsTo(Language::class); } public function attribute() { return $this->belongsTo(Attribute::class); } I want to show the json object like this {

use distinct with multiple columns in laravel

萝らか妹 提交于 2020-12-30 08:56:31
问题 my table structure is as below date seller unit price total 05-06-17 abc 14 700 9800 05-06-17 pqr 12 600 7200 05-06-17 abc 10 520 5200 06-06-17 abc 10 600 6000 06-06-17 pqr 15 520 7800 06-06-17 pqr 16 520 8320 I need to fetch record like 1) 'date' = '05-06-2017', 'seller' = 'abc', 'total' = '15000' 2) 'date' = '05-06-2017', 'seller' = 'pqr', 'total' = '7200' 3) 'date' = '06-06-2017', 'seller' = 'abc', 'total' = '6000' 4) 'date' = '06-06-2017', 'seller' = 'pqr', 'total' = '16120' I need data

use distinct with multiple columns in laravel

痴心易碎 提交于 2020-12-30 08:55:01
问题 my table structure is as below date seller unit price total 05-06-17 abc 14 700 9800 05-06-17 pqr 12 600 7200 05-06-17 abc 10 520 5200 06-06-17 abc 10 600 6000 06-06-17 pqr 15 520 7800 06-06-17 pqr 16 520 8320 I need to fetch record like 1) 'date' = '05-06-2017', 'seller' = 'abc', 'total' = '15000' 2) 'date' = '05-06-2017', 'seller' = 'pqr', 'total' = '7200' 3) 'date' = '06-06-2017', 'seller' = 'abc', 'total' = '6000' 4) 'date' = '06-06-2017', 'seller' = 'pqr', 'total' = '16120' I need data