eloquent

Laravel get data based on foreign key

邮差的信 提交于 2021-01-27 17:21:22
问题 So I have 2 tables, TABLE1 and TABLE2. They are linked throug Eloquent: TABLE1 has many TABLE2's, and TABLE2 has one TABLE 1. They are linked with foreign keys as such: in TABLE2 table1_id -> links to -> TABLE1 id. How do I retrieve the entire table data or TABLE 2, + replace table1_id with a column of table1 (let's say by booktitle for example)? Sorry for the abstraction, but this is the best way I can explain it? :D 回答1: The easiest way is to make use of Eloquent's eager loading and fetch

After Search Export to PDF in Laravel?

半世苍凉 提交于 2021-01-25 06:57:10
问题 I have search data of the school now I like to export the searched only data into PDF. It is easy to send all just write $school=School::all() , but I like to send not all only searched data to PDF. For more information please see the attached pictures. all Schools and it information Once I searched I get this page: searched page Here is the code in Laravel controller. Search function: public function search(Request $request) { $camp_id=$request->camp_id; $school_level=$request->school_level;

After Search Export to PDF in Laravel?

拜拜、爱过 提交于 2021-01-25 06:56:29
问题 I have search data of the school now I like to export the searched only data into PDF. It is easy to send all just write $school=School::all() , but I like to send not all only searched data to PDF. For more information please see the attached pictures. all Schools and it information Once I searched I get this page: searched page Here is the code in Laravel controller. Search function: public function search(Request $request) { $camp_id=$request->camp_id; $school_level=$request->school_level;

Pagination with many to many relationships

只愿长相守 提交于 2021-01-24 07:20:24
问题 The bounty expires in 3 days . Answers to this question are eligible for a +50 reputation bounty. curved wants to draw more attention to this question. I have products , categories and category_product (pivot) tables in my database. I want to return the category info and products with pagination in the same response . I know I can use Product::with('categories')->pagination(20) , but I don't want to attach the category to each product. What is the proper way to get products belong to a

Laravel 5.3 Schema::create ENUM field is VARCHAR

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-23 08:53:46
问题 I just created fresh migration. After running it I see my field type not ENUM type . It has a VARCHAR(255) type instead Schema::create('payments', function (Blueprint $table) { $table->increments('id'); $table->text('response'); $table->enum('type', ['apple', 'paypal']); $table->smallInteger('flags'); $table->timestamps(); }); Can somebody tell me what can be the reason. Am I missing something, I tried multiple times - getting same result. I'm using PostgreSQL 9.5.4 回答1: From the Laravel

Laravel eloquent does not update JSON column : Array to string conversion

时光毁灭记忆、已成空白 提交于 2021-01-21 07:18:49
问题 I want to update a JSON column in my database but I get this error : Array to string conversion I have declared the column name as array in the model : protected $casts = [ 'destinations' => 'array' ]; this is the code that I use : $data[] = [ 'from' => $fromArray, 'to' => $toArray ]; Flight::where('id', $id)->update(['destinations' => $data]); What should I do ? 回答1: According to this conversation on Github : Make json attributes fillable if Model field is fillable Taylor Otwell recommande

Laravel Get ancestors (URL)

≡放荡痞女 提交于 2021-01-20 05:57:59
问题 In Laravel, I have a table which contains id, parent_id, slug (Self-referring), When I have an ID, I need to get all its ancestors in a format like this (Separated by "/"). level1/level2/level3 But in an efficient way without a package like "laravel-nestedset ". I have implemented it like this. public function parent() { return $this->belongsTo('Collection', 'parent_id'); } public function getParentsAttribute() { $parents = collect([]); $parent = $this->parent; while(!is_null($parent)) {

Laravel Get ancestors (URL)

瘦欲@ 提交于 2021-01-20 05:55:59
问题 In Laravel, I have a table which contains id, parent_id, slug (Self-referring), When I have an ID, I need to get all its ancestors in a format like this (Separated by "/"). level1/level2/level3 But in an efficient way without a package like "laravel-nestedset ". I have implemented it like this. public function parent() { return $this->belongsTo('Collection', 'parent_id'); } public function getParentsAttribute() { $parents = collect([]); $parent = $this->parent; while(!is_null($parent)) {

Laravel get class name of related model

半城伤御伤魂 提交于 2021-01-16 05:08:13
问题 In my Laravel application I have an Faq model. An Faq model can contain many Product models, so the Faq class contains the following function: class Faq extends Eloquent{ public function products(){ return $this->belongsToMany('Product'); } } In a controller, I would like to be able to retrieve the class name that defines the relationship. For example, if I have an Faq object, like this: $faq = new Faq(); How can I determine the class name of the relationship, which in this case would be

Laravel get class name of related model

拟墨画扇 提交于 2021-01-16 05:07:29
问题 In my Laravel application I have an Faq model. An Faq model can contain many Product models, so the Faq class contains the following function: class Faq extends Eloquent{ public function products(){ return $this->belongsToMany('Product'); } } In a controller, I would like to be able to retrieve the class name that defines the relationship. For example, if I have an Faq object, like this: $faq = new Faq(); How can I determine the class name of the relationship, which in this case would be