function connection() on null when use database - laravel

i'm new on lumen , i start a lumen project with some table. but when I want get some table use this code
Course::all();
lumen said Call to a member function connection() on null
FatalErrorException in Model.php line 3245:
Call to a member function connection() on null
what's wrong ?

in lumen by default Eloquent is off , you should enable that.
for that you should write this code on your app.php in bootstrap folder
$app->withEloquent()

Related

Laravel PHPUnit Call to a member function connection() on null

I am learning testing on Laravel and I am unable to continue testing Eloquent by this error:
Error: Call to a member function connection() on null
This is my code:
public function test_users_can_follow_each_other()
{
$this->userOne = User::find(1);
$this->userTwo = User::find(2);
$this->userOne->followedBy($this->userTwo->id);
$this->assertTrue($this->userTwo->isFollowedBy($this->userOne->id));
}
I am also using the trait use DatabaseTransactions;
The database is already created with 2 records on users table. Do I need to config anything else, thanks!
replace
PHPUnit\Framework\TestCase
with
Tests\TestCase

upload a row to database table in laravel 4.2

Use Illuminate\Http\Request;
public function registerAStudent(Request $request)
{
Student::Create($request->all());
}
I want to upload a row to the database table.
Form data is stored in the request variable and using Student model.
but can't update the database I'm getting following error:
ErrorException (E_RECOVERABLE_ERROR) HELP Argument 1 passed to
AdminController::registerAStudent() must be an instance of
Illuminate\Http\Request, none given
please give me a proper way to do it.

Laravel 5.4 route name not working

In my routes on web.php I have the following line
Route::get('/', 'DashboardController#create')->name('dashboard');
In my DashboardController.php I have a create function with the following line like I saw on a Laracast tutorial but it's not working.
return redirect()->dashboard();
I get the following error
(1/1) FatalThrowableError
Call to undefined method Illuminate\Routing\Redirector::dashboard()
What could I be doing wrong?
You should use
return redirect()->route('dashboard');
this is the way to do it.
visit Named Routes for more info
return redirect()->dashboard(); calls a method named as dashboard inside your controller and that's what error is saying
(1/1) FatalThrowableError
Call to undefined method
Illuminate\Routing\Redirector::dashboard()
You need to call named routes like this
return redirect()->route('dashboard');
For deep insight always trust laravel docs
Instead of:
return redirect()->dashboard();
Try:
return redirect()->route('your-route-name');

Laravel 5.4 Getting Call to a member function fill() on null

I am trying to save image on database to be used as background for the website. The problem is, I am getting Call to a member function fill() on null
Here is what I did
https://paste.laravel.io/ZoWBM
This simply means there is no any record in SiteSettings model with name_setting = $key.
You should always do a check like this:
if (is_null($siteSettingsUpdate)) {
// There is no such record in DB.
}

Trying to get property of non-object laravel on command dispatch

I am using "Mapping Command Properties From Requests" getting this error
Trying to get property of non-object
My Code
use App\Command\CreateSomethingCommand;
public function store(Request $request)
{
$this->dispatchFrom(CreateSomethingCommand::class,$request,['user'=>Auth::User()->id]);
.....
}
Where as the command stated at laravel
https://laravel.com/docs/5.0/bus#dispatching-commands
Please Help!
The error is most likely triggered by this part of your code Auth::User()->id. If the user is not logged in then the result of Auth::user() will be null and since null is not an object you can't access the id property on it, thus throwing the exception:
Trying to get property of non-object
To fix that you can use Auth::id() which will return the ID of the logged in user or null otherwise, but you'll avoid the exception.

Resources