Laravel Lumen Call to undefined function App\Http\Controllers\responce() - php

When I try to call API then I got this error.
Here is My Controller
<?php
namespace App\Http\Controllers;
use App\Video;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class VideoController extends Controller
{
public function index(){
//return 'index function of video controller';
$Video = DB::table('videos')->get();
return responce()->json($Video);
}
}
?>
here is my web.php
$app->get('/', 'VideoController#index');

You should return $Video or any other variable or array, not a function response()
EDIT
try to just return json_encode($Video);

Related

Laravel function does not exists

I'm having some problems with my Laravel APP.
I'm using laravel 8 and every time when I try to visit home page it gives me like:
Function () does not exist
This is how my routes looks like:
Route::get('/{any}', [VueController::class])->where('any', '.*');
And here how VueController looks like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class VueController extends Controller
{
public function __invoke()
{
return view('application');
}
}
VueController::class second parameter is missing it should be
Route::get('/{any}', [VueController::class,'index'])->where('any', '.*');`
then in controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class VueController extends Controller
{
public function index()
{
return view('application');
}
}
Route::get() get req second array must have function name like [VueController::class,'index']
if you don't want to mention index then in resouncse it is possible but it generate some url with some function it will not work in __invoke()
ref link https://laravel.com/docs/8.x/routing

undefined type Auth

namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(Auth::Attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Change the Auth namespace to:
use Illuminate\Support\Facades\Auth;
You may use the auth helper instead and then no worries about any class name
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(auth()->attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Try this :
\Auth::attempt([...]);
I think this is helpfull..
Use the Auth namespace to:
use Illuminate\Support\Facades\Auth;

Error in Laravel 5.6.9 while trying to display view

I'm trying to display data in my views in Laravel 5.6.9, but I keep getting this error.
Error
Code snippet
TodosController
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with('todos', '$todos');
}
}
Browser gave this error
<div class="title m-b-md">
<?php $__currentLoopData = $todos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $todo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php echo e($todo->todo); ?>
<br>
In your controller, you must remove the single quotes around the $todos variable:
return view('todos')->with('todos', $todos);
You should change your controller code like:
namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller
{
public function index() {
$todos = Todo::get();
return view('todos',compact('todos'));
}
}
A wiser approach to the situation would be to use compact. Compact is a PHP function that creates an array containing variables and their values.
When returning a view, we can easily use compact to pass some data.
One can use compact like this:
$data = Data::all();
return view('viewname')->with(compact('data'));
So in your script:
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with(compact('todos'));
}
}
If you wish to do it the way you tried it in the first place, you should do it like this:
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with('todos', $todos);
}
}
Mind that there are no apostrophes around the variable $todos.

Laravel redirect controller to controller with var

im trying to redirect a controller to a controller but im getting the MethodNotAllowedHttpException in RouteCollection.php line 218: error and i cant seem to figure whats wrong
commentcontroller:
<?php
namespace App\Http\Controllers;
use Session;
use Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class commentcontroller extends Controller
{
public function create()
{
$linked_to_post = Request::input('linked_to_post');
$creator_id = Request::input('creator_id');
$comment = Request::input('comment');
DB::table('comments')->insert(['linked_to_post'=>$linked_to_post,'creator_id'=>$creator_id,'content'=>$comment]);
return redirect()->action('postcontroller#post', ['redirectid' => $linked_to_post]);
}
postcontroller:
<?php
namespace App\Http\Controllers;
use App\Users;
use Session;
use App\posts;
use Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class postcontroller extends Controller
{
public function post(){
if (isset($redirectid)) {
$currentid = $redirectid;
}else{
$currentid = request::input('hiddenpostid');
}
$users = users::getusers();
$posts = posts::getposts();
foreach ($posts as $post) {
if ($currentid == $post->post_id) {
$currentpost = $post;
}
}
return view('post',['posts'=>$currentpost]);
}
routes:
Route::get('/', function () {
return view('welcome');
});
Route::get('new','productcontroller#product');
Route::get('admin','admincontroller#authenticate');
Route::get('blog','postcontroller#index');
Route::post('createpost','postcontroller#create');
Route::post('registeruser','usercontroller#create');
Route::post('loginuser','usercontroller#login');
Route::post('logoutuser','usercontroller#logout');
Route::post('post','postcontroller#post');
Route::post('submitcomment','commentcontroller#create');
You are attempting to redirect to a POST route. Redirects make a GET request.
Thus you get a MethodNotAllowedHttpException as you do not have a GET method route set up for the /post uri.

Call the UI function in controller class in laravel

UI Code: in resources\views\DistributorRegistration.php
<?php
class DistributorRegitrationForm
{
public function distributorRegitrationFormHtml(){
return '<h1>Hello</h1>';
}
}
?>
In Controler Class.....
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use resources\views\DistributorRegistration;
class DistributorRegistration extends Controller
{
function VestigePOS_GRNHandler(Request $request){
$id = $request->input('id');
return view(DistributorRegitrationForm::distributorRegitrationFormHtml()) ;
}
}
When I call this controller in routes
Fatal error: Class 'App\Http\Controllers\DistributorRegitrationForm' not found
Which file contains the class DistributorRegitrationForm? I'm missing an use App\...\DistributorRegitrationForm; in the controller, if it's not in the same namespace.
Calling DistributorRegitrationForm::distributorRegitrationFormHtml() won't work, unless the method becomes a static method (public static function).
You have some typos in there ;-)

Categories