87.3 laravel中常见问题以及解决方案

87.3 laravel中常见问题以及解决方案

没配置好 重定向文件可能会出的问题
配置的时候出问题查看是否配置.haccess

Options -MultiViews

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]

在windows 系统遇到如下的错误的话
错误:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

其它没有问题后:修改.htaccess 为以下内容,

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

在ngix 环境中配置重写规则:

location / {
    index  index.html index.htm index.php;
    try_files $uri $uri/ /index.php?$query_string ;  
}

如果在宝塔界面的话是在 目录 /www/server/panel/vhost/rewrite

文章目录

    • 查询构造器
    • 获取一列的值
    • laravel 服务提供者
    • 多个laravel项目数据库连接错乱原因和解决方法

原生sql的写法:


$sql = <<<Eof
SELECT
	id,
	question_id,
	answer_id
FROM
	user_study_detail 
WHERE 
	course_id = %u
AND 
	guan_id = %u
AND
	user_id = %u
ORDER BY 
	UNIX_TIMESTAMP(created_at)
DESC

Eof;

	     //防sql注入
		$sql = sprintf($sql,$course_id,$guan_id,$user_id);
		$userTreeQidMapAwid = array();
		$userStudyTrees =  DB::select($sql); 
		

        //获取到某个课程的某一关是否有问题,会查询出有多少个课
        $number_of_examination_questions =  DB::connection("mysql_center")->select($sql);
        $number_of_examination_questions_array = json_decode(json_encode($number_of_examination_questions),true);

涉及到时间段的查询

$lastMonth = $this->rechargeInfo
            ->where('admin_id', $admin_id)// \Auth::id()
            ->when($lastMonthFistDay, function ($query) use ($lastMonthFistDay) {
                $query->whereDate('pay_time', '>=', $lastMonthFistDay);
            })->when($lastMonthFistDay, function ($query) use ($lastMonthLastDay) {
                $query->whereDate('pay_time', '<=', $lastMonthLastDay);
            })->sum('recharge_amount');

使用关联查询

$data=DB::table('article as t1')
            ->select('t1.id','t1.article_name','t2.author_name')
            ->leftjoin('author as t2','t1.author_id','=','t2.id')
            ->get();

更新操作

DB::table('users')
            ->where('id', 1)
            ->update(['votes' => 1]);

artisan命令:

    • 查询构造器
    • 获取一列的值
    • laravel 服务提供者
    • 多个laravel项目数据库连接错乱原因和解决方法

创建普通控制器

php artisan make:controller Backend/SwitchWebController

创建资源控制器

php artisan make:controller Frontend/AdvertMonitorInfoController  --resource

资源控制器 默认的删除方式 不是post或者get 是 delete
不习惯用这个方法的可以考虑重新写路由。

清缓存

php artisan cache:clear
php artisan route:cache
php artisan config:cache

php artisan view:clear
php artisan view:cache

php artisan config:clear
php artisan config:cache

php artisan route:clear
php artisan route:cache

php artisan api:cache

 mkdir -p framework/views
 mkdir -p framework/cache
 mkdir -p framework/sessions






修改路由之后 要记得清除路由缓存

php artisan route:cache

可以使用
php artisan route:list 命令查看自己的路由是否绑定成功。

出现了 Your lock file does not contain a compatible set of packages. Please run composer update. 这个报错,可以执行
composer install --ignore-platform-reqs
这条命令试一下。

Laravel报错Please provide a valid cache path的解决方法

使用Laravel发送邮件没有成功,查看了一下日志发现报了如下错误:

Please provide a valid cache path.
看了下代码发现是因为laravel的缓存路径没有找到,laravel的缓存文件路径是在 config/cache.php中设置的,默认存放在storage文件夹中。
解决方法是:

确保storage目录下有app,framework,views三个目录并有读写权限
确保storage/framework目录下有cache,sessions,views三个目录并有读写权限
如果缺少以上目录,手动创建出来即可。

拼接条件
$where = [];
$where[‘user_id’] = $user->id;
$where[] = [‘created_at’, ‘>=’, $date[‘start’]];
$where[] = [‘created_at’, ‘<’, $date[‘end’]];
c a t e D a t a s = Q u e s t i o n : : s e l e c t ( ′ c a t e g o r y i d ′ , D B : : r a w ( ′ c o u n t ( ∗ ) a s c o u n t ′ ) ) − > w h e r e ( cateDatas = Question::select('category_id', DB::raw('count(*) as count')) ->where( cateDatas=Question::select(categoryid,DB::raw(count()ascount))>where(where)->groupBy(‘category_id’)->get()->toArray();

创建jwt 密码

php artisan jwt:secret

Please provide a valid cache path.

解决方法如下:

1、确保storage目录下有如app,framework,views三个目录。

2、确保storage/framework目录下也有cache,sessions,views三个目录。

laravel的门面模式

核心技术是__callStatic()方法,当调用的方法不存在时,会自动调用魔术方法__callStatic()方法,和__autoload()方法同工异曲。

win10 开机启动添加的地址:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

一些实用网址:

    • 查询构造器
    • 获取一列的值
    • laravel 服务提供者
    • 多个laravel项目数据库连接错乱原因和解决方法

激活phpstorm的网址

laravel
laravel实用手册

查询构造器

获取一列的值

 $ad_account_infos = DB::table('ad_account_info')
            ->pluck('advertiser_id');

laravel 服务提供者

服务提供者,在laravel里面,其实就是一个工厂类。它最大的作用就是用来进行服务绑定。当我们需要绑定一个或多个服务的时候,可以自定义一个服务提供者,然后把服务绑定的逻辑都放在该类的实现中。在larave里面,要自定一个服务提供者非常容易,只要继承Illuminate\Support\ServiceProvider这个类即可

多个laravel项目数据库连接错乱原因和解决方法

问题描述:

我在服务器部署了两个laravel项目,项目A 和 项目B,当我访问项目B的URL时,有时返回的却是项目A 数据库的数据。

然后各种百度,google,才发现 laravel 中通过 getenv() 和 putenv() 这两个函数来获取配置参数,但这两个函数是非线程安全的。

但自已安装的PHP7.2版本又是线程安全模式(ts版),所以就出问题了。

解决方案:

1、更改PHP版本为非线程安全( nts )版本。

2、在config/database.php中写死数据库连接配置,不用 env()。
之前使用这一条解决了问题。

3、如果使用 apache,可以将工作模式设置为 prefork 模式。

laravel 开启sql查询日志
\DB::enableQueryLog();
dd(\DB::getQueryLog());

laravel 速查表

你可能感兴趣的:(laravel,专栏,laravel,php)