抱歉,如果标题不完全有意义......我不确定模型链接的确切术语是否是其控制器中的相关数据。
我正在尝试使用用户创建的帖子对表格进行分页。
我之前设法对其进行了分页,而帖子表中没有 user_id。但我已经添加user_id到posts表中,并运行工作正常迁移。
以下是我的Post模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的User模型:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = ['name', 'email', 'password',];
protected $hidden = ['password', 'remember_token',];
public function posts() {
return $this->hasMany('App\Post');
}
}
Run Code Online (Sandbox Code Playgroud)
这是后控制器的相关部分:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
use Session;
use Carbon\Carbon;
class PostController extends …Run Code Online (Sandbox Code Playgroud) 我正在构建一个Laravel站点,并希望在我构建它(电话,ipad等)时在其他设备上测试它.
据我了解,这样做的方法是运行php artisan serve --host=0.0.0.0.
我的问题是......有没有办法为主机定义默认值,并将其设置为0.0.0.0,这样我就可以简单地运行php artisan serve它会自动运行0.0.0.0?
我想根据我的环境拥有动态播种机。(例如,在测试中我只想要种子 100 行,而本地则是 10'000)。
我在配置中创建了 seeder.php,它从 .env 文件调用值。
当我在我的播种机中使用配置时,它返回一个字符串,该字符串应该是一个整数。例如:
.env.local:
SEED_USER_COUNT=10000
Run Code Online (Sandbox Code Playgroud)
配置\播种机.php:
return [
'user_count' => env('SEED_USER_COUNT', 10),
];
Run Code Online (Sandbox Code Playgroud)
用户播种机
factory(User::class, config('user_count'))->create();
Run Code Online (Sandbox Code Playgroud)
以上无法正常工作,似乎config('user_count')返回一个字符串"10000"而不是整数10000
我对工厂和种子完全陌生,直到现在我一直在向 Sequel Pro 或 Workbench 中的每个项目手动添加数据。
总的来说,我对 Laravel 相当陌生,但我认为我已经正确设置了模型。
我有一个可以包含多种成分的食谱,并且这些成分可以属于多个食谱。
两者之间的枢轴有一个数量和单位列(后者可以为空)。
食谱.php
class Recipe extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function ingredients(){
return $this->belongsToMany('App\Ingredient');
}
}
Run Code Online (Sandbox Code Playgroud)
成分.php
class Ingredient extends Model
{
public function recipes(){
return $this->belongsToMany('App\Recipe');
}
}
Run Code Online (Sandbox Code Playgroud)
原料工厂.php
$factory->define(App\Ingredient::class, function (Faker $faker) {
return [
'name' => $faker->word,
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
];
});
Run Code Online (Sandbox Code Playgroud)
配方工厂.php
$factory->define(App\Recipe::class, function (Faker $faker) {
$userIds = User::all()->pluck('id')->toArray();
return [
'title' => $faker->text(30),
'description' => …Run Code Online (Sandbox Code Playgroud) 我对此有点陌生,最初试图检查我的模型是否返回结果isEmpty(),但我想我应该尝试count()一下,然后我遇到了以下情况:
我有以下代码,它从我的模型返回数据:
$results = Game::where('code', '=', $code)->with('genre', 'creator')
无论我使用first()还是get()结合count(result),$results->count()我都会得到不同的值,我不确定为什么。
使用时->first()
dd($results->count()) = 11930 // Number of rows in the db
Run Code Online (Sandbox Code Playgroud)
使用时->get()
dd($results->count()) = 1 // What I'd expect the query to return
Run Code Online (Sandbox Code Playgroud)
使用时->first()
dd(count($results)) = "count(): Parameter must be an array or an object that implements Countable"
Run Code Online (Sandbox Code Playgroud)
使用时->get()
dd(count($results)) = 1
Run Code Online (Sandbox Code Playgroud)
我不明白 1)为什么首先使用时,计数与数据库中的每一行相同。2)为什么count()不能与first().
有谁能解释一下为什么我不能像我想的那样先使用 count on 吗?
更新:
我也无法使用->isEmpty() …
laravel ×5
php ×4
artisan ×1
count ×1
dotenv ×1
eloquent ×1
laravel-5.6 ×1
pagination ×1
phpdotenv ×1