I can not get any records by using
return Offer::all();
although I see them if using
return DB::select('select * from offers where id = ?', array(1));
I get following error:
FatalErrorException in OffersController.php line 21:
Class 'App\Http\Controllers\Offer' not found
Both commands were run from OffersController. I also have model file
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Offer extends Model {
protected $fillable = array('title', 'done');
}
What am I doing wrong? I think I will use both 'DB' and 'Eloquent' in the future.
Add the following at the top of your OffersController
use Offer;
or perhaps
use App\Offer;
Related
I'm using Lumen 8.3 ,wanted to use factory() function in my tests, it gives me
Undefined Function ,there is nothing useful in the Docs of Lumen
Am i missing something here?
class ProductTest extends TestCase
{
public function test_if_can_send_products_list(){
$products = factory('App/Products',5)->make();
$this->json('post','/payouts',$products)
->seeJson([
'created' => true,
]);
}
}
->
Error: Call to undefined function factory()
It's better to use direct class like that:
$products = factory(Products::class, 5)->create();
don't forget to add Products model usage (namespace).
Edit
You should create Factory:
<?php
namespace Database\Factories;
use App\Products;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ProductFactory extends Factory
{
protected $model = Products::class;
public function definition(): array
{
return [
'name' => $this->faker->unique()->userName()
];
}
}
And add HasFactory Trait to your model:
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Products extends Model {
use HasFactory;
}
you can also use it like this
Products::factory()->count(5)->make();
I just uncommented these lines in app.php file
$app->withFacades();
$app->withEloquent();
Apparently Laravel 8 removed the 'factory' helper, and it seems Lumen followed that path without updating documentation;
#Faesal Answer is the correct way to do it these days;
remember to add use HasFactory; to your Model.
Good day, i'm trying to get the result from my model that called with Mainmodel through my controller, my controller is MainController.
Here is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Mainmodel;
class MainController extends Controller
{
function index(){
echo "Kok, direct akses sih?";
}
function get_menu(){
$menu = app\Mainmodel::request_menu();
dd($menu);
}
}
Here is my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Mainmodel extends Model
{
function request_menu(){
$menu = DB::table('menu')
->orderBy('[order]', 'desc')
->get();
return $menu;
}
}
my routes
Route::get('menu','MainController#get_menu');
with my script above i get this
FatalErrorException in MainController.php line 17: Class
'App\Http\Controllers\app\Mainmodel' not found
how can i fix this ? thanks in advance.
Note: I'm bit confuse with laravel. I'm using codeigniter before. And i have a simple question. In laravel for request to database should i use model ? or can i just use my controller for my request to database.
sorry for my bad english.
I would imagine it's because your using app rather than App for the namespace.
Try changing:
app\Mainmodel
To:
App\Mainmodel
Alternatively, you can add a use statement to the top of the class and then just reference the class i.e.:
use App\Mainmodel;
Then you can just do something like:
Mainmodel::request_menu();
The way you're currently using you models is not the way Eloquent should be used. As I mentioned in my comment you should create a model for each table in your database (or at least for the majority of use cases).
To do this run:
php artisan make:model Menu
Then in the newly created Menu model add:
protected $table = 'menu';
This is because Laravel's default naming convention is singular for the class name and plural for the table name. Since your table name is menu and not menus you just need to tell Laravel to use a different table name.
Then your controller would look something like:
<?php
namespace App\Http\Controllers;
use App\Menu;
class MainController extends Controller
{
public function index()
{
echo "Kok, direct akses sih?";
}
public function get_menu()
{
$menu = Menu::orderBy('order', 'desc')->get();
dd($menu);
}
}
Hope this helps!
You can solve it by different solution. The solution is you don't have to call request_menu(); you can get it in your controller.
MainController
use use Illuminate\Support\Facades\DB;
public function get_menu(){
$menu = DB::table('menu')
->orderBy('Your_Field_Name', 'DESC')
->get();
dd($menu);
}
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Mappable;
class User extends Model
{
use Eloquence, Mappable;
public $id;
public $name
}
$data= User::select('distinct User.name')->join('Member as m','User.id','m.userId')->whereRaw('User.name is not null')->get();
I want to avoid Using table name all time instead i want to use alias name.
You can use the Model::from method for this
<?php
class User extends Model
{
use Eloquence, Mappable;
public $id;
public $name
}
$data= User::from('User as u')
->select('distinct u.name')
->join('Member as m','u.id','m.userId')
->whereRaw('u.name is not null')
->get();
NOTE: I see a lot upvotes, but this approach isn't good practice. It's better to find a different approach. My advice stay on the common walked paths.
You can add
protected $table = 'users as u';
-- users is exact your table name in db
And use it like this
User::where('u.id', 1229)->select('u.email')->get();
This works for me:
$query = Model::from('table as alias')->get();
And u can check it with:
dd($query->toSql());
Basically I manage to get my soft delete working on my User table., problem is now my other pages would not work, I believe I need to make some changes using the withTrashed to the queries for the pages? For example the controller as shown below, how do I add the users column which have been soft deleted, can someone guide me and help me with this?
Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Zone;
use App\Parameter;
class DashboardController extends Controller
{
public function index() {
$zones = Zone::all();
$parameters = Parameter::all();
return view('dashboard', compact('zones', 'parameters'));
}
}
You can just add ->withTrashed() to your query and use get instead of all. Like so:
$zones = Zone::all(); //Excludes soft deleted
$allparameters = Parameter::withTrashed()->get(); //Includes soft deleted
$allusers = User::withTrashed()->get();
Also, onlyTrashed() will do what it suggests:
$trashedusers = User::onlyTrashed()->get(); //Only soft deleted users
Using Laravel 5. I have a model Property, that has many Weeks available to it. This is the Property Model.
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* #property mixed week
*/
class Property extends Model
{
public function week()
{
return $this->hasMany('App\Models\Week', 'PropertyID', 'PropertyID');
}
}
I am trying to use WhereBetween on a hasMany relation of an object using the WhereBetween method. This is the error I get though.
Method 'WhereBetween' not found in class 'illumainate\Database\Eloquent\Relations\HasMany.
list($from, $to) = $params->get('range');
$week = $property->week()
->whereBetween('WeekDate', array($from, $to))
->get();
The same error occurs if I try WhereRaw instead of WhereBetween. hasMany class doesn't have WhereRaw or WhereBetween. Is there a way around this?
I had a similar issue in Laravel 5.2 with the following code:
$events = Events::whereBetween('start', array($start, $end))
->whereBetween('end', array($start, $end))
->get();
This throws an error like:
Call to undefined method App\Events::whereBetween()
Somehow in Laravel 5.0+ I could not use whereBeetween directly on the model. But in the API documentation whereBetween is still listed as function of the DB class (see here). By replacing the model with the DB class we can fix it:
use DB;
...
$events = DB::table('events')
->whereBetween('start', array($start, $end))
->whereBetween('end', array($start, $end))
->get();