lara_net

Experience

210

0 Best Reply Awards

  • Member Since 6 Months Ago
  • 0 Lessons Completed
  • 0 Favorites

26th March, 2018

lara_net left a reply on Display Json From Database To View • 6 months ago

Database Table

public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->increments('id');
            $table->text('item');
            $table->timestamps();
        });
    }

Product Model

public function items() {
        return $this->hasMany(Item::class, 'prod_id');
  }

Item Model*

protected $cats = ['item' => 'array'];
public function item()
    {
        return $this->belongsTo(Product::class, 'prod_id');
    }

View

<input type="checkbox" name="item[]" value="item_1">
<input type="checkbox" name="item[]" value="item_2">
<input type="checkbox" name="item[]" value="item_3">
<button>Add Item</button>

Controller for save method

public function create (Request $request, Product $product){
    $items = new Item;
        {
         $item = json_encode(request('item'));
         $items->item = $item;
         $product->items()->save($items);
        }
}

lara_net left a reply on Display Json From Database To View • 6 months ago

@ekhlas Still it's saved as ["item_1", "item_2", "item_3"]

lara_net left a reply on Display Json From Database To View • 6 months ago

@rin4ik It's string. Because my MySQL does not support json column.

lara_net left a reply on Display Json From Database To View • 6 months ago

Can't I store ["item_1","item_2","item_3"] data on database and display on blade view later?

lara_net left a reply on Display Json From Database To View • 6 months ago

I understand. How can I display php ["item_1","item_2","item_3"] data in view?

lara_net left a reply on Display Json From Database To View • 6 months ago

shows ```php Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => ["small_size","medium_size","large_size"] ) ) ``` {{ $items }} outputs ```php ["[\"item_1\",\"item_2\",\"item_3\"]"]; ```

lara_net left a reply on Display Json From Database To View • 6 months ago

Means?

lara_net started a new conversation Display Json From Database To View • 6 months ago

I have a column that stores json data as ["Item_1", "Item_2", "Item_3"]. It has only one field - Items.

How can I display the json data on my blade view?

My Controller

public function show(Request $request, $id)
{
$items= Item::findorfail($id);
return view('display', compact('items));
}

**View Blade**
```php 
@foreach($items as $item)
{{ json_decode($item) }}
@endforeach

It gives error:

htmlspecialchars() expects parameter 1 to be string, array given

20th March, 2018

lara_net left a reply on Update Dynamic Form Fields In Database (Insert/Delete) • 6 months ago

Is shows

ErrorException Invalid argument supplied for foreach()

Are there any Events in Laravel, that I can be tracked and fire once the user clicked 'Update' button?

lara_net left a reply on Update Dynamic Form Fields In Database (Insert/Delete) • 6 months ago

I use [Bootstrap/jQuery Image uploader plugin] (https://github.com/egonolieux/bootstrap-imageupload).

lara_net left a reply on Update Dynamic Form Fields In Database (Insert/Delete) • 6 months ago

I use HTML submit button to update. From Laravel Update method, I want to update the data. (Question updated.)

lara_net left a reply on Update Dynamic Form Fields In Database (Insert/Delete) • 6 months ago

Ya, I'm not getting the updated number of rows in the database. However, I can update the exact number of rows that are saved in database. ie.

If I add one subject if there are 2 subjects already, that new row is not added.

lara_net started a new conversation Update Dynamic Form Fields In Database (Insert/Delete) • 6 months ago

I have a dynamic form where the user should be able to add or remove data while updating.

I am able to update the exact number fields that are in the database table. But what I want is if a user clicked a 'Delete Subject' and 'Update' buttons, I want that entire row deleted from the database.

And, if he added a subject by clicking 'Add another Subject' the form and clicked 'Update' I want those subjects added. What am I missing here?

My form Subject Screenshot

Students Model

protected $fillable = ['name', 'address'];
public function subjects() {
    return $this->hasMany(Subjects::class, 'stu_id');
}

Subjects Model

protected $fillable = ['stu_id', 'sub_code', 'sub_name', 'sub_img'];

public function subs(){
    return $this->belongsTo(Students::class, 'stu_id');
}

Update Method

public function update(Request $request, $id){
$stu=Students::FindOrFail($id)
$subjects = Subjects::with(['subs'])->where('stu_id', $stu->id)->get();
$stu->update($request->all());

$i=0;
foreach( $subjects as $new_subjects)
   {
    $sub_images =request()->file('sub_img');    
    $sub_name = request('sub_name');
    if(isset($sub_images[$i]))
      {
       $pathToStore = public_path('images');    
       if($request->hasFile('sub_img') && isset($sub_images[$i]))
         {
          $sub_img_name = uniqid() . '.' . $sub_images[$i]->getClientOriginalExtension();
           $sub_images[$i]->move(public_path('/images/'), $sub_img_name);            
           $new_subjects->sub_img = '/images/'.$sub_img_name;

           $new_subjects->sub_code = request('sub_code')[$i];
           $new_subjects->sub_name = request('sub_name')[$i];
           $new_subjects->sub_img = "images/{$sub_img_name}";
           $i++;
           $new->subjects()->save($new_subjects);
        }
      }
    }
  }

Subjects Database Subject Database

What am I missing?

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.