简体   繁体   中英

How to handle Laravel Eloquent “WHERE” Query with slash on the value? (Laravel 5.3)

My table is like this :

在此输入图像描述

On the mysql, I try like this :

SELECT * FROM players WHERE player_type = 'App\Models\Player'

Data does not showing

So, I add slash like this :

SELECT * FROM players WHERE player_type = 'App\\Models\\Player'

Data showing

Then, In laravel eloquent, I try like this :

$select = array(
    'player_type'
);
$query = self::where('player_type', '=', 'App\\\Models\\\Player')
             ->paginate(10, $select, 'page', null, null);

Data does not showing

How can I solve it?

Just a different approach here, not the solution.

All your models have the same namespace App\\Models\\... . If so, then just save the actual model in your database without its namespace.

id        player_type
 1        Player
 2        Player

Then later in your controller, you get the palyer type and add the namespace manually when trying to instantiate the Model.

you have to use double quotation "" not ''

$result = DB::table('users')
    ->where("city", "App\\Models\\Player")
    ->get();
    dd($result);

I try this in my localhost and it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM