小编Fer*_*art的帖子

Laravel:从控制器抛出错误

我有一个项目,如果我想访问partner/X我会get property of non object出错,因为我的合作伙伴比 X 少。

我的问题。如何告诉控制器,那if the result of the modelquery is empty, than throw a 404 error

我的代码到目前为止:

public function showPartner($id = 0){

   //Only allow numerical values    
  if ($id > 0){

    $partner = Partner::find($id);

    if (empty($partner)){
        return ???
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

php laravel

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

Laravel:子文件夹中的lang文件

我想"多本地化"我的Laravel项目.

我创建了这样的目录结构:

lang
- en
 - front
  - contact.php
-footer.php
Run Code Online (Sandbox Code Playgroud)

我像这样建立了我的页脚:

{{ link_to('/', trans('footer.frontpage'))}}
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但是当我想要本地化其他刀片页面时,例如这样的contact us页面:

@lang('front.contact.name')
Run Code Online (Sandbox Code Playgroud)

或这个:

{{ __('front.contact.name') }}
Run Code Online (Sandbox Code Playgroud)

或这个:

{{ trans('front.contact.name') }}
Run Code Online (Sandbox Code Playgroud)

我只回到了页面上:

front.contact.name

有什么问题?

php localization laravel laravel-5

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

Laravel:检查 MySQL 查询是否有结果

第一个 Laravel 项目。我有一个控制器功能,用于检查是否有任何带有该条形码的记录。如果没有插入记录。如果是,则为计数加一。

public function sellmode(Request $request){
    $barcode=$request->barcode;
    $test = DB::select('select count from sellmode where barcode = ?', [$barcode]);
    $row_cnt = mysqli_num_rows($test);
    if ($row_cnt == 0) {
        Debugbar::info('Új sor');
        DB::insert('insert into sellmode (barcode, count) VALUES (?, ?)', [$barcode, '1']); 
    } else {
        Debugbar::info('Meglév? frissítése');
        DB::update('update sellmode set count = count + 1 WHERE barcode = ?' [$barcode]);
    }
    return view(sell);
Run Code Online (Sandbox Code Playgroud)

}

当我尝试它时,它有以下错误:

SellController.php 第 17 行中的 ErrorException:mysqli_num_rows() 期望参数 1 为 mysqli_result,给定数组

我做错了什么?

php mysql laravel laravel-5

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

Laravel:雄辩的“大于”和“小于”

我想在查询构建器中使用这样的数据库查询:

SELECT * FROM posts WHERE active = 1 AND published <= '{$now}' LIMIT 5

我做了什么:

$now = new Carbon;
$feed = Post::where([
        ['active' => 1],
        ['published' => $now]
    ])
    -> take(5)
    -> get()
    -> toArray();
Run Code Online (Sandbox Code Playgroud)

但它就像:

SELECT * FROM posts WHERE active = 1 AND published = '{$now}' LIMIT 5

如何让<<=>>=<>LIKE与报表::where的方法?

php mysql laravel eloquent laravel-5.5

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

Laravel:找不到Layouts.master

我在Laravel中构建了demosite,但是我收到了以下错误:

FileViewFinder.php第137行中的ErrorException:未找到视图[layouts.master].(查看:/var/www/html/project/laravel/laravel/resources/views/page.blade.php)

master.blade.php位于page.blade.php旁边,两者都在资源/视图中

master.blade.php

<html>
    <head>
        <title>@yield('title')</title>
    </head>
    <body>
        @section('sidebar')
        This is the master sidebar.
        @show
        <div class="container">
        @section('content')
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

page.blade.php

@extends('layouts.master')
@yield('title', 'Page Title')
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<h2>{{$name}}</h2>
<p>This is my body content.</p>
@endsection
Run Code Online (Sandbox Code Playgroud)

出了什么问题?我在本教程中工作.

php laravel laravel-5

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

Jquery addClass 不是函数

我想要的是:

我想制作一个表单,根据联系人类型显示不同的输入字段(用户可以从下拉列表中选择联系人类型)。我尝试过 jQuery,但我有两个错误。

我如何尝试:

function showContact() {
/* */
    $('#userType').change(function(){
        var value = $(this).val();

        console.log(value);
        var member = document.getElementById("private");
        var corporation = document.getElementById("corporation");

        if (value == 1) {
            member.removeClass('hidden');
            corporation.removeClass('hidden').addClass('hidden');

            return;
        } else if (value == 2) {
            member.removeClass('hidden').addClass('hidden');
            corporation.removeClass('hidden');

            return;
        } else {
            member.removeClass('hidden').addClass('hidden');
            corporation.removeClass('hidden').addClass('hidden');

            return;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

什么错误:

1:脚本仅在第二个下拉列表更改时启动。我想从第一个变化开始。

2:当我更改时,出现此错误:

类型错误:member.removeClass 不是函数

有什么问题?

html javascript jquery

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