lara_net

lara_net

Member Since 1 Year Ago

Experience Points
210
Total
Experience

4,790 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
210 XP
Mar
26
1 year ago
Activity icon

Replied to Display Json From Database To View

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);
        }
}
Activity icon

Replied to Display Json From Database To View

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

Activity icon

Replied to Display Json From Database To View

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

Activity icon

Replied to Display Json From Database To View

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

Activity icon

Replied to Display Json From Database To View

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

Activity icon

Replied to Display Json From Database To View

print_r($sizes); shows

 Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => ["item_1","item_2","item_3"] ) ) 

{{ $items }} outputs

["[\"item_1\",\"item_2\",\"item_3\"]"]; 
Activity icon

Started a new conversation Display Json From Database To View

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

@foreach($items as $item)
 {{ json_decode($item) }}
@endforeach

It gives error:

htmlspecialchars() expects parameter 1 to be string, array given
Mar
20
1 year ago
Activity icon

Replied to Update Dynamic Form Fields In Database (Insert/Delete)

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?

Activity icon

Replied to Update Dynamic Form Fields In Database (Insert/Delete)

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

Activity icon

Replied to Update Dynamic Form Fields In Database (Insert/Delete)

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.

Activity icon

Started a new conversation Update Dynamic Form Fields In Database (Insert/Delete)

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 Edit View

<form accept-charset="UTF-8" action="{{action('[email protected]', $id)}}" enctype="multipart/form-data" method="post" files="true">
<label>Student Name:</label> <input type="text"  name="name">
<label>Student Address:</label> <input type="text" name="name">
<label>Student Subject Feature</label>

@foreach($subjects as $sub)
<input type="text" name="sub_code[]" value="{{ $sub->sub_code  }}" placeholder="Subject Code">
<input type="text" name="sub_name[]" value="{{ $sub->sub_name  }}" placeholder="Subject Name">
<input type="file" name="sub_img[]">
<a href="#" class="remove_field">Delete This Subject</a>
@endforeach
<button class="add_more">Add Subject</button>

<button type="submit">Update</button>

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? Should I use other method for UpSert?