I get the Object not found error while trying to output all users details from database - laravel

I am trying to out all users on my admin dashboard by parsing the result through to my route
Here is my controller;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('notAdmin');
}
public function index(){
$user = User::all()->orderBy('id', 'desc')->paginate(100);
return view('admin-dashboard')->with('users', $users);
}
}
I get the "Method Illuminate\Database\Eloquent\Collection::orderBy does not exist." error

You have applied wrong query:
You don't need to add all() as you are using paginate
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('notAdmin');
}
public function index(){
$users = User::orderBy('id', 'desc')->paginate(100);
return view('admin-dashboard')->with('users', $users);
}
}

Related

How to pass data from database to Mailable?

I know how to pass data to view but how do you pass data to a mailable? I'm also a bit confused on where to put the database call DB::select, in the controller or the mailable? So far I've tried this:
web.php:
Route::get('/test-email', 'App\Http\Controllers\TestMailController#test');
TestMailController.php:
<?php
namespace App\Http\Controllers;
use App\Mail\ContactMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class TestMailController extends Controller
{
public function test()
{
return new ContactMail();
}
}
TestMail.php:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
class TestMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $data;
public function __construct()
{
$this->data = DB::select('select * from db_name');
}
public function build()
{
return $this->markdown('emails.testmail')->with('data', $data);
}
}
You need to use $this :
public function build()
{
return $this->markdown('emails.testmail')->with('data', $this->data);
}

Class 'App\Http\Controllers\Post' not found in laravel-5.8

hi m trying to show data at index page but it says: Class 'App\Http\Controllers\Post' not found
controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class IndexController extends Controller
{
public function index()
{
$data = Post::all();
return view('index', compact('data'));
}
You need to add the following line:
use App\Post;
now your code should look like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class IndexController extends Controller
{
public function index()
{
$data = Post::all();
return view('index', compact('data'));
}

Laravel repository Class App\Repository\User does not exist

Hello I want to use my own repository class in my Laravel 5.8 project
I created my file Repository in the App File and in this file I added A class called ConversationRepository
This is my class:
<?php
namespace App\Repository;
class ConversationRepository{
private $user;
public function __construct(User $user){
$this->user=$user;
}
public function getConversation(int $userId){
return $this->user->newQuery()
->select('name','id')
->where('id','!=',$userId)
->get();
}
}
And then when I use it on my controller :
<?php
namespace App\Http\Controllers;
use App\User;
use Auth;
use Illuminate\Http\Request;
use App\Repository\ConversationRepository;
class ConversationsController extends Controller
{
private $r;
private $auth;
public function __construct(ConversationRepository $conversationRepository,AuthManager $auth){
$this->r = $conversationRepository;
$this->auth = $auth;
}
public function index(){
return view('conversation.index',[
'users'=>$this->r->getConversation($this->auth->user()->id)
]);
}
public function show(User $user){
return view('conversation.show',['users'=>$this->r->getConversation(
$this->auth->user()->id),
'user'=>$user
]);
}
public function store(User $user){
}
}
I get the error
Class App\Repository\User does not exist
Apparently, you forgot to add use App\User; in the class ConversationsController file.

Laravel - Public variable in controller

I want to have one public variable $users = User::all(); so i could use it in different methods inside controller and it doesn't work this way:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class AdminController extends Controller
{
public $users = User::all();
public function __construct() {
$this->middleware('auth');
}
public function index()
{
return view('admin.index');
}
public function showUsers()
{
return view('admin.users', compact('users'));
}
}
i get this error: Constant expression contains invalid operations
What am i doing wrong?
Try adding the assignment into the __construct() function instead:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class AdminController extends Controller
{
public $users;
public function __construct() {
$this->users = User::all();
$this->middleware('auth');
}
public function index()
{
return view('admin.index');
}
public function showUsers()
{
$users = $this->users;
return view('admin.users', compact('users'));
}
}
You need to initialize $users in your constructor:
<?php
public $users;
public function __construct() {
$this->middleware('auth');
$this->users = User::all();
}

Call to a member function save() on a non-object

I am building a small laravel app beginning level. I got error in code in I couldn't solve out.
Here is the code
post model
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
public function user()
{
return $this->belongsTo('app\user');
}
}
user model
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
$this->hasMany('app\post');
}
}
postcontroller
namespace App\Http\Controllers;
use App\post;
use Illuminate\Http\Request;
class postcontroller extends Controller{
public function postCreatePost(Request $request){
$post = new post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
Thank you in advance for any try from you to solve this query.
Make sure inside your user model you're returning the relationship.
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('app\post');
}
}
Your posted code doesn't have a return inside the posts function.
i think this should fix
add your table field to protected $fillable = [] array
user
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
protected $fillable = ['body'];
public function user()
{
return $this->belongsTo('app\user');
}
}
post
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
protected $fillable = ['username', 'field2', 'blabla'];
public function posts()
{
return $this->hasMany('app\post');
}
}
postcontroller
namespace App\Http\Controllers;
use App\post;
use Illuminate\Http\Request;
class postcontroller extends Controller{
public function postCreatePost(Request $request){
$post = new post(['body' => $request->body]);
$request->user->posts()->save($post);
return redirect()->route('dashboard');
}
}

Resources