小编ter*_*ško的帖子

Laravel:orderBy 喜欢计数

在我的网站上,注册用户可以创建帖子并喜欢其他人的帖子。在显示所有帖子时,我可以选择用户可以按标题、日期和喜欢计数排序。我已经完成了标题和日期,但我在按喜欢计数排序时遇到了麻烦。在我的系统中实现此功能的最佳方法是什么。

这是我用于检索和排序帖子的帖子控制器方法:

public function getEvents(Request $request){

    if($request['sort'] == "created_at"){
        $posts = Post::orderBy('created_at', 'desc')->get();
    }
    elseif($request['sort'] == "title"){
        $posts = Post::orderBy('title', 'desc')->get();
    }
    elseif($request['sort'] == "like"){

        //how would I go about ordering by like count here
    }
    return view ('eventspage',  ['posts' => $posts]);
}
Run Code Online (Sandbox Code Playgroud)

这是我的用户选择订购内容的视图:

<form action="{{ route('events') }}">    
    <select name="sort" onchange="this.form.submit()" class="form-control">
        <option value="">Sort By</option>
        <option value="created_at">Date</option>
        <option value="title">Title</option>
        <option value="like">Likes</option>
        <input type="hidden" name="formName" value="sort">
    </select>
</form>
Run Code Online (Sandbox Code Playgroud)

html php laravel eloquent

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

PHP检查$ model是否是许多类中的一个的实例

我想检查$ model是否是A,B或C类的实例,但不是D.所以我有一个这样的数组:

$relevant_classes = [A, B, C];

我知道我可以查看instance of模型是否在这些类的实例中.但是,如果模型是它的一个实例,我现在必须循环遍历数组并询问每个单独的类吗?

我宁愿做if(in_array($model, $relevant_classes))比较类实例的事情.这可能在PHP(Laravel)?

php laravel

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

如何在我的服务中使用学说方法(Symfony 4)?

我在 Symfony 中创建了我自己的第一个服务:

// src/Service/PagesGenerator.php 
Run Code Online (Sandbox Code Playgroud)

namespace App\Service;

class PagesGenerator
{
    public function getPages()
    {

      $page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);

        $messages = [
            'You did it! You updated the system! Amazing!',
            'That was one of the coolest updates I\'ve seen all day!',
            'Great work! Keep going!',
        ];

        $index = array_rand($messages);

        return $messages[$index];
    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误消息:

试图调用类“App\Service\PagesGenerator”的名为“getDoctrine”的未定义方法。

然后我尝试添加到我的 services.yaml 中:

PagesGenerator:
    class: %PagesGenerator.class%
    arguments:
      - "@doctrine.orm.entity_manager"
Run Code Online (Sandbox Code Playgroud)

但后来我收到错误消息:

文件“/Users/work/project/config/services.yaml”在/Users/work/project/config/services.yaml(加载在资源“/Users/work/project/config/服务.yaml”)。

php symfony doctrine-orm

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

错误:文件格式或文件扩展名无效。验证文件未损坏

我想在我的项目中使用 PHPSpreadsheet 库将数据导出到导出的文件中,但是当我尝试打开文件时,此错误显示:excel 无法打开文件,因为文件格式或文件扩展名无效。验证文件未损坏

  • 注意:我在我的项目中使用 MVC,所以控制器中的代码如下:

           protected function Excel($view, $variables = [])
    {
      require_once PATH_LIBRARY_FOLDER.'PhpSpreadsheet\vendor\autoload.php';
      ob_start();
      // Note: make new Spreadsheet object
      $spreadsheet = new Spreadsheet();
      // Note: get current active sheet (frist sheet)
      $sheet = $spreadsheet->getActiveSheet();
      $sheet->setCellValue('A1', 'Hello World !');
    
      extract($variables);
      include($this->viewPath.$view.'.php');
      // Note: set the header to define it is excel file
      header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
      // Note: set the header to define excel file name
      header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
      header("Cache-Control: max-age=0");
    
      // Note: create IOFactory object
      $writer = IOFactory::createWriter($spreadsheet, 'xlsx');
      ob_get_clean(); …
    Run Code Online (Sandbox Code Playgroud)

php phpspreadsheet

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

php函数将字符串分成不同的字符串?

让我说我在这里有这个字符串:$string = 'hello my name is "nicholas cage"'.

我想将这些单词分成不同的字符串,如下所示:

$word1 = 'hello';
$word2 = 'my';
$word3 = 'name';
$word4 = 'is';
$word5 = 'nicholas cage';
Run Code Online (Sandbox Code Playgroud)

对于前4个单词我可以使用爆炸.但是我如何处理word5?我希望名字和姓氏是一个字符串.

php

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

PHP OOP静态属性语法错误

为什么不呢

public static $CURRENT_TIME = time() + 7200;
Run Code Online (Sandbox Code Playgroud)

工作(错误):

解析错误:语法错误,意外'('

class Database {
  public static $database_connection;

  private static $host = "xxx";
  private static $user = "xxx";
  private static $pass = "xxx";
  private static $db = "xxx";

  public static function DatabaseConnect(){
    self::$database_connection = new mysqli(self::$host,self::$user,self::$pass,self::$db);
    self::$database_connection->query("SET NAMES 'utf8'");
    return self::$database_connection;
  }
}
Run Code Online (Sandbox Code Playgroud)

确实有效.

我是OOP的新手,我很困惑.

php static-members

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

CodeIgniter模型不起作用

我需要从我的控制器的数据库中读取一些数据

class Pro_ajax extends CI_Controller{
    public function _construct() {
        parent::_construct();
        $this->load->model('ajax_model');
    }
    public function _remap($method,$param = array())
    {
        if ($method == 'user_search')
        {
            $this->user_search($param[0]);
        }
    }
    private function user_search($text = '')
    {
        $result = $this->ajax_model->ajax_user_search($text);
        $count = count($result);
        $data = array();
        for ($i = 0;$i < $count AND $i < 5;$i++)
        {
            $data[$i][0] = $result->id;
            $data[$i][1] = $result->firstname.' '.$result->lastname;
        }
        echo (json_encode($data));
    }
}
Run Code Online (Sandbox Code Playgroud) 我的模特是:

class Ajax_model extends CI_Model{
    public …
Run Code Online (Sandbox Code Playgroud)

php mysql codeigniter

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

ASP.NET MVC何时成为开源?

这是一个相当简单的问题,但我找不到答案 -

自MV以来,ASP.NET MVC框架是开放源代码,还是仅在发布候选版本时才发布的Codeplex源代码?

asp.net-mvc open-source

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

在注册后的mvc中,新用户不想登录

我创建了mvc web app.我使用会员提供商进行注册.

但是,当我注册一个新用户时,它会自动登录,我不想要这个而不想检查,如果isapproved属性为false,那么它应该继续移动任何其他页面上说

//redirect to Welcome page
return RedirectToAction("Welcome", "Home");
Run Code Online (Sandbox Code Playgroud)

还想检查用户角色.

asp.net-mvc membership-provider asp.net-mvc-3

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

使用带有对象和ViewModel的控制器的MVC中的最佳实践

使用Controller时,建议您公开域实体,或者更好地创建模型.我以此为例:

[HttpPost]
public ActionResult Create(Order order)
{
        if (SaveObject<Order>(order, false))
        {
                return RedirectToAction("Index", new { id = order.CustomerNo });
        }
        else
        {
                ViewData.Model = order;
                return View();
        }
}
Run Code Online (Sandbox Code Playgroud)

如果最好公开一个模型应该包含在哪个模型中?我是否需要创建支持属性,或者可能只需要为当前用例公开我需要的字段?我遇到了"模型注入"这个术语,有人可以解释使用它的含义是什么以及如何实现它?

asp.net-mvc asp.net-mvc-4

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