我正在使用 Laravel 5.5 和 Dusk 2.0。我有以下 html。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body class="my-body-class" id="my-body-div">
<div class="my-content-class" id="my-content-div">
Content goes here.
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的黄昏测试。
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/test/admin-fixed-layout');
$this->assertNotNull($browser->element('.my-content-class'));
$this->assertNotNull($browser->element('#my-content-div'));
// $this->assertNotNull($browser->element('.my-body-class'));
$this->assertNotNull($browser->element('#my-body-div'));
});
}
Run Code Online (Sandbox Code Playgroud)
如果我取消注释使用主体类选择器的断言,则测试失败。为什么?
我试图从我的产品表中获取独特品牌的数量,以及来自 Laravel 集合的数量。
我能够使用产品的特定查询来做到这一点,但我现在使用集合的原因是因为我还想获得产品的产品来源(国家/地区)、条件(使用/新),我认为它使用来自一个查询的集合会更好,而不是对每个数据使用三个单独的查询。
下面的代码有效,但没有显示每个唯一品牌的计数。
这是我的控制器
$products = DB::table('products')
->select('products.*')
->whereNull('products.deleted_at')
->get();
$BrandCollection = collect($products);
$Brands = $BrandCollection->unique('Brand')->sortBy('Brand')->keyBy('Brand')->pluck('Brand');
Run Code Online (Sandbox Code Playgroud)
所以,我正在寻找的结果是
惠普 3
东芝 2
联想 1
我认为可以使用 concat 来完成集合,但由于我使用的是 Laravel 5.2,我正在寻找其他解决方案。
这是在Laravel中迭代集合的代码:
$usersData = $users->mapWithKeys(function ($item) {
return [$item->id => array("name" => $item->name, "email" => $item->email, "id" => $item->id)];
});
Run Code Online (Sandbox Code Playgroud)
我试图获得$usersData具有自定义key和值作为数组的数组。
但是结果我得到了:
array:1 [ 0 => array:3 [ "name" => "Doctor" "email" => "doctor@il.com" "id" => 2 ]]
Run Code Online (Sandbox Code Playgroud)
相反,键2我有0数组元素的键。
laravel laravel-collection laravel-5.3 laravel-5.4 laravel-5.5
我正在系统中获取我的帖子的输出,但是我收到了这个 errror Call to a member function diffForHumans() on null...你能帮帮我吗?谢谢...
文章模型是:
protected $fillable=[
'user_id','content','live','post_on'
];
public function setLiveAttribute($value)
{
$this->attributes['live']=(boolean)($value);
}
Run Code Online (Sandbox Code Playgroud)
而文章控制器是
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function create()
{
return view('articles.create');
}
public function store(Request $request)
{
Article::create($request->all());
}
public function show()
{
}
public function update()
{
}
public function destroy()
{
}
Run Code Online (Sandbox Code Playgroud) 我尝试了各种方法来解决这个问题,比如清除缓存、composer 更新/安装,但之后php artisan config:cache,它又出现了。
在 config.php 第 839 行:
调用未定义的方法 Illuminate\Validation\Rules\In::__set_state()
整页变成空白页。在bootstrap/cache/config.php文件中,它显示:
'environment' =>
array (
'form' =>
array (
'rules' =>
array (
'app_name' => 'required|string|max:50',
'environment' => 'required|string|max:50',
'environment_custom' => 'required_if:environment,other|max:50',
'app_debug' =>
array (
0 => 'required',
1 =>
Illuminate\Validation\Rules\In::__set_state(array(
'rule' => 'in',
'values' =>
array (
0 => 'true',
1 => 'false',
),
)),
),
Run Code Online (Sandbox Code Playgroud) 我的routes.php我有以下内容:
<?php
Route::group(array(), function () {
View::share('roots', Category::roots()->get());
$tree = Category::get()->toHierarchy()->toArray();
View::share('categories', $tree);
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'));
});
Run Code Online (Sandbox Code Playgroud)
当我的数据库还没有表,我想做php artisan migrate时,结果是:SQLSTATE [42S02]:找不到基表或视图:1146表'ae_dev.categories'不存在
我的迁移文件:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('categories', function(Blueprint $table) {
$table->increments('id');
$table->integer('parent_id')->nullable()->index();
$table->integer('lft')->nullable()->index();
$table->integer('rgt')->nullable()->index();
$table->integer('depth')->nullable();
$table->string('name', 255);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('categories');
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel 5.4,如何对两列进行两次排序?
例如:
ArticleController.php
public function index()
{
$articles = Auth::user()->articles->sortByDesc('updated_at')->sortByDesc('status');
return view('index', compact('articles'));
}
Run Code Online (Sandbox Code Playgroud)
我使用了sortByDesc()两次,但结果不符合规定,我该怎么办?
更新:
User.php
public function articles()
{
return $this->hasMany('App\Article');
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个帮助程序来在 mysql 中运行自定义函数,我正在尝试匹配
数据库中找到的参数,因此如果成功,它应该返回 X。
刀刃 :
@foreach($ficha as $est)
@foreach($est->equipos as $eq)
@foreach($eq->parametros as $param)
<p>{{ tiene_parametro_ficha($param->nombre ,$ficha->first()->id) }}</p> //FUNCTION, pass 2 values
@endforeach
@endforeach
@endforeach
Run Code Online (Sandbox Code Playgroud)
帮手:
function tiene_parametro_ficha($param, $id_est)
{
$queries =DB::select("tiene_parametro_ficha($param, $id_est)"); //I tried alias Result , it gives error too
}
Run Code Online (Sandbox Code Playgroud)
MySQL 功能:
BEGIN
DECLARE mivar INT;
SET @PAR=PARAM;
SET @ID_EST = ID_EST;
SELECT (if((p.nombre = @PAR),1,0)) INTO mivar
FROM estacion AS e
INNER JOIN equipo_estacion AS ee ON ee.estacion_id = @ID_EST
INNER JOIN equipo AS …Run Code Online (Sandbox Code Playgroud) 我正在使用集合过滤数据.但我需要使用类似的方法.我曾试图这样写:('name', 'LIKE', '%value%')但它没有用.
这是我的方法:
protected function filterData(Collection $collection, $transformer) {
foreach (request()->query() as $query => $value) {
$attribute = $transformer::originalAttribute($query);
if (isset($attribute, $value)) {
$collection = $collection->where($attribute, $value);
}
}
return $collection;
}
Run Code Online (Sandbox Code Playgroud) 我在我的网站上为管理员构建了一个cms界面.除其他外,管理员可以使用表单添加\编辑用户信息.当我发送编辑表单时,我不断收到此错误:Column not found: 1054 Unknown column 'updated_at' in 'field list'这表明数据库更新正在尝试保存所有请求索引(包含其他表中列的值),而不仅仅是我正在尝试更新的索引.
我已设法将问题跟踪到一行$user_role->save();.上面的行做他们想做的事情(找到thr correcct user_role并改变它的值).
这是我的代码
模型
static public function update_user($request, $id){
$image_name = '';
if( !empty($request['profile_image']) && $request->hasFile('profile_image') && $request->file('profile_image')->isValid() ){
$file = $request->file('profile_image');
$image_name = date('Y.m.d.H.i.s') . '-' . $file->getClientOriginalName();
$request->file('profile_image')->move( public_path() . '/images/profile-images/' , $image_name);
$img = Image::make( public_path() . '/images/profile-images/' . $image_name );
$img->resize(370, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save();
}
$user = self::find($id);
$user->name = $request['name'];
$user->email = $request['email'];
$user->phone = $request['phone']; …Run Code Online (Sandbox Code Playgroud)laravel ×9
laravel-5 ×8
php ×5
laravel-5.5 ×4
eloquent ×3
laravel-5.4 ×2
collections ×1
laravel-4 ×1
laravel-5.2 ×1
laravel-5.3 ×1
laravel-dusk ×1
php-carbon ×1