0

Explanation : here I have a button that when pressed will bring the view, well the problem can be seen I do a loop in the controller. I don't want to loop in controller but I want to loop it in view.blade.php, how do I do that?

BUTTON :

<button onclick="getData('{{ $workbench->material_number }}','{{ $workbench->inc }}')"></button>

SCRIPT AJAX :

function getData(argument, argument2) {
    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });


    $.ajax({
        url: '{{ url("/workbench/manufacture_ref/detail") }}/'+argument+"/"+argument2,
        type: 'POST',
        data: {material_number: argument},
        success: function(str) {
            $("#manref_detail").html(str);
        }
    })

ROUTE :

Route::post('/workbench/manufacture_ref/detail/{material_number}/{inc}', 'WorkbenchController@manref_detail');
enter code here

CONTROLLER :

public function manref_detail1($material_number) 
{
    if ( isset($_POST['material_number']) ) {

       $material_number = $_POST['material_number'];
       $i = DB::select("SELECT * FROM part_manufacture_ref WHERE material_number = '$material_number';");
       $token= md5(uniqid());

        foreach ($i as $value) {
            echo "<tr style='font-size: 12px' tabindex='0'>";
            echo "<th>$value->manufacture_code</th>";
            echo "<th>$value->source_type</th>";
            echo "<th>$value->manufacture_ref</th>";
            echo "<th>$value->type</th>";
            echo "<th>$value->note</th>";
            echo "</tr>";
           } 
    } else {
        echo "Access Denied";
    }
}

Please help me, Thanks.

Dewa Aditya
  • 79
  • 1
  • 2
  • 9

1 Answers1

0

Why dont you return your result as JSON?

public function manref_detail1($material_number) 
{
    if ( isset($_POST['material_number']) ) {
        $material_number = $_POST['material_number'];
        $i = DB::select("SELECT * FROM part_manufacture_ref WHERE material_number = '$material_number';"); //Why dont you use Eloquent?
        $token= md5(uniqid()); //Why do you have this line? $token is not used?
        return $i;
    } else {
        echo "Access Denied";
    }
}

(also, learn about validators in Laravel, way easier than the if/else https://laravel.com/docs/5.6/validation)

Then you have a JSON response in your View.

So,you will probably have an Array of materials.

$.ajax({
    url: '{{ url("/workbench/manufacture_ref/detail")}}/'+argument+"/"+argument2,
    type: 'POST',
    data: {material_number: argument},
    success: function(result) {
        // So something with result. You can foreach here or call an external function.
    }
})
Fjarlaegur
  • 1,395
  • 1
  • 14
  • 33
  • thank you for your response, after foreach in success function, how to throw the value of foreach in #manref detail. sorry i just learn, thank you. – Dewa Aditya Jul 27 '18 at 03:05
  • please help me brother @Clemenz – Dewa Aditya Jul 29 '18 at 10:45
  • You are constructing Table rows with the data right? Maybe you can take a look here: https://stackoverflow.com/questions/171027/add-table-row-in-jquery I hope that helps you. – Fjarlaegur Jul 30 '18 at 10:31