小编Mar*_*łek的帖子

为什么我不能在 Laravel Dusk 中使用 body 类选择器?

我正在使用 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 laravel-5 laravel-dusk laravel-5.5 laravel-dusk2

1
推荐指数
1
解决办法
780
查看次数

获取 Laravel 5.2 Collection 中列名的唯一值计数

我试图从我的产品表中获取独特品牌的数量,以及来自 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,我正在寻找其他解决方案。

php laravel laravel-5 laravel-collection laravel-5.2

1
推荐指数
1
解决办法
2742
查看次数

如何使用自定义键mapWithKeys返回数组?

这是在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

1
推荐指数
1
解决办法
1947
查看次数

在 null 上调用成员函数 diffForHumans()

我正在系统中获取我的帖子的输出,但是我收到了这个 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)

laravel eloquent php-carbon laravel-5

1
推荐指数
1
解决办法
3548
查看次数

调用未定义的方法 Illuminate\Validation\Rules\In::__set_state()

我尝试了各种方法来解决这个问题,比如清除缓存、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)

php laravel laravel-5 laravel-validation laravel-5.5

1
推荐指数
2
解决办法
5960
查看次数

未找到Laravel基表或视图

我的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)

php laravel laravel-4 laravel-routing

0
推荐指数
1
解决办法
3331
查看次数

如何在Laravel中用两列对两次排序?

我正在使用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)

php laravel eloquent laravel-5 laravel-5.4

0
推荐指数
1
解决办法
1594
查看次数

从 laravel 助手调用自定义 mysql 函数

我创建了一个帮助程序来在 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)

mysql-error-1064 laravel laravel-5

0
推荐指数
1
解决办法
7043
查看次数

Laravel 5.5 Collection喜欢的地方

我正在使用集合过滤数据.但我需要使用类似的方法.我曾试图这样写:('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)

php collections laravel-5 laravel-collection laravel-5.5

0
推荐指数
1
解决办法
4277
查看次数

使用laravel编辑用户信息

我在我的网站上为管理员构建了一个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 eloquent laravel-5 laravel-migrations

0
推荐指数
1
解决办法
32
查看次数