Saturday, April 15, 2017

Laravel 5.4 route group and resource mess up the parameters

This resource definition is good:

Route::resource('servers', 'ServerController');

Gives:

+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+
| Domain | Method    | URI                   | Name            | Action                                        | Middleware   |
+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+
|        | GET|HEAD  | servers               | servers.index   | App\Http\Controllers\ServerController@index   | web          |
|        | POST      | servers               | servers.store   | App\Http\Controllers\ServerController@store   | web          |
|        | GET|HEAD  | servers/create        | servers.create  | App\Http\Controllers\ServerController@create  | web          |
|        | GET|HEAD  | servers/{server}      | servers.show    | App\Http\Controllers\ServerController@show    | web          |
|        | PUT|PATCH | servers/{server}      | servers.update  | App\Http\Controllers\ServerController@update  | web          |
|        | DELETE    | servers/{server}      | servers.destroy | App\Http\Controllers\ServerController@destroy | web          |
|        | GET|HEAD  | servers/{server}/edit | servers.edit    | App\Http\Controllers\ServerController@edit    | web          |
+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+

But I need to wrap it into a group with prefix like this:

Route::group(['prefix' => 'servers'], function()
{
    Route::resource('/', 'ServerController', ['names' => 'servers']);
});

And here comes the problem, because of prefix the parameterised routes' parameters are empty:

+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+
| Domain | Method    | URI             | Name            | Action                                        | Middleware   |
+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+
|        | GET|HEAD  | servers         | servers.index   | App\Http\Controllers\ServerController@index   | web          |
|        | POST      | servers         | servers.store   | App\Http\Controllers\ServerController@store   | web          |
|        | GET|HEAD  | servers/create  | servers.create  | App\Http\Controllers\ServerController@create  | web          |
|        | GET|HEAD  | servers/{}      | servers.show    | App\Http\Controllers\ServerController@show    | web          |
|        | PUT|PATCH | servers/{}      | servers.update  | App\Http\Controllers\ServerController@update  | web          |
|        | DELETE    | servers/{}      | servers.destroy | App\Http\Controllers\ServerController@destroy | web          |
|        | GET|HEAD  | servers/{}/edit | servers.edit    | App\Http\Controllers\ServerController@edit    | web          |
+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+

So the servers/1 goes 404. Is there any way to fix this problem?



via MrRP

Accessing session variable inside css file laravel

I have a css file name style-usercolors.css.php for dynamic css color in my laravel app. Inside the file I can declare variable and use it, but can't access the session variable.

header("Content-type: text/css");

$primaryColor = session()->has('ORGANISATION_SETTINGS') ? session()->get('ORGANISATION_SETTINGS')['style_settings']['primary_color'] : '#F18805';
$secondaryColor = session()->has('ORGANISATION_SETTINGS') ? session()->get('ORGANISATION_SETTINGS')['style_settings']['secondary_color'] : '#20576B';
$offwhite = '#f6f6f6';



via hizbul25

Laravel Secondary database connection does not work

I have one mysql database connection in config/database.php as below and it is working fine.

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null,
],

I am trying to define another alternate connection (for testing purposes) as below. (Same host but different database):

'mysql_testing' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE_TEST', 'forge'),
    'username' => env('DB_USERNAME_TEST', 'forge'),
    'password' => env('DB_PASSWORD_TEST', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null,
],

where DB_DATABASE_TEST, DB_USERNAME_TEST and DB_PASSWORD_TEST are defined in .env as below:

DB_DATABASE_TEST=db_other
DB_USERNAME_TEST=usr_other
DB_PASSWORD_TEST=secret

However, this second connection does not work.

In tinker, if I try to use the new connection:

DB::connection('mysql_testing')->getPdo()
InvalidArgumentException with message 'Database [mysql_testing] not configured.'

And if I try to check the config values, mysql works but the new connection mysql_testing returns null:

Config::get('database.connections.mysql')
[
 "driver" => "mysql",
 "host" => "127.0.0.1",
 "port" => "3306",
 "database" => "***",
 "username" => "***",
 "password" => "***",
 "charset" => "utf8",
 "collation" => "utf8_unicode_ci",
 "prefix" => "",
 "strict" => false,
 "engine" => null,
]

Config::get('database.connections.mysql_testing')
null

Any idea how may I debug this issue?



via hashbrown

Laravel : QUERY BUILDER SELECT ON WHERE LIKE STATEMENT

I have a Query Builder problem on Laravel 5.4,

I want to select only certain field on where statement.

if I don't put WHERE statement function the SELECT function performed nicely, but when I put WHERE LIKE function then seems the SELECT function not working and it automatically selecting all (*)

Fyi, both user and address table has 'name' field. it only select name from user table. it work nicely.

DB::table('user')
-leftJoin('address', 'user.id', '=', 'address.user_id')
->select('user.name')
->get();

but this is not working

DB::table('user')
-leftJoin('address', 'user.id', '=', 'address.user_id')
->select('user.name')
->where('user.name', 'LIKE', '%jhon%')
->get();

how do I accomplish this? Any anwer will be highly appreciated!



via Angger

How to use traits in Laravel 5.4.18?

I need an example of where to exactly create the file, to write to it, and how to use the functions declared in the trait. I use Laravel Framework 5.4.18

-I have not altered any folder in the framework, everything is where it corresponds-

From already thank you very much.



via emi

Laravel 5.4 password reset using ajax

I have this code:

 $.ajax({
               url:"/password/email", 
               data: {
                     _token: $(".modal-forgotpass-content input[name='_token']").val(),
                     email: $(".modal-forgotpass-content input[name='email']").val()
                },
                type: "POST",
                success: function(data) {
                    alert("Successful : link send");
                },
                error: function(e) {
                     alert("Faild: link not send");
                }

Controller (this function is coming with laravel 5.4) :

 public function sendResetLinkEmail(Request $request)
    {
        $this->validate($request, ['email' => 'required|email']);

        // We will send the password reset link to this user. Once we have attempted
        // to send the link, we will examine the response then see the message we
        // need to show to the user. Finally, we'll send out a proper response.
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($response)
                    : $this->sendResetLinkFailedResponse($request, $response);
    }

Routes :

Auth::routes();

this code its work well ,but he send password reset link to all emails even if the email not exist in database(users table) i want the link to be sent only to the emails already exist in user table any help? Thank you



via Ilham Guezzoula

Retrieve the author of the last comments

I want to retrieve the name of the person who posted the last comment in a topic for a forum. This is what I do at PhpMyAdmin and that works so I get the right values:

select commentaires.auteur 
from commentaires inner join sujets on commentaires.sujet_id = sujets.id
where commentaires.id = (
    select max(commentaires.id)
    from commentaires inner join sujets on commentaires.sujet_id = sujets.id
    where sujets.id = 12
);

I thought using QueryBuilder to retrieve the values I wanted but I realize it's quite complex .. Here is one of my essays:

DB::table('commentaires')->join('sujets', 
'sujets.id','=','commentaires.sujet_id')  
->where(['commentaires.id' => DB::raw('max(commentaires.id')])
->where('sujets.id','=', $unSujet->id)
->value('commentaires.auteur')

Or would you have a simpler idea? Thank you in advance for your help.



via B.Rousseaux

Advertisement