Laravel5 default pagination - laravel-5

I have an error when simply put this
->paginate(15);
what is wrong with my code below
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Order;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator as Paginator;
class OrderController extends Controller {
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$orders = Order::with('customer')->get()->paginate(5);
return view('order.index', compact('orders'));
}
}
appreciate if you someone can highlight to me what have I done wrong?

Remove the get() when you use paginate()
$orders = Order::with('customer')->paginate(15);

Related

Policies Laravel not sending variable to controller

I'm new at Laravel, and I'm trying to make Policies that will prevent user that doesn't have id_level 1 which is admin to access InventarisController, but the InventarisPolicy doesn't send variable to InventarisController.
it's my Inventaris Policies
InventarisPolicy.php
<?php
namespace App\Policies;
use App\{User, Level};
use Illuminate\Auth\Access\HandlesAuthorization;
class InventarisPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* #return void
*/
public function __construct()
{
//
}
public function inventaris_add(User $user)
{
$user->id_level == 1;
// dd($user);
// $user->id_level == 2;
}
}
it's my Inventaris Controller
InventarisController.php
<?php
namespace App\Http\Controllers;
use App\{Inventaris, DetailPinjamanView};
// use Illuminate\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
// use App\Http\Controllers\Auth\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class InventarisController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// $viewpinjaman = DetailPinjamanView::all();
$this->authorize('inventaris_add', $user);
$inventaris = Inventaris::all();
return view('index', compact('inventaris'));
}

Accessing the user object in a base controller in Laravel 7?

I have a base controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Auth;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct()
{
$user = Auth::user();
if (!empty($user))
{
$uid = $user->id;
DB::table("users")
->where("id", $uid)
->update(array(
'last_time_page_accessed' => time(),
));
}
}
}
The problem is, even if I am logged in, it doesn't reach the $uid = $user->id part, because the $user object is always empty.
This is how I call it from the main controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Redirect;
use Auth;
use DB;
use App\Files;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
class ChatController extends Controller
{
public function __construct()
{
parent::__construct();
$this->middleware('auth');
}
}
What am i missing? Why can't it access it?
UPDATE
I tried switching these, and it also didn't work:
public function __construct()
{
$this->middleware('auth');
parent::__construct();
}
As apokryfos pointed out, the authenticated user is not yet available when the controller is constructed, so auth()->user() will always be null.
What you're trying to do is a perfect use case for Middleware.
You can use something like this. If you want this code to run for every route, you may register your Middleware as global Middleware for every web route.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class StoreLastTimeUserAccessedPage
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param string|null $guard
* #return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($user = Auth::user()) {
$user->update([
'last_time_page_accessed' => time()
]);
}
return $next($request);
}
}

Passing a collection to a mailable| Laravel 5.4

I am trying to get a mailable setup which has a collection of files. Mail controller looks like:
<?php
namespace App\Mail;
use App\Document;
use App\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
class OrderComplete extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $order;
public $documents;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(User $user, Order $order, Document $document)
{
//
$this->user = $user;
$this->order = $order;
$this->documents = $document;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('emails.customers.complete');
}
}
Controller calling the mailable looks like;
use App\Document;
// other code...
$documents = Document::where('order_id', $orderId)
->where('product', 'like', '%response')
->get();
Mail::to($customer)
->send(new OrderComplete($customer, $order, $documents));
But I keep getting this error:
Type error: Argument 3 passed to App\Mail\OrderComplete::__construct() must be an instance of App\Document, instance of Illuminate\Database\Eloquent\Collection given, called in /Users/ap/sites/propair/app/Http/Controllers/OrderController.php on line 253
I'm pretty confused as I thought this should work?
thanks
This function declaration:
public function __construct(..., Document $document)
means PHP will enforce that $document is an instance of App\Document.
If you want to pass it a collection instead, you'll need to do:
public function __construct(..., \Illuminate\Database\Eloquent\Collection $documents)

how to get current logged-in user details in laravel 5.2?

how do i get current logged in user details in laravel 5.2 ? I have done something to get the user name but it doesn't work properly.
Here is the code that gets the current logged-in user name
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Illuminate\Contracts\Auth\User;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\AuthController;
class UserProfileController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function current_user()
{
if(\Auth::check() && \Auth::user()->name) {
return 'hello';
return user()->name;
}
}
}
Here If i just return a ** return 'Hello'** in the function current_user , it works fine after checking the if condition, so i see there is no problem with if condition. But when i return user()->name it says
Call to undefined function App\Http\Controllers\user()
To access authorized user information use \Auth::user(). You probably just missing \Auth:: part, because You using correct syntax in condition.
You need to change your code a little bit.
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Illuminate\Contracts\Auth\User;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\AuthController;
class UserProfileController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function current_user()
{
if(\Auth::check() && \Auth::user()->name) {
return 'Hello '.\Auth::user()->name; //this line changed
}
}
}
After that you can get the current user name.
You want to return 2 values, that's not possible. When you return something your method will stop running. So that's why you didn't get the user name.
Hope this works!

Laravel pass object to view

When I'm trying to pass an object through a view with the AppServiceProvider it gives an error an says
Trying to get property of non-object
This is currently my App\Providers:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\User;
use Auth;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$users = Auth::user();
view()->share('user','users');
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
And when I say in the view:
{!! user->name !!}
It throws an error.
you are missing the '$' on users when passing the variable to view. So it can't be rendered on the view.
it must be:
$users = Auth::user();
view()->share('user', $users);

Resources