0

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.

user3790198
  • 47
  • 1
  • 9
  • Check the web server log for errors. – Jason K Nov 21 '19 at 14:34
  • nothing is being printed in webserver log – user3790198 Nov 21 '19 at 14:39
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) Scroll a bit down to "HTTP Error 500 - Internal server error" – digijay Nov 21 '19 at 14:39
  • @digijay no , not helpful – user3790198 Nov 21 '19 at 14:46
  • @JasonK attaching server log. – user3790198 Nov 21 '19 at 14:47
  • Does `http://localhost/lara-vue-login/public/main/getAllBooks` throw errors? What's in your laravel.log? – brombeer Nov 21 '19 at 14:47
  • @kerbholz ,yes, but code is executing fine before I execute DB query , if I comment lines used for DB connectivity it works fine – user3790198 Nov 21 '19 at 14:50
  • What you posted is not the server log. Check [here](https://stackoverflow.com/a/53843354/4178487) to see how to check the log. – Jason K Nov 21 '19 at 14:52
  • It's same what i have posted. D:\Softwares\Xampp\apache\logs\error.logs – user3790198 Nov 21 '19 at 14:55
  • 2
    Laravel has its own logs for 500 errors. `storage/logs/laravel.log`. You should be able to see the error there. That being said, you're using `mysqli`, which goes against the point of using Laravel... You shouldn't have to use native PHP logic when you have Models and the Query Builder available. – Tim Lewis Nov 21 '19 at 15:09
  • @TimLewis I am using laravel model , then getting no result in return. I have updated the code with model. – user3790198 Nov 22 '19 at 06:00

2 Answers2

0

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!!!!!!!

user3790198
  • 47
  • 1
  • 9
0

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

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65