Laravel 5.8 "Class 'App\Exports\Auth' not found" - laravel

I am trying to download records related to authenticated user using maatwebsite/excel and here is my export:
<?php
namespace App\Exports;
use App\User;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return Auth::user()->getrecs();
}
}
but when I try to visit the route related to this export I get:
"Class 'App\Exports\Auth' not found"
how to solve this?

You forgot to use Auth.
<?php
namespace App\Exports;
use Auth;
use App\User;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return Auth::user()->getrecs();
}
}
FYI, Now Laravel has released version 7.

Related

Trying to define a many-to-many relation with a pivot model in Laravel 8

Trying this for hours now and I don't see the error. I have a model 'User' and a model 'Round'. I want to define a n:m-relation with a model 'Flight' as pivot model.
User.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
class User extends Authenticatable implements MustVerifyEmail, HasMedia
{
use Notifiable;
use InteractsWithMedia;
/*
.....
*/
public function rounds() {
return $this->belongsToMany(Round::class)->using(Flight::class);
}
}
Round.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Round extends Model
{
/*
.....
*/
public function users() {
return $this->belongsToMany(User::class)->using(Flight::class);
}
}
Flight.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class Flight extends Pivot
{
public $incrementing = true;
/*
.....
*/
}
I made several migrations and seeder.
RelationSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Round;
class RelationSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$round = Round::find(1);
$round->users()->sync([1]);
}
}
When running artisan migrate:refresh --seed all tables are created as expected, but the following error occurs on seeding
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tour.round_user' doesn't exist (SQL: select * from round_user where round_id = 1)
Obviously Laravel is looking for a standard named pivot-table and not for the desired flights-table.
I am using Laravel 8 in a Docker-Container with Sail.
Where is my mistake?
I found my mistake. With using the pivot model, the name conventions for the intermediate table stay valid. It works with
public function rounds() {
return $this->belongsToMany(Round::class, 'flights')->using(Flight::class);
}
and same thing in the inverse definition
public function users() {
return $this->belongsToMany(User::class, 'flights')->using(Flight::class);
}
Thank you everybody who is helping here - I solved 1000s of problems reading your posts!

How to get online users and their count?

I want to use this package in my Laravel 8 projects for retrieving online users. However, I am lost. I follow the documentation but I don't understand the steps. When I login through another browser, it keeps giving me 0 users. What did I do wrong?
User.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laratrust\Traits\LaratrustUserTrait;
use Shetabit\Visitor\Traits\Visitor;
class User extends Authenticatable
{
use Visitor;
use LaratrustUserTrait;
use HasFactory, Notifiable;
protected $guarded = [];
}
DashboardController.php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\User;
class DashboardController extends Controller
{
public function index(User $user)
{
$onlineUsers = $user->visits()->count();
return view('dashboard.index', compact('onlineUsers'));
}
}
visitor()->onlineVisitors(User::class); // returns collection of online users
User::online()->get(); // another way

Error when using Spate Media Library: "BadMethodCallException Call to undefined method Illuminate\Foundation\Auth\User::addMediaFromRequest()"

This is my User Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\File;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements HasMedia
{
use HasMediaTrait;
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role(){
return $this->belongsToMany('App/Role');
}
}
This is my UserController
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
I want to use Spatie Media Library and upload a photo for each user but I get this error related to Spatie Library:
"Call to undefined method
Illuminate\Foundation\Auth\User::addMediaFromRequest()".
I have read some related posts but I don't understand how to fix this.
Thank you.
Version 8 of the library has this documentation. Are you using the correct trait?
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class YourModel extends Model implements HasMedia
{
use InteractsWithMedia;
}
Edited:
On another note, using HasMediaTrait is for version 7 so I assume you're using version 7. I think it's because you're importing the wrong User class. The User class used to implement HasMedia lies in the App\User namespace. But you're importing the User class from the Illuminate\Foundation\Auth\User namespace. So change it to use App\User; and you should be fine.
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
https://docs.spatie.be/laravel-medialibrary/v8/basic-usage/preparing-your-model/

Eloquent query doesn't use the where/first conditions. Why?

Ma simple eloquent query doesn't use the where condition.
Players::where('id_player',"=" ,3113)->first()->toSql();
Returns:
"select * from players"
My Controller:
namespace App\Http\Controllers\Office;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Players;
class OfficeController extends Controller{
public function playerLineup(Request $request){
$e = Players::where('id_player',"=" ,3113)->first()->toSql();
dd($e);
}
}
My Modal:
namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Players extends Model{
protected $primaryKey = 'idp';
}
Once you run ->first() the result is a fresh instance of the Player model. then ->toSql() on a fresh model is like Player::query()->toSql().
you need to call ->toSql() before that:
Players::where('id_player',"=" ,3113)->toSql();
or
Players::where('id_player',"=" ,3113)->limit(1)->toSql();
/try to do this/
namespace App\Http\Controllers\Office;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Players;
use DB;
class OfficeController extends Controller{
public function playerLineup(Request $request){
$e = DB::table('Players')->where('id_player',"=" ,3113)->get()->first();
dd($e);
}
}

laravel QueryException table not found

I am not getting it that why i am facing it.
I created a migration name 2018_01_12_035551_create_inquiry_master_table
In my route file assign a route Route::get('lists-inquiry','InquiryController#listinquiry')->name('lists-inquiry');
Created a controller name InquiryController
Controller code:
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller as BaseController;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\InquiryMaster;
use Illuminate\Support\Facades\Input;
class InquiryController extends BaseController
{
public function listinquiry() {
$inquiry = InquiryMaster::all();
// dd($inquiry) Here i am getting error
return view('admin.list_inquiry')->with('listinquiry', $inquiry);
}
}
Created a model InquiryMaster
My model look like:
<?php
namespace App;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class InquiryMaster extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'created_date_time',
'user_id',
'stitch_video_path',
'completion_status'
];
/**
* Indicates if the model should be timestamped.
*
* #var bool
*/
}
When i dd($inquiry) in controller it returns error
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'bigball_travel.inquiry_masters' doesn't exist (SQL: select * from inquiry_masters)
It shows table inquiry_masters
and i created inquiry_master
add protected $table = 'inquiry_master'; to InquiryMaster model
so class would look like this.
<?php
namespace App;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class InquiryMaster extends Model
{
protected $table = 'inquiry_master';
protected $fillable = [
'created_date_time',
'user_id',
'stitch_video_path',
'completion_status'
];
}

Resources