I'm trying to create a callback to return my views based on data from my current logged-in user. If I do something basic like echoing 'hi' it works, is there any way accomplish this?
function checkUser($type,$callback){
if( is_callable($callback) ){
call_user_func($callback);
}
}
class FichaController extends Controller
{
public function contarFichas()
{
checkUser('particular',function(){
$currentUser = Auth::user();
$countFichas = Ficha::where('user_id',$currentUser->id)->count();
return view('particular.index', array('countFichas' => $countFichas));
});
}
}
Return the result from checkUser
if( is_callable($callback) ){
return $callback();
}
public function contarFichas()
{
return checkUser('particular',function(){
$currentUser = Auth::user();
$countFichas = Ficha::where('user_id',$currentUser->id)->count();
return view('particular.index', array('countFichas' => $countFichas));
});
}
Related
I have few $request->user->load() relationships :
Route::middleware(['auth:api','request.log'])->get('/user', function (Request $request) {
return $request->user()->load('accesses');
});
Route::get('settings', function (Request $request) {
return $request->user()->load('settings');
});
Route::get('logs', function (Request $request) {
return $request->user()->load('logs');
});
But for some reason, first relationship('accesses') don't work properly. I call this route right after user login to get user info and his accesses to different site modules. It returns user info correctly, but accesses is always null. Also, $this->id don't work inside model for this relation, while works for others. How can I fix it?
Relationship with problem
public function accesses()
{
if (!($this->hasOne(UserAccesses::class)->exists())) {
$accesses = new UserAccesses();
$accesses->user_id = Auth::id(); // $this->id don't work for some reason
$accesses->disk = true;
$accesses->mail = true;
$accesses->calendar = true;
$accesses->photos = true;
$accesses->contacts = true;
$accesses->save();
}
return $this->hasOne(UserAccesses::class);
}
almost the same relationship that works ok
public function settings()
{
if (!($this->hasOne(UserSettings::class)->exists())) {
$settings = new UserSettings();
$settings->user_id = $this->id;
$settings->backup_password = '';
$settings->backup_email = '';
$settings->codeword = '';
$settings->security_notifications = 0;
$settings->password_changed_at = null;
$settings->two_step_authentication = 0;
$settings->save();
}
return $this->hasOne(UserSettings::class);
}
Seeking for your help again. Just wondering for any idea's to return something if the column is NULL. Please see below codes: Thank you!
Application Controller
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first()->get();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
If user is not authenticated then you will get unexpected error. So first check the auth as Auth::check()
Try this :
use Illuminate\Support\Facades\Auth;
public function index()
{
if(!Auth::check()) {
return view('login');
}
$applications = Application::where('user_id', $Auth::user()->id)->first();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
i have to get the company which user chooses but i can't get user data in my class and provider boot function .
user can have more than one company so user have to choose a company for some operations. But as i said , i can't get the company which user chooses.
Like this :
public function boot()
{
$user = Auth::user();
dd( $user ); // return null;
$bid = new Bid();
$show = $bid->check();
Blade::directive('bid',function() use($show){
return "<?php if( $show ) { ?>";
});
Blade::directive('endbid',function(){
return '<?php } ?>';
});
}
My other class :
<?php
namespace App\Services\Buying\Package;
use App\Services\Buying\Package\PackageInterface;
use App\Models\Company\Company;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use App\User;
class PackageBuying extends PackageQuery implements PackageInterface
{
private $company_id;
public function __construct()
{
$user = Auth::user();
dd( $user ); // return null
$this->setCompanyId($this->company_id);
}
public function setSession( Request $request )
{
$request->session()->get('selected-company');
}
public function company()
{
return $this->company_id;
}
public function package()
{
if ( $this->check() ) {
return $this->getPackage($this->company())->first();
}
return [];
}
public function features()
{
return (object)json_decode( $this->package()->features );
}
public function importantFeatures()
{
return (object)json_decode( $this->package()->important_features );
}
public function active()
{
return (bool) $this->getPackage()->firstOrFail()->active;
}
}
Actually if i got user data in the provider boot function , i could send data to my class .
May you please help me ?
Thanks in advance.
Put the code inside your construct function to calMethod function like this
public function callAction( $method, $parameters ) {
$user = Auth::user();
dd( $user ); // return null
$this->setCompanyId($this->company_id);
return parent::callAction( $method, $parameters );
}
I'm having a controller with some functions. In every function I get user data by sharing it from the Contoller.php
In controller.php
public function share_user_data() {
$user = Auth::user();
$this->checkValidation($user);
$this->user = $user;
View::share('user', $user);
}
public function checkValidation($user){
if($user->email_activated == 0){
var_dump($user->email_activated); // I get: int(0)
return redirect('/verifyEmail');
}
}
In the other controller
public function viewCategory(Category $category){
$this->share_user_data(); // here's the check
$this->get_site_lang();
$products = $category->products;
return view('category', compact('category','products'));
}
But I get the category view and not redirected to the verifyEmail route. How to fix this and why it's happening?
The controller function called by the route is the one responsible for the response. I guess it is viewCategory() in your example?
Your viewCategory() function is always returning view(). It must return redirect() instead. I think the main function should be responsible for picking the output of the request.
private function checkValidation($user) {
return $user->email_activated == 0;
}
public function viewCategory(Category $category) {
$user = Auth::user();
/* ... call to share_user_data() or whatever ... */
if ($this->checkValidation($user)) {
return redirect('/verifyEmail');
}
return view('category', compact('category','products'));
}
I have created a function in my HomeController.php beside the index method in Laravel 5.3. In the index method, there are some data retrieved from database, and the sent to the other method to be analyzed and sent to the home view to be shown.
My problem is that laravel displays a mere blank page instead of the actual home.blade.php view. Below is the code that I am using to retrieve data from database and send them to the view. Could you please tell me what am I doing wrong here?
public function index ()
{
$user_table_data = DB::table($table_name)->get();
$this->ChooseAction($user_table_data);
}
private function ChooseAction ($r_value)
{
foreach ($r_value[0] as $key => $value)
{
if ( $key !=='id' && $value !== '0' )
{
array_push($this->choose_action_result, $key);
}
}
if (!empty($this->choose_action_result))
{
return view('home', ['rvalue' => $this->choose_action_result]);
}else{
return view('home');
}
}
in your index method return the call of private value
public function index ()
{
$user_table_data = DB::table($table_name)->get();
return $this->ChooseAction($user_table_data);
}
mark the return keyword in the call statement if should return the view
This will be your code:
public function index ()
{
$user_table_data = DB::table($table_name)->get();
return $this->ChooseAction($user_table_data); //here you also need a return
}
private function ChooseAction ($r_value)
{
foreach ($r_value[0] as $key => $value)
{
if ( $key !=='id' && $value !== '0' )
{
array_push($this->choose_action_result, $key);
}
}
if (!empty($this->choose_action_result))
{
return view('home', ['rvalue' => $this->choose_action_result]);
}else{
return view('home');
}
}
You didn't return the view in your public function index() {} that you got from your private method.