Laravel model all() function is not working as intended - laravel

I have a model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model {
protected $table = 'contacts';
//
}
And in the controller action
$c= Contact::all();
I am getting the "Whoops, looks like something went wrong." error.
Error detail:
FatalErrorException in ContactController.php line 9:
Class 'App\Http\Controllers\Contact' not found
The table "contacts" exists in the database.
What thing I am missing? Whats wrong here?

Tell your controller where your model is if it is in route of your project then on the top of your controller add
use App\Contact;
Or you may also define it every time
$c = App\Contact::all();
also in your model no need to define a table until it is not different from the plural model name. If your model name is Contact, laravel on its own query contacts table, you should define the table name if the model name is Contact and the table name is somethingElse.

Don't use
$c= Contact::all();
Instead use
$c = \App\Contact::all()
or
$c = new \App\Contact;
$c->all();
This is all that you have to use.
UPDATE: Just after digging in the concepts of OOP, I found this:
<?php
use \App\Contact;
var_dump(Contact::all());

Related

Laravel Backpack 4.0. Save a model then another one (different)

In my laravel/backpack 4.0 project I have three model:
Product: id, name, ...
Pricebook: id, name, ...
Price: id, pricebook_id, value, ...
I need to create a row in prices table every time a new Product is stored id database.
Is there a method like 'before' or 'after' save to hook?
if not, any other solution?
...I got confused about something trivial, probably a mispelling. This is the solution thank's Martin
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\ProductRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
use App\Models\Price;
class ProductCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation {store as traitStore ;}
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
public function store()
{
$request = $this->crud->validateRequest();
$response = $this->traitStore();
//get last inserted row id
$entryID = $this->data['entry']->id;
$price = new Price();
$price->product_id = $entryID;
$price->price = 0;
$price->save();
// show a success message
\Alert::success('New price created in DEFAULT_BASE_PRICEBOOK'))->flash();
return $response;
}
...
There are no such hooks, but you can override these methods that control the database insert/update. From the documentation:
Callbacks
Developers coming from GroceryCRUD on CodeIgniter or other CRUD systems will be looking for callbacks to run before_insert, before_update, after_insert, after_update. There are no callbacks in Backpack. The store() and update() code is inside a trait, so you can easily overwrite that method, and call it inside your new method. For example, here's how we can do things before/after an item is saved in the Create operation:
namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
class ProductCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
// ...
public function store()
{
// do something before validation, before save, before everything
$response = $this->traitStore();
// do something after save
return $response;
}
Source

Why is Laravel eloquent returning empty

This is my model
namespace App\Models\Invitation;
use App\Models\Model;
class SendtoType extends Model
{
}
When i get data from SendtoType it return empty or null but i sure sendto_types table has datas
$types = SendtoType::all();
dd($types); // return empty collection
$types = SendtoType::find(1);
dd($types);// null
It's happend after i run php artisan command:reset_table sendto_types command.
I cleared cache but it's not working.
Thank you everyone, i found my mistake:
i set timestamp for table's deleted_at column but forgot to set not null so it also filled deleted_at column when i add data.
That's why eloquent clause return empty.
So silly mistake!
You can add protected $table = 'sendto_types'; inside your model class with actual table name in your database.
class SendtoType extends Model
{
protected $table = 'sendto_types';
}
Command command:reset_table is not a built in command, so you'd need to look at your source code to see what it does, but by the name of it, I would assume the command empties your table.
In this case, your table is empty, so your query will not return any results.

laravel 5.2 - Accessing Model in Controller Method

I have added the line use App\Post; in the header of my PostController class.
When I try $post = new App\Post; in a controller method, I get the following error message
Class 'App\Http\Controllers\App\Post' not found
What are some possibilities for why I am getting this error message?
Since you already included the Post class, you don't have to reference the path again.
$post = new Post();
This should work.
u can use facades..
$post = Post::all(); << return all row
I am assuming you used artisan to generate the Model. You may use eloquent as:
use App\Post as Post;
...
$post = Post::all();
The reason you are not able to access the model via App\Post is because the file you are attempting to do this in already has a namespace, shown in the error: App\Http\Controllers\App\Post, which implies the namespace of the file is App\Http\Controllers.
Since you are not referencing the model with an absolute namespace (\ at the beginning), PHP is looking for that class relative to the current namespace.
<?php
namespace App\Http\Controllers;
...
$post = App\Post::find(1); // App\Http\Controllers\App\Post
$post = \App\Post::find(1); // App\Post
That explains the error. However, as mentioned by others, you have already used the model in your file and can access it simply with Post.
$post = Post::find(1);

Inserting a variable in sql through laravel

I am trying to insert data into sql in laravel 5
My Code:
$a1=Input::get('username');
$a2=Input::get('reg_no');
DB::insert("INSERT INTO ngosignup VALUES($a1,$a2)");
But I am getting SQL error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'#gmial.com,a,a,a,a,9,a,9814937684,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,N'
at line 1 (SQL: INSERT INTO
ngosignup(name,email,pass,confirmpass,address,city,pin,state,mobile,registration,secretary,dest,firstmember,mobile1,secondmember,mobile2,thirdmember,mobile3,objective,dev1,place1,obj1,dev2,place2,obj2,dev3,place3,obj3)
VALUES(a,amit#gmial.com,a,a,a,a,9,a,9814937684,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL))
PLEASE HELP.
Try Eloquent way.. for example of using your db.. Make a Model for ngosignup table and include it on controller
public function create(\Illuminate\Http\Request $request) {
$input = $this->request->all();
$a1 = $input['username'];
$a2 = $input['reg_no'];
$newngo = new ngosignup; //Replace ngosignup with your Model name.
$newngo->name = $a1; //name and reg_no should be column name
$newngo->reg_no = $a2;
$newngo->save(); //this will add these values into your DB
}
You can do this way.. Easier and Flexible.. You can add validator.. Refer to Laravel docs on their official site :)
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\ngosignup;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ngosignupController extends Controller
{
public function index() {
$ngo = ngosignup::first(); // this shows first row of table..you can use get() instead of first to get all table as collection.
$data= array(
'ngo ' => $ngo
);
return View('index', $data);
}
Then on Index.Blade.php File.. you can display data from ngosignup table like
{{ $ngo->email }} // replace email with which data to display. if ollection.. use foreach loop to display all data.
Refer to Laravel docs.. they list all methods available..

Laravel 4 - Method [where] does not exist

I am trying to get row from the table with "->find(X)" or "->where" but i get error in return. i guess i am missing something but i can't tell what.
<?php
class SnippetsController extends BaseController {
public $restful = true;
public function index($id)
{
$snippet = SnippetsController::where('id', '=', 11)->get();
i tried it with find but noting helps.
$snippet = SnippetsController::find(11);
with "->get" and without. but noting works. i do have the "11" id in the table itself and the error seems to have noting to do with it.
maybe i need to extend the class to Eloquent? if so, how do i keep the BaseController on top of it?
Thanks!
error:
BadMethodCallException Method [find] does not exist.
You must have a Model called Snippet, right? So this is how you use it:
$snippet = Snippet::find(11);
Your SnippetsController is not tied to a database (Eloquent ORM), so you must create (if you don't have already) a model:
class Snippet extends Eloquent {
}
Just edit the code $snippet = SnippetsController::where('id', '=', 11)->get(); with $snippet = Snippets::where('id', '=', 11)->get();. May be your model name is Snippets it may solve your problem.

Resources