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'));
}
Related
I create new controller and models using php artisan command. And I tried to access model using controller but I get Class 'Thankful' not found error.
Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Thankful;
class Thanks extends Controller
{
public function hello(){
$my_data = new Thankful;
$data = $my_data->my_model();
return view("thanks",compact('data'));
}
}
Model Code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Thankful extends Model
{
public function my_model()
{
$data = "Thank You";
return $data;
}
}
The namespace of your Thankful model is incorrect:
<?php
namespace App\Http\Controllers;
use App\Thankful;
use Illuminate\Http\Request;
class Thanks extends Controller
{
public function hello ()
{
//
}
}
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.
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);
}
}
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();
}
Tables reletions
Category.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function getVideo(){
return $this->hasMany('App\Video','category_id','id')->where('categories.kat_name', '=', 'comedy');
}
}
PagesController.php(Controller)
namespace App\Http\Controllers;
use App\User;
use App\Category;
use App\Video;
use App\Http\Controllers\Controller;
class PagesController extends Controller
{
function getCategory($kategori_ad){
$myvideo = Category::all()->getVideo();
}
}
I want make this : SELECT * FROM categories join videos ON categories.id=videos.category_id AND categories.kat_name='animals' with orm
class Category extends Model {
public function getVideo(){
return $this->hasMany('App\Video','category_id','id');
}
}
class PagesController extends Controller {
function getCategory($kategori_ad){
$myvideo = Category::where('kat_name', '=', 'comedy')->with('getVideo')->get();
}
}
$myvideo = Category::with('getVideo')->get();