fetch data laravel code example

Example 1: how to get data from a table in laravel

use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    public function index()
    {
        $users = DB::table('users')->select('id','name','email')->get();

        return view('some-view')->with('users', $users);
    }
}

Example 2: How to request and display data from db in larave

$users = DB::table('users')->get();
foreach($users as $key => $value)
{
	//to get each columns value
	//$value->name
}

Example 3: laravel fetch post

fetch(url, {   
      	headers: {     
          "Content-Type": "application/json",     
          "Accept": "application/json, text-plain, */*",     
          "X-Requested-With": "XMLHttpRequest",     
          "X-CSRF-TOKEN": token    
        },   
  		method: 'post',
  		credentials: "same-origin",
  		body: JSON.stringify({     name: name,     number: number   })  })
  	.then((data) => {
      		form.reset();       
      		window.location.href = redirect;   
    })  
  	.catch(function(error) {      
      console.log(error);    
    });  
}

Tags:

Php Example