I have set in my model a custom primary key as:
protected $primaryKey = 'id_player';
It is set as the primary key on MySql
But If I do a
$rows = DB::table("players")->find($playerId);
It returns no items (but it exists).
Players Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Players extends Model{
protected $primaryKey = 'id_player';
}
PlayersController
namespace App\Http\Controllers\Players;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Players;
use Illuminate\Support\Facades\DB;
class PlayersController extends Controller{
...
...
}
I'm new on Laravel, what I'm missing?
You are querying from the table.
If you want to use Model primaryKey, you need to access from the Model eloquent
//Supposing your Model Name is User
$row = App\User::find($playerId);
This will work:
$rows = DB::table("players")->where('id_player', $playerId)->first();