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
Related
have been trying to get the output of these code from my vew but its giving me issues, Please i woild really be bappy to get help.
In my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;
class LeavesController extends Controller
{
public function getAllLeave()
{
$data = App\allLeave::find(1)->full_name;
return view('leave/allLeave',["data"=>$data]);
}
}
in my employee model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* Class Personnel
* #package App
*/
class Employee extends Audit
{
public function leave()
{
return $this->belongsTo('App\allLeave');
}
}
In allLeaveModel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class allLeave extends Model
{
public function empolyee()
{
return $this->hasMany('App\Employee');
}
}
in the blade
{{$data->employee->full_name}}
You already assign full name to data in controller. You only need {{ $data }} in blade
If i have to do the same i will do it simply like
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;
class LeavesController extends Controller
{
public function getAllLeave()
{
/* No need to use App\allLeave because you already have that used in
top of the project */
$data=allLeave::findorFail(1);
return view('leave.allLeave')->with('data', $data);
}
}
in front end just use
{{$data->first_name}} //same column as in database table
Note: Make sure are using laravel eloquent model relationships
In your controller should be like this:
$data = App\allLeave::find(1)->empolyee();
And your blade:
{{$data->full_name}}
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.
I'm following the Laracasts series and have run into an issue on the episode Laravel 5.4 From Scratch: Route Model Binding.
Laravel version:
Laravel Framework 5.6.13
The error:
Class App\Http\Controllers\Panel does not exist
This shows on both the /panel and /panel/1 pages
App\Http\Controllers\PanelController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
// Code works if I uncomment below line, and change the show function to "show($panel)"
//use App;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
routes/web.php
// Main panel view
Route::get('/panel', 'PanelController#index');
// Individual panel view
Route::get('/panel/{panel}', 'PanelController#show');
App/Panel.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Panel extends Model
{
public static function activePanels()
{
return static::where('status', 1)->get();
}
}
Add this line in panel controller before the class
use App\Panel;
You need to add use App\Panel; to top of class
Or call it by full namespace $panels = App\Panel::all();
You don't included your model to class.
Add App\Panel to main include section:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Panel;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
or load model in your class method manually:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PanelController extends Controller
{
public function index()
{
$panels = App\Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
I want to call a function of CommonController in PostsController.
PostsController is in Posting namespace
PostController: App\Http\Controllers\Posting\PostsController
CommonController: App\Http\Controllers\CommonController
I tried this code but it did not work in PostingController, it is a small code from my PostingController
namespace App\Http\Controllers\Employer;
use App\Http\Controllers\CommonController;
class PostsController extends Controller
{
public function myFunction(Request $request, $id){
$commonControllerObj = new CommonContoller;
$result = $commonControllerObj->commonCallingFunction($id);
}
}
but it did not work, its giving error
Class 'App\Http\Controllers\Posting\CommonContoller' not found
The first your namespace is wrong
namespace App\Http\Controllers\Posting;
The second you can call another Controller like this
app('App\Http\Controllers\CommonController')->commonCallingFunction();
This will work, but this is bad in terms of code organisation
You can extends Controller like this
use App\Http\Controllers\CommonContoller;
class PostsController extends CommonContoller
{
public function myFunction(Request $request, $id){
$result = $this->commonCallingFunction($id);
}
}
I have the following method in my controller:
public function store(Request $request){
$data = session()->get('dmca');
// return $data;
$notice = Notice::open($date);
$notice->useTemplate($request->input('template'));
$notice->save();
return Notice::first();
}
When the controller runs i get the following error:
Now i do have the following modal class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notice extends Model {
protected $fillable = [
'provider_id',
'infringing_title',
'infringing_link',
'original_link',
'original_description',
'template',
'content_removed'
];
public static function open(array $attributes) {
return new static($attributes);
}
public function useTemplate($template) {
$this->template = $template;
}
}
Now why am i getting this error in laravel class not found , even though i have this class defined ??
Just use use keyword at the top of your controller class.
<?php
namespace App\Http\Controllers;
use App\Notice; // <------------------ use here
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function store(Request $request)
{
$data = session()->get('dmca');
// return $data;
$notice = Notice::open($date);
$notice->useTemplate($request->input('template'));
$notice->save();
return Notice::first();
}
}
The use keyword helps php to recognize the class Notice within a certain namespace
Hope this helps!
Add this line to the top of a controller:
use App\Notice;
Or use full namespace:
$notice = \App\Notice::open($date);