How to Retrieving All Records From Users Table, You may use the table method on the DB facade to begin a query. will get the results using the get method.
Compact function in Laravel
We mostly use compact in Laravel to send the values to the view. Something like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show a list of all of the application's users.
*
* @return Response
*/
public function index()
{
$users = DB::table('users')->get();
return view('user.index', ['users' => $users]);
}
}
The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP stdClass object.foreach ($users as $user) {
echo $user->name;
echo $user->email;
}
Compact function in Laravel
We mostly use compact in Laravel to send the values to the view. Something like this:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class CategoryController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { $categories = DB::table('categories')->get(); return view('admin.pages.category.view',compact('categories')); } }
Laravel expects an array to be passed to the view helper function. Second argument in view helper function is an array that where keys are the names of the variable and the value are the contents of those variables. These variables will be available in our views to be used.
No comments:
Post a Comment