Home > database >  how to fix Undefined variable: data on laravel view
how to fix Undefined variable: data on laravel view

Time:06-23

I am trying to send variable with data from a function to laravel front page view and i get Undefined variable: data (View: C:\xampp\htdocs\laravelproject\resources\views\process-url.blade.php).

This is my code and web.php routes.

web.php

Route::get('/',  [welcomechecker::class, 'getfrontpage']);
Route::post('process-url',  [welcomechecker::class, 'saveformdata']);

welcomechecker.php controller

class welcomechecker extends Controller
{

    function getfrontpage(){
        return view('welcome'); 
    }

function saveformdata(Request $request){

$client = new Client();

$data = [];
    
  $url = $request->url;   //getting value from ajax       $url =   $request->url; 
 
  $wp = $url.'/'.'wp-admin';
 
  $website = $client->request('GET', $url);

 $html = $website->html();

//Check if cms is wordpress
 
$cms  = '';

if($this->isWordPress($wp, $html) == 'WordPress'){

$cms  = 'WordPress';

 $data['cms'] = 'WordPress';
}
return view('process-url', compact('data'));
}

view blade: process-url.blade.php

@foreach($data as $student)
    {{$student->cms}}
@endforeach

front page view blade: welcome.blade.php

<div  >
    @include('process-url')
</div>

application.js having ajax code

jQuery(document).ready(function(){

   $(document).ajaxStart(function(){
  $("#wait").css("display", "block");
  });
  $(document).ajaxComplete(function(){
  $("#wait").css("display", "none");
  });

          jQuery('#ajaxsubmit').click(function(e){
           e.preventDefault();
           
             jQuery('.alert').show();
           
           $.ajaxSetup({
              headers: {
                  'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
              }
          });
           jQuery.ajax({
              url: 'process-url',
             type: 'POST',
              data: {
                 url: jQuery('#enter_url').val()
               
              },
              success: function(result){
                  console.log(result);
                
                   jQuery('.display-content').html(result.success).fadeIn(700);
               
                // jQuery('.display-  content').html(result.success).fadeIn(700).fadeOut(5000);
              }});
           });
        });

Someone please help me to identify what i am doing wrong. I am trying to submit the variable data to the process-url blade view which is routed to the saveformdata() on the web.php. The error occurs in process-url.blade.php at the data variable. The saveformdata function has a whole lot of code than what i actually put here the whole idea is that it returns an array of data from a scraping tool which i want to display on a table on process-url blade

CodePudding user response:

Of course you will get Undefined Variable error when you are trying to include a blade file that is waiting for an array $data which is only passed to the view when you hit the route process-url. Also it is really, really bad practice to return a view after a POST request. Anyways, to solve your error (because that's what you actually want) you can do the following:

  1. Pass the $data to the welcome page view and remove it from the process-url view
    function getfrontpage(){
        return view('welcome', [
           'data' => ['Balloon Fight', 'Donkey Kong', 'Excitebike']
        ]); 
    }

    function saveformdata(){
        return view('process-url');
    }
  1. Pass the $data array from the welcome view to the process-url view through @include
<div  >
    @include('process-url', ['data' => $data])
</div>

Your error now disappeared. Your code still makes no sense, but this is what you wanted.

CodePudding user response:

In the first URL you show a welcome blade file and it includes process-url blade without data variable

you should pass data variable in getfrontpage function like saveformdata and the include directive pass variable to child blade

function getfrontpage(){
        $data = ['Balloon Fight', 'Donkey Kong', 'Excitebike'];
 
        return view('welcome', compact('data'));
    }

and data variable array type that student will show every element in the array

@foreach($data as $student)
    {{$student}}
@endforeach
  • Related