小编Phi*_*rks的帖子

有没有办法检测Laravel是否存在数据库表

我希望能够使用创建表

Schema::create('mytable',function($table)
{
    $table->increments('id');
    $table->string('title');
});
Run Code Online (Sandbox Code Playgroud)

但在此之前,我想检查表是否已经存在,或许类似于

Schema::exists('mytable');
Run Code Online (Sandbox Code Playgroud)

但是,上述功能不存在.我还能用什么?

php laravel

69
推荐指数
4
解决办法
6万
查看次数

无法在Laravel 4中切换语言

我尝试使用路由来切换语言,但没有任何变化.你能帮我吗,请问?

Route::get('lang/{lang}', function($lang)
{
    App::setLocale($lang);
    return Redirect::to('/');
});
Run Code Online (Sandbox Code Playgroud)

laravel laravel-4

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

Laravel 4雄辩的透视台

我有三个表:users,items和user_items.用户拥有许多项目,而项目属于许多用户.

表格:

Users
id
username
password

Items
id
name
equipable

User_items
id
user_id
item_id
equipped
Run Code Online (Sandbox Code Playgroud)

型号:

class User extends Eloquent {
     public function items()
     {
         return $this->belongsToMany('Item', 'user_items')
                   ->withPivot('equipped');
     } 
}

class Item extends Eloquent {
    public function users()
    {
         return $this->belongsToMany('User', 'user_items');
    }
}
Run Code Online (Sandbox Code Playgroud)

在pivot(user_items)表中,我有一个非常重要的列"配备".

我有一个用户可以装备,取消装备和扔物品的表格.此表单有一个隐藏字段,其中包含pivot(user_items)表行id.因此,当用户试图装备物品时,系统检查物品是否可以装备.

所以,我想要一个带有数据透视数据和项目数据的对象,基于数据透视表中的item_id,我可以发送给处理程序(处理所有逻辑).

所以我要做的是先访问数据透视表,然后访问项目表.

像这样的东西(不起作用):

$item = User::find(1)->items->pivot->find(1);
Run Code Online (Sandbox Code Playgroud)

这可能吗?

php laravel eloquent laravel-4

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

Laravel 4:如何使用迁移创建非主要自动递增列?

我目前正在开发一个由主服务器和许多客户端组成的Laravel 4项目.客户端创建数据并将其发送到主服务器.为避免冲突,我使用UUID v4作为主键.

但是,一旦在服务器上创建数据,我想分配一个唯一的自动递增整数,以便用户更容易识别数据.例如:而不是说item 5a8e896d-3ab4-48d2-9d39-faeb5227f012一个用户可以谈论item #24567

为了保持我的应用程序可管理我正在使用迁移,我对此表的当前迁移如下所示:

public function up()
{
    Schema::table('items', function($table)
    {
        $table->create();
        $table->string('id')->primary(); //'id' For the purpose of keeping the ORM working, this field stores the UUID.
        $table->integer('number', true); //The human readable item number, the second parameter is true for auto-increment
        $table->text('otherdata');
        $table->timestamps();
    });
}
Run Code Online (Sandbox Code Playgroud)

问题是Laravel在定义自动增量时会自动创建主键,因此迁移最终会失败,因为有两个主键.

[Exception] SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined
  (SQL: alter table `items` add primary key items_id_primary(`id`)) (Bindings: array ())
Run Code Online (Sandbox Code Playgroud)

有没有办法使用Laravel 4迁移使用主键和单独的自动递增字段.

php mysql database-design laravel laravel-4

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

在多个关系laravel4的情况下更新数据透视表

我最近开始使用Laravel4.在多个关系的情况下,我在更新数据透视表数据时遇到一些问题.

情况是:我有两个表:Product,ProductType.它们之间的关系是多对多的.我的模特是

class Product extends Eloquent {
    protected $table = 'products';
    protected $primaryKey = 'prd_id';

    public function tags() {
        return $this->belongsToMany('Tag', 'prd_tags', 'prta_prd_id', 'prta_tag_id');
    }
}

class Tag extends Eloquent {
    protected $table = 'tags';
    protected $primaryKey = 'tag_id';
        public function products()
    {
    return $this->belongsToMany('Product', 'prd_tags', 'prta_prd_id', 'prta_tag_id');
    }
}
Run Code Online (Sandbox Code Playgroud)

在将数据插入数据透视表prd_tags时,我做了:

$product->tags()->attach($tag->tagID);
Run Code Online (Sandbox Code Playgroud)

但是现在我想更新此数据透视表中的数据,将数据更新到数据透视表的最佳方法是什么.比方说,我想删除一些标签并为特定产品添加新标签.

many-to-many pivot-table laravel eloquent laravel-4

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

Laravel 4 beta3和beta4之间对Input :: json()函数的更改

当我在Laravel4 Beta3中开发时,我曾经使用Input :: json()函数从服务获取JSON POST数据,但是当我更新到Laravel4 Beta4时,我收到以下错误:

注意:未定义的属性:/Applications/MAMP/htdocs/commonDBAPI/app/controllers/UserController.php第47行中的Symfony\Component\HttpFoundation\ParameterBag :: $ productName

有没有人有任何想法,可能是什么原因.

谢谢,

laravel laravel-4

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

在Laravel中生成随机数

我正在开发一个关于Laravel的项目,我想以这种格式生成一个随机数:任意位置的一个字符和其余的整数.示例:C87465398745635,87474M745436475,98487464655378J8等,这是我的控制器:

    function generatePin( $number ) {
    $pins = array();
    for ($j=0; $j < $number; $j++) { 
        $string = str_random(15);
        $pin = Pin::where('pin', '=', $string)->first();
        if($pin){
            $j--;
        }else{
            $pins[$j] = $string;
        }
    }



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

但它似乎是生成这样的其他东西:ZsbpEKw9lRHqGbv,i7LjvSiHgeGrNN8,pyJEcjhjd3zu9Su我已尽力而为但没有成功,请任何帮助解决方案将不胜感激,谢谢

php string random laravel

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

Laravel-4将空PUT数据发送到资源丰富的控制器

天儿真好,

我遇到通过Chrome Postman向控制器发出PUT请求的问题,PUT数据不存在,POST数据工作正常.

我之前已经执行过编写器更新,以确保最新版本的供应商产品可用,甚至删除了bootstrap/compiled.php.

还有其他人有类似问题吗?

响应中的section_id和data都为空的更新函数:

public function update($id)
{
    $section_id = Input::get('section_id');

    $data = Input::all();

    return Response::json(array('id' => $id, 'section_id' => $section_id, 'data' => $data));
}
Run Code Online (Sandbox Code Playgroud)

我已经一直调试代码到ParameterBag.php和$ this->请求的参数列表是空的,我不确定什么应该包含任何值,但所有通过代码输入值为空.不知道现在该做什么,缺少使用post而不是put.

laravel laravel-4

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

Laravel 4:将数据从make传递给服务提供商

以下代码说明了一切......

// routes.php
App::make('SimpleGeo',array('test')); <- passing array('test')

// SimpleGeoServiceProvider.php
public function register()
{
    $this->app['SimpleGeo'] = $this->app->share(function($app)
    {
        return new SimpleGeo($what_goes_here);
    });
}

// SimpleGeo.php
class SimpleGeo 
{
    protected $_test;

    public function __construct($test) <- need array('test')
    {
        $this->_test = $test;
    }
    public function getTest()
    {
        return $this->_test;
    }
}
Run Code Online (Sandbox Code Playgroud)

laravel laravel-4

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

Laravel 4 Eloquent ORM通过动态属性访问一对一关系

我试图在'Users'表和'User_profiles'表之间建立一个非常简单的关系模型.每个用户都有一个user_profile,所以它是一对一的简单.根据发现的文档@ http://four.laravel.com/docs/eloquent#one-to-one我已将以下功能添加到我的用户模型中:

public function user_profile()
{
    return $this->hasOne('User_profile');
}
Run Code Online (Sandbox Code Playgroud)

这是我的User_profile模型中定义的关系:

public function user()
{
    return $this->belongsTo('User');
}
Run Code Online (Sandbox Code Playgroud)

我试图从控制器访问像这样:

    // Get current user
    $user = User::find(Auth::user()->id);
    $profile = $user->user_profile;

    print_r($user);
    print_r($profile);

    echo "User's name is: " . $user->user_profile->first_name . ' ' . $user->user_profile->last_name;
Run Code Online (Sandbox Code Playgroud)

不幸的是,打印$ user打印出用户模型字段就好了,但没有显示任何关系的痕迹; $ profile是空的.'relations'数组也是空的,我猜应该填充它.

我正在尝试使用此处建议的"动态属性" http://four.laravel.com/docs/eloquent#dynamic-properties

否则如果我去:

echo "User's name is: " . $user->user_profile()->first()->first_name . ' ' . $user->user_profile()->first()->last_name;
Run Code Online (Sandbox Code Playgroud)

它有效......但我真的不喜欢这样做.

有什么建议?

laravel eloquent laravel-4

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