mahenda Posted December 11, 2020 Share Posted December 11, 2020 //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 Quote Link to comment https://forums.phpfreaks.com/topic/311846-how-can-i-fetch-data-from-laravel-mysql-using-ajax/ Share on other sites More sharing options...
requinix Posted December 11, 2020 Share Posted December 11, 2020 What have you done for the AJAX stuff so far? Quote Link to comment https://forums.phpfreaks.com/topic/311846-how-can-i-fetch-data-from-laravel-mysql-using-ajax/#findComment-1582963 Share on other sites More sharing options...
mahenda Posted December 12, 2020 Author Share Posted December 12, 2020 //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)); } } - Quote Link to comment https://forums.phpfreaks.com/topic/311846-how-can-i-fetch-data-from-laravel-mysql-using-ajax/#findComment-1582986 Share on other sites More sharing options...
requinix Posted December 12, 2020 Share Posted December 12, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311846-how-can-i-fetch-data-from-laravel-mysql-using-ajax/#findComment-1582997 Share on other sites More sharing options...
mahenda Posted December 14, 2020 Author Share Posted December 14, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311846-how-can-i-fetch-data-from-laravel-mysql-using-ajax/#findComment-1583051 Share on other sites More sharing options...
maxxd Posted December 14, 2020 Share Posted December 14, 2020 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'. Quote Link to comment https://forums.phpfreaks.com/topic/311846-how-can-i-fetch-data-from-laravel-mysql-using-ajax/#findComment-1583052 Share on other sites More sharing options...
mahenda Posted December 15, 2020 Author Share Posted December 15, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311846-how-can-i-fetch-data-from-laravel-mysql-using-ajax/#findComment-1583090 Share on other sites More sharing options...
maxxd Posted December 15, 2020 Share Posted December 15, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311846-how-can-i-fetch-data-from-laravel-mysql-using-ajax/#findComment-1583098 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.