Calling Laravel function dynamically - php

I just started learning Laravel.
I get an error message of "Error: Call to undefined function addPost()". What seems to be the problem in my code?
$command == "addPost";
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class PostsController extends Controller
{
function checkPostsCommand(Request $request) {
$command = $request->btn;
$data = $this->$command($request->all());
return response()->json($data);
}
private function addPost() {
return 'wew';
}
}

use it like this :
$this->$command();

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class PostsController extends Controller
{
function checkPostsCommand(Request $request)
{
$command = $request->btn;
return $this->addPost();
}
private function addPost() {
echo 'wew';
}
}

Related

Getting no result from table , while fetching data using laravel model

I am trying to execute simple CRUD operation using laravel. but it gives an error code 500 , when I try to fetch data from table either by laravel framework as well as with plain PHP.
Here is my controller class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = DB::select('select * from book');
$data = BookModel::all();
echo($data);
return compact('data');
}
}
axiom , which is been used. ---> "https://unpkg.com/axios/dist/axios.min.js"
Model Class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BookModel extends Model {
protected $table = "book";
public $timestamps = false;
}
It is returning no result from the table.
It worked after I changed my controller to
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return $data;
}
}
But it was giving null result when I was doing something like below
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return view('admin')->withTasks($data);
}
}
Can someone explain why it was not working earlier?
Anyways , Thanks everyone for your help.
Cheeeeeeeeerrsssss!!!!!!!
You need to add response() to return data without view.
Like below the code returns the JSON data from your BookModel that sometimes we need through the ajax request like axios.
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return response()->toJson($data);
}
}
With view you can do the following:
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return view('admin', compact('data'));
}
}
Where you can access the BookModel data through $data variable in your admin.blade.php

Laravel 5.7: Class 'App\Http\Controllers\MailableClass' not found

I created a Mailable called Class UserRequest
I'm trying to call it from inside a controller buy this is the error I get:
Class 'App\Http\Controllers\UserRequest' not found
I also tried ->send(new \UserRequest($msgdata)); but it still doesn't work.
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request)
{
$msgdata = array('subject'=>$request->subject,'email'=>$request->email, 'name'=>$request->name,'body'=>$request->body);
try
{
Mail::to('dddddddd#dddsdsf.com')
->send(new UserRequest($msgdata));
}
catch(Exception $e)
{
}
}
}
Include your class at the top like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\UserRequest; // including your class
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request){
$msgdata = array('subject'=>$request->subject,'email'=>$request->email,
'name'=>$request->name,'body'=>$request->body);
try {
Mail::to('dddddddd#dddsdsf.com')->send(new UserRequest($msgdata));
}catch(Exception $e){
// Log your exception
}
}
}
add 'App\Http\Controllers\UserRequest' to the head
use App\Http\Controllers\UserRequest;
at the top.
You will need to add the right path to the top as stated by other.
Also check the namespace in the UserRequest class

Laravel: Export is not found

I'm working on the Excel exports in my laravel project. Now I get this error:Class 'App\Export\UsersExport' not found I made 3 others and they worked. This one doesn't and I can't seem to figure out what causes this error. I'm using "Maatwebsite's Laravel-Excel".
My exports controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//exports
use App\Export\UsersExport;
use App\Exports\StudyClassExport;
use App\Exports\EducationsExport;
use App\Exports\IntakesExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class ExportsController extends Controller
{
public function users()
{
return Excel::download(new UsersExport, 'gebruikers.xlsx');
}
public function educations()
{
return Excel::download(new EducationsExport, 'opleidingen.xlsx');
}
public function classes()
{
return Excel::download(new StudyClassExport, 'klassen.xlsx');
}
public function intakes()
{
return Excel::download(new IntakesExport, 'intakes.xlsx');
}
}
UsersExport:
<?php
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return User::all();
}
}
I have no idea what causes this error. The others work but this one doesn't
You have a typo:
use App\Export\UsersExport;
use App\Exports\UsersExport;
ExportS

How to Access to Laravel 5 with Pingpong-Modules to a specific Module

I am new to Laravel 5 and Pingpong-Modules https://github.com/pingpong-labs/modules
I want to Access from an outsite of the Modules-Directory to a Specific Module-Function.
My Actual Configuration is:
I want to access to the Method "test()" from the DashboardController - what is here the best practice?
Code of Controller 1:
<?php namespace App\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\News;
use App\NewsCategory;
use App\User;
use App\Video;
use App\VideoAlbum;
use App\Photo;
use App\PhotoAlbum;
use \Pingpong\Modules\Facades\Module;
use App\Helpers\ModulesHelper;
class DashboardController extends AdminController {
public function __construct()
{
parent::__construct();
}
public function index()
{
$title = "Dashboard";
$news = News::count();
$newscategory = NewsCategory::count();
$users = User::count();
$photo = Photo::count();
$photoalbum = PhotoAlbum::count();
$video = Video::count();
$videoalbum = VideoAlbum::count();
return view('admin.dashboard.index', compact('title','news','newscategory','video','videoalbum','photo',
'photoalbum','users'));
}
Code of Controller 2:
<?php namespace Modules\Users\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;
class UsersController extends Controller {
public function index()
{
return View::make('users::index');
}
public function test() {
return "TEST";
}
}
Put this block wherever you want in DashboardController:
$usersController = App::make('Modules\Users\Http\Controllers\UsersController');
$usersController->test();

Laravel 5 - Where to define functions and call them in views & controllers

I have following function and want to call it from view. Basically i want to put all common functions in one file. I am not sure where to create that file and how to call it inside controller and view.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
So far i have created CommonController.php in app/Http/Controllers and put above function in it.
Then in other controller i have tried to call it following way:
use App\Http\Controllers\Common;
class SongsController extends Controller {
public function index($id)
{
echo Common::BytesToMB('7012187');
}
}
But i am getting error:
Class 'App\Http\Controllers\Common' not found
Ok, new try. You missed to use the complete class name and add the static keyword:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public static function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends Controller {
public function index($id)
{
echo CommonController::BytesToMB('7012187');
}
}
Another and more OOP solution is to use the function from the parent class:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
protected function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends CommonController {
public function index($id)
{
echo $this->bytesToMB('7012187');
}
}

Categories