Lumen: Using Models without Eloquent - laravel

Is it possible to have Eloquent disabled in lumen bootstrap file and still use Lumen (Eloquent) Models?

Short answer: Thanks to #El_Matella for his correct answer. It's impossible to use Lumen Models without having Eloquent enabled.
Description of problem I faced: I was unable to use lumen models while having eloquent disabled. I added a custom validator in AppServiceProvider boot method and boom! Lumen models works! What happens is that ValidationServiceProvider enables eloquent:
https://github.com/laravel/framework/blob/5.3/src/Illuminate/Validation/ValidationServiceProvider.php#L57
$this->app->singleton('validation.presence', function ($app) {
return new DatabasePresenceVerifier($app['db']);
});
$app['db'] causes following function calls:
./vendor/illuminate/validation/ValidationServiceProvider.php(57): Illuminate\Container\Container->offsetGet('db')
./vendor/illuminate/container/Container.php(1182): Laravel\Lumen\Application->make('db')
Which Application->make('db') is equal to $app->withEloquent()!

Related

Is the '#' syntax in laravel routes depreciated?

Long time since i last coded in laravel, and when i tried registering a simple route in the API routes:
Route::post('/register', 'AuthController#register');
I got "Target class [AuthController] does not exist." error. I made it work by registering with:
use App\Http\Controllers\AuthController;
Route::post('/register', [AuthController::class, 'register']);
Confused, i gave a look at the docs and didn't find any reference to the first syntax. Is it gone and i am not knowing or i am doing something wrong?
The change was introduced in Laravel 8. Previously, routes were namespaced in the RouteServiceProvider:
// ...
protected $namespace = 'App\Http\Controllers';
That value comes set as null since v8+. That's why you have that error response. So you have two options:
A) Add the prefix in the RouteServiceProvider
B) Use the FQCN and import classes as you solved it (recommended, helps IDEs and static analysis AFAIK)

Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically

Hi I am using laravel Sentinel as my Auth, also I am trying to use laravel auditing I am getting "Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically".
In my user model I have added a static function resolveId() for adding user_id in Laravel Auditing 'audits' table
public static function resolveId(){
return Sentinel::getUser()->getUserId();
//return auth()->check() ? auth()->user()->getAuthIdentifier() : null;
}
When I try to use \Sentinel::getUser() I am getting the error below.
Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically
From the docs:
After installing the package, open your Laravel config file located at config/app.php and add the following lines.
In the $aliases array add the following facades for this package.
'Sentinel' => Cartalyst\Sentinel\Laravel\Facades\Sentinel::class,
Then just add this to the top of the class:
use Sentinel;
Put this use on top of the file in question:
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
I'm aware that the package version #manikandan k was asking help for is 4.x or 5.x, and while the documentation does mention the use case for Sentinel, it doesn't provide an actual example.
Since version 6.x, the Audit Resolvers documentation has this very use case, where Sentinel is used instead.
I suggest updating the resolver logic to the following:
return Sentinel::check() ? Sentinel::getUser()->getUserId() : null;
This will prevent calling getUserId() on null, when a user isn't logged.

Laravel 5.3 - how do I set fetchmode in query builder?

I'm working with legacy code. The old report engine uses associative arrays, Laravel's query builder returns an array of objects.
I need to turn objects into arrays. I've tried using:
\DB::connection('tars-test') //->setFetchMode(PDO::FETCH_ASSOC)
but that gets me Class 'App\Http\Controllers\PDO' not found
It's been suggested to put ->all() at the end of the query but that throws error Call to a member function all() on array
The most efficient way would be to set the fetchmode at runtime, for the legacy function and just for the legacy function. How do I do it?
You can use 'toArray' method:
https://laravel.com/docs/5.3/collections#method-toarray
Laravel 5.3 and lower
You went the right way but as you can see laravel is trying to find PDO in App\Http\Controllers\PDO which probably means you forgot to add use Illuminate\Database\PDO;
Laravel 5.4 and up
Since laravel 5.4 this is not an option. But you still can set fetch mode globally: Laravel 5.4 - How to set PDO Fetch Mode?
Or if you still wish to change it just locally:
Return values only (no keys/associative array) in Laravel

Laravel about Requests

i am new in programing laravel. so here I will not ask about my problems but I am here just want to ask how to use laravel. here I would like to ask how to use:
Determining If an Input Value Is Present in laravel. and what it does?
thanks ^^
Laravel is really a good MVC Framework.
I can suggest you some source from where you can get better understanding of Laravel Framework.
https://laravel.com/docs/5.2
https://laracasts.com/skills/laravel
For simple example - How to use Laravel after installation
Go to path using terminal ex. /var/www/laravel/project
Go to - /var/www/laravel/project/resources/views
Create a directory test and create a file index.php
Create a controller - php artisan make:controller TestController
Create a function testGet() and return view - return view('test.index'); //test is directory and index is file
Create a function testPost(Request $request) and to get data use $data = $request->all(); and then print this data.
Create a model with migration file - php artisan make:model Test --migration
Go to route file - /var/www/laravel/project/app/Http/routes.php
Add line Route::get('/test', 'TestController#testGet');
Add line Route::post('/test', 'TestController#testPost');
Now check GET request to your project http://project/test it will call testGet function.
POST request http://project/test?name=Adam will call testPost function and will print your name.
as said in the comments you better check laracasts! its the laravel school
anyway to anwser your question, its simple
View
<input type="text" name="fname">
Controller
public function doingstuff (Illuminate\Http\Request $request) {
if ($request->has('fname')) {
// a man gotta do what a man gotta do here
}
}
smooth huh? :3

How can I render a twig template in a custom controller in Silex?

I'm playing with Silex microframework to build a very simple app.
The Silex documentation briefly illustrates how to keep your code organised using controller as class and I've also found this useful article talking about the same practice:
https://igor.io/2012/11/09/scaling-silex.html
but still can't solve my problem
The issue:
in my app.php I'm using
$app->get('/{artist}', 'MyNamespace\\MyController::getAlbum');
This is working. MyController is a class correctly loaded through composer using psr-4.
At the moment the return method of getAlbum($artist) is return $player;
What I'd like to do instead, is returning a twig view from getAlbum, something like:
return $app['twig']->render('player.twig', $player);
To do so, what I've tried to do in my custom class/controller is:
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
[...]
public function getAlbum(Request $request, Application $app, $artist)
but this is generating the following error when I try to access the routed pages:
ReflectionException in ControllerResolver.php line 43:
Class MyNamespace\Request does not exist
Whic made me think that there's a namespace conflict between myNamespace and the Silex namespaces?!
What am I doing wrong?
Is this the right way to make $app visible in my custom controller in order to use return $app['twig']... ?
Thank you in advance!
EDIT
After several other tries still didn't get to the point (replies still welcome!) but I've found a workaround solution that could be useful to anyone will incur in a similar issue. Directly in my app.php I added this
$app->get('/{artist}', function (Silex\Application $app, $artist) use ($app)
{
$object = new MyNamespace\MyController();
$player = $object->getAlbum($artist);
return $app['twig']->render('player.twig',
array(
//passing my custom method return to twig
'player' => $player,));
});
then in my player.twig I added:
{{player | raw}}
And this basically means that I still need an anonymous function to get use of my custom method which is a working solution I'm not really happy with because:
I'm using 2 functions for 1 purpose.
The return value of getAlbum is dependent from the use of "raw" in twig.
SOLVED
The workflow described works fine. It was a distraction error: I've placed the namespace of my custom class after use Symfony\Component\HttpFoundation\Request;
namespace declaration in PHP needs always to be at the top of the file, Silex wasn't able to injects $app and $request for this reason.

Resources