Jump to content

how can i fetch data from laravel-mysql using ajax?


Recommended Posts

//my controller
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class homeController extends Controller
{
    public function index() {
        
        $employee = DB::table('employee')->orderBy('id','desc')->get();
        
        $department = DB::table('department')->orderBy('id','desc')->get();

    	return view('index', ['employee' => $employee , 'department' => $department]);
    }
}

//my routes

Route::get('index','homeController@index');

//my view using blade temmplating engine
@foreach($employee as $emp)
<div class="employee">
  <b>{{ $emp->name }} </b>
  <a href="employee/{{ $emp->id }}">
	<p class="intro">{{ substr($emp->intro ,0, 50) }}...</p>   
  </a>
</div>
@endforeach

@foreach($department as $dep)
<div class="department">
  <b>{{ $dep->name }} </b>
  <a href="department/{{ $dep->id }}">
	<p class="desc">{{ substr($dep->description ,0, 100) }}...</p>   
  </a>
</div>
@endforeach

I want to fetch using ajax, how can i do it, teach/help me

Link to post
Share on other sites
//my controller
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class homeController extends Controller
{
    public function index() {
        
        $employee = DB::table('employee')->orderBy('id','desc')->get();
        
        $department = DB::table('department')->orderBy('id','desc')->get();
		//I added this line but the data came in the json format so how to pass it to the view with ajax but remember they are comming from different tables
    	return response()->json(array('employee' => $employee , 'department' => $department));
    }
}

-

Link to post
Share on other sites
Quote

I added this line but the data came in the json format so how to pass it to the view with ajax but remember they are comming from different tables

Where the data comes from is completely irrelevant. All the PHP has to do is return the correct JSON.

That part looks fine. How about the Javascript that makes the AJAX call? What have you tried for that?

Link to post
Share on other sites
On 12/13/2020 at 12:45 AM, requinix said:

Where the data comes from is completely irrelevant. All the PHP has to do is return the correct JSON.

That part looks fine. How about the Javascript that makes the AJAX call? What have you tried for that?

The above php return the correct JSON DATA, the issue here is how to pass them to the welcome.blade.php , i know this in the normal php and I was supposed to do the following

//the ajax in normal php
    $.ajax({

      type: "GET",
      url: "/homeController.php",       
      success: function (data) {
        $(".employee #name").html(data.name);
        $(".employee #intro").html(data.intro);

         $(".department #name").html(data.name);
        $(".department #desc").html(data.desc);
      }

    });

Help me i cant figure it in laravel

Link to post
Share on other sites
19 hours ago, maxxd said:

Your PHP is returning an array including 'employee' and 'department'. Your JavaScript is looking for 'name' (twice, so you might want to rethink that), 'intro', and 'desc'.

When am doing this;

$(".employee #name").html(data.employee.name);
$(".employee #intro").html(data.employee.intro);

         $(".department #name").html(data.department.name);
 $(".department #desc").html(data.department.desc);

 

 

Nothing work can you help me here, I don't want to change the field names 

Link to post
Share on other sites

Assuming your employee table includes a columns named 'name' and one named 'intro', and your department table includes the columns 'name' and 'desc', everything should be working. What does 'Nothing work' mean, exactly? What are you seeing now?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.