小编Lau*_*nce的帖子

具有任意数量的URL段的Laravel 4路由

我有一个L3应用程序,我正在尝试移植到L4.在L3版本中,我的一条路线是

Route::get('/(:any)/(:all?)', etc...
Run Code Online (Sandbox Code Playgroud)

这允许我处理任意数量的URL段,例如:

/contact_page
/store_category
/store_category/shirts_category
/store_category/shirts_category/specific_shirt_page
/an/arbitrary/number/of/nested/categories
Run Code Online (Sandbox Code Playgroud)

但是在L4中我无法弄清楚如何模仿(:all?)的功能

以下代码有效:

Route::get('/{arg1?}/{arg2?}/{arg3?}', function($arg1='home', $arg2, $arg3)
{
  //do something
});
Run Code Online (Sandbox Code Playgroud)

所以我可以添加大量的可选参数(超过我认为在实际使用中我会需要的),但这不是很优雅.

在Laravel 4中是否有某种方法来定义可以响应任意数量的URL段的Route?

php url-routing laravel laravel-4

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

如何在Laravel 4中将变量发送到主布局?

如何在Laravel 4中将变量发送到主布局?

这里$ categories在主布局中不可用:

protected $layout = 'layouts.master';

public function index()
{
    $categories = Category::all();
    $data = array('categories' => $categories);
    $this->layout->content = View::make('catalog.categories', $data);
}
Run Code Online (Sandbox Code Playgroud)

我的主要观点:

@yield('content')

@if($categories)
   <h1>TEST</h1>
@endif
Run Code Online (Sandbox Code Playgroud)

"测试"没有出现!

laravel laravel-4

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

有没有办法在Laravel 4中配置自动路由?

我是Laravel 4的新手,来自codeigniter,我一直在尝试在Laravel中编写代码并来回查看他们的文档,我的问题是,有没有办法让路由能够自动检测控制器的方法?

我一直在用

Route::resource('admin','AdminController');
Run Code Online (Sandbox Code Playgroud)

但它似乎只提供相同的结果

Route::get('admin','AdminController@index');
Run Code Online (Sandbox Code Playgroud)

即使在包中也会出现同样的问题,我一直在尝试开发一个模块化的应用程序,就像我在codeigniter中可以实现的那样,其中有模块文件夹来放置不同的视图和控制器.

我只是不希望每次我有一个新方法的方式我也需要在路线中注册,如果你要更新或增强你的控制器方法,它有点像很多工作.

laravel laravel-4

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

php artisan serve - 如何让mysql运行

我对所有这些工匠东西都不熟悉。当我启动我的 php 服务器时

$ php artisan serve 
Run Code Online (Sandbox Code Playgroud)

如何让mysql服务器运行?

php mysql laravel-4

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

将外键添加到现有表Laravel 4

我无法在现有表中添加外键,我的第一次迁移是:

public function up() {
    Schema::create('clases_inventario', function($t) {
                $t->increments('id');
                $t->integer('grupos_inventario_id');
                $t->integer('laboratorio_id');
                $t->string('codigo', 4);
                $t->string('descripcion', 64);
                $t->boolean('estado')->default(true);
                $t->timestamps();
            });
}
Run Code Online (Sandbox Code Playgroud)

我的第二次迁移是添加forein键

public function up() {

        Schema::table('clases_inventario', function($table) {
                    $table->foreign('grupos_inventario_id')->references('id')->on('grupos_inventario');
                });
    }
Run Code Online (Sandbox Code Playgroud)

然后我运行php artisan migration并输出下一个错误

 [Exception]
 SQLSTATE[HY000]: General error: 1005 Can't create table 'bd_e_soft.#sql-818
 _55' (errno: 150) (SQL: alter table `clases_inventario` add constraint clas
 es_inventario_grupos_inventario_id_foreign foreign key (`grupos_inventario_
 id`) references `grupos_inventario` (`id`)) (Bindings: array (
 ))
Run Code Online (Sandbox Code Playgroud)

laravel laravel-4

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

Laravel 4查询构建器 - 未找到空列

Route::get('/test', function(){
    return DB::table('table')->where_not_null('column')->get();
});
Run Code Online (Sandbox Code Playgroud)

在Laravel 4中,我有以下路线,显然更换了表格和列.我在控制器中有它,但它没有工作,所以我把它移到测试路线.

我得到的错误是:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '_not_null' in 'where clause' (SQL: select * from `table` where `_not_null` = column)
Run Code Online (Sandbox Code Playgroud)

https://tower.la.utexas.edu/docs/database/fluent#where

文件说:

return DB::table('table')->where_null('column')->get();
Run Code Online (Sandbox Code Playgroud)

php sql laravel laravel-4

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

Laravel重定向图像路径

使用GET变量时,我的图像路径被重定向到公用文件夹之外.这是我的纲要(任何改善这一点的建议也会受到赞赏,但不是必要的:

路线:

Route::get('retailers/{country}', array(
    'as' => 'find-retailers',
    'uses' => 'SiteController@getRetailers')
);
Run Code Online (Sandbox Code Playgroud)

控制器:

   public function getRetailers($country)
    {
     // Query Builder
    $locations = DB::table('retailers_listings')
        ->orderBy('country', 'asc')
        ->where('country', $country)
        ->get();

        return View::make('retailers.stores')
        ->with('retailers_listings', $locations);
    }
Run Code Online (Sandbox Code Playgroud)

编辑:

上面的路由和控制器是我用来处理视图stores.blade.php.在我的视图文件夹中,我有一个名为retailers的子目录,它包含2个视图.index.bladestores.blade.

/views
 /retailers
   index.blade.php
   stores.blade.php
Run Code Online (Sandbox Code Playgroud)

在index.blade.php中工作的图像路径如下: public/uploads/retailers/logos/image.png

图像路径无法在stores.blade.php中工作,如下所示: /public/retailers/uploads/retailers/logos/image.png

public/retailers应该从文件夹中加载图像public.

php laravel

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

模型/控制器变量 - Laravel

我一直在我的控制器中使用Query构建器,但是想要使用eloquent和query builder.我想知道在两者之间传递变量的最佳实践.我已经通过文档但仍然遇到问题.

以下是我在控制器中执行操作的方法:

public function getRetailers($city) {    
    $locations = DB::table('retailers_listings')
        ->orderBy('country', 'asc')
        ->Where('city', $city)
        ->get();

        $this->layout->header = $city;
    $content = View::make('retailers.stores')
        ->with('header', $this->layout->header)
        ->with('store_listings', $locations)

        if (Request::header('X-PJAX')) {
        return $content;
        } else { 
            $this->layout->content = $content; 
        } 
    }
Run Code Online (Sandbox Code Playgroud)

我如何以雄辩和模特的方式完成上述工作?如何从视图中将变量$ city传递给模型.更深入的建议然后文档给我的将是伟大的.

php eloquent laravel-4

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

Laravel Undefined Offset:1 - 由路由引起

我在尝试拨打电话时收到错误消息 /api/subject/search

我认为这是一个我遗漏的简单语法错误

我的api路线定义如下

Route::group(array('prefix' => 'api'), function()
{
    Route::post('resource/search', 'ResourceController');
    Route::resource('resource', 'ResourceController');


    Route::post('subject/search', 'SubjectController');
    Route::resource('subject', 'SubjectController');

    Route::resource('user', 'UserController');

    Route::controller('/session', 'SessionController');
    Route::post('/login', array('as' => 'session', 'uses' => 'SessionController@Store'));
});
Run Code Online (Sandbox Code Playgroud)

我的控制器大多是空的

class SubjectController extends \BaseController 
{
    public function search()
    {
        $subjects = [];
        if((int)Input::get('grade_id') < 13 && (int)Input::get('grade_id') > 8)
            $subjects = Subject::where('name', 'like', '%HS%')->get();
        else
            $subjects = Subject::where('name', 'not like', '%HS%')->get();

        return Response::json([
            'success' => true,
            'subjects' => $subjects->toArray()
        ]);
    }
    /**
     * Display a listing of the resource.
     * …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-4

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

laravel数据库查询`where`总是需要`first()`?

我是laravel的新手,对一些查询方法感到困惑.

find($id)是有用的,并返回一个很好的数组,但有时我需要选择其他字段而不是id.

Laravel文档说我可以使用where('field', '=', 'value')并返回一堆数据,这很好.

我无法理解的是为什么我需要->first()每次都添加,即使我非常确定只有一行匹配查询.

laravel eloquent

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

标签 统计

laravel ×8

laravel-4 ×8

php ×6

eloquent ×2

mysql ×1

sql ×1

url-routing ×1