我正在尝试使用Laravel 4中的Mail Class,而我无法将变量传递给$ m对象.
$ team对象包含我从数据库中获取的具有eloquent的数据.
Mail::send('emails.report', $data, function($m)
{
$m->to($team->senior->email, $team->senior->first_name . ' '. $team->senior->last_name );
$m->cc($team->junior->email, $team->junior->first_name . ' '. $team->junior->last_name );
$m->subject('Monthly Report');
$m->from('info@website.com', 'Sender');
});
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我得到一个错误,其中$ team对象不可用.我想它与范围有关.
有任何想法吗 ?
我正在Laravel制作我的第一个应用程序,并试图让我的头围绕会话flash消息.据我所知,在我的控制器操作中,我可以设置一条flash消息
Redirect::to('users/login')->with('message', 'Thanks for registering!'); //is this actually OK?
Run Code Online (Sandbox Code Playgroud)
对于重定向到另一条路线的情况,或
Session::flash('message', 'This is a message!');
Run Code Online (Sandbox Code Playgroud)
在我的主刀片模板中,我有:
@if(Session::has('message'))
<p class="alert alert-info">{{ Session::get('message') }}</p>
@endif
Run Code Online (Sandbox Code Playgroud)
:正如你可能我在我的应用程序中使用自举3,并想使不同的消息类别的使用已经注意到alert-info,alert-warning,alert-danger等.
假设在我的控制器中我知道我正在设置什么类型的消息,在视图中传递和显示它的最佳方式是什么?我应该在会话中为每种类型设置单独的消息(例如Session::flash('message_danger', 'This is a nasty message! Something's wrong.');)吗?然后我需要为我的刀片模板中的每条消息添加一个单独的if语句.
任何建议表示赞赏
如何在两列上设置唯一约束?
class MyModel extends Migration {
public function up()
{
Schema::create('storage_trackers', function(Blueprint $table) {
$table->increments('id');
$table->string('mytext');
$table->unsignedInteger('user_id');
$table->engine = 'InnoDB';
$table->unique('mytext', 'user_id');
});
}
}
MyMode::create(array('mytext' => 'test', 'user_id' => 1);
// this fails??
MyMode::create(array('mytext' => 'test', 'user_id' => 2);
Run Code Online (Sandbox Code Playgroud) 我正在查看特定帖子的作者发布的所有评论.
foreach($post->user->comments as $comment)
{
echo "<li>" . $comment->title . " (" . $comment->post->id . ")</li>";
}
Run Code Online (Sandbox Code Playgroud)
这给了我
I love this post (3)
This is a comment (5)
This is the second Comment (3)
Run Code Online (Sandbox Code Playgroud)
我如何通过post_id订购,以便上面的列表被命令为3,3,5
我是使用Laravel和Homestead的新手,并且非常感谢任何帮助或正确的方向.当我运行"php artisan serve"时,我已经成功地进入了"你已经到达"屏幕但是当我尝试通过Vagrant做同样的事情时,我得到"没有指定输入文件".我的Homestead.yaml文件如下所示:
authorize: /Users/me/.ssh/id_rsa.pub
keys:
- /Users/me/.ssh/id_rsa
folders:
- map: /Users/me/code/exampleproject
to: /home/vagrant/code/exampleproject
sites:
- map: exampleproject.app
to: /home/vagrant/code/exampleproject/public
variables:
- key: APP_ENV
value: local
Run Code Online (Sandbox Code Playgroud)
在我的电脑上,我有以下目录:
/Users/me/code/Homestead
/Users/me/code/exampleproject //this is the directory created with composer
Run Code Online (Sandbox Code Playgroud)
在我的Vagrant Box上我出于某种原因有两个名为"code"和"Code"的目录:
/home/vagrant/code/exampleproject
/home/vagrant/Code
Run Code Online (Sandbox Code Playgroud)
我已经检查过,我可以看到对我的计算机exampleproject文件所做的更改反映在vagrant box文件中.
不太确定如何解决这个问题!我真的很感激任何可能的帮助:)
我有一个脚本,我使用php artisan(使用root用户)运行,有时它会导致在apache www-data用户之前创建每日日志文件- 这意味着当真实用户使用我的Web应用程序时,我得到文件夹权限错误:
无法打开流:权限被拒绝
我每次都将权限更改回www-data,但我想通过始终使用正确的权限创建日志文件来解决此问题.
我考虑创建一个创建文件或触摸它的cron作业,以确保它每天都有正确的权限,但我正在寻找一个不依赖于另一个脚本的更好的解决方案.
我们还考虑将php artisan包装在另一个脚本中,以确保它始终使用www-data凭据运行,但我们想要做的事情实际上是不允许apache 执行的root程序.
还有什么建议吗?
我有多对多的关系设置和工作,将项目添加到我使用的购物车:
$cart->items()->attach($item);
Run Code Online (Sandbox Code Playgroud)
这会将项目添加到数据透视表中(应该如此),但如果用户再次单击链接以添加他们已添加的项目,则会在数据透视表中创建重复项.
是否有内置的方法将记录添加到数据透视表只有一个尚不存在?
如果没有,我如何检查数据透视表以查找是否已存在匹配的记录?
我有两个相关的模型:Category和Post.
该Post模型具有published范围(方法scopePublished()).
当我尝试获取该范围的所有类别时:
$categories = Category::with('posts')->published()->get();
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
调用未定义的方法
published()
类别:
class Category extends \Eloquent
{
public function posts()
{
return $this->HasMany('Post');
}
}
Run Code Online (Sandbox Code Playgroud)
帖子:
class Post extends \Eloquent
{
public function category()
{
return $this->belongsTo('Category');
}
public function scopePublished($query)
{
return $query->where('published', 1);
}
}
Run Code Online (Sandbox Code Playgroud) 我和Laravel 4的Pagination课一起使用Eloquent.
问题:当URL中有一些GET参数时,例如:http://site.com/users?gender=female&body=hot,生成的分页链接只包含page参数而没有其他内容.
刀片模板
{{ $users->link() }}
Run Code Online (Sandbox Code Playgroud)
有一个->append()功能,但是当我们不知道有多少GET参数时,我们怎样才能append()在分页链接中包含其他GET参数而不if会让我们的刀片模板搞乱一大堆代码?
我想使用Eloquent ORM通过以下SQL获得价值.
- SQL
SELECT COUNT(*) FROM
(SELECT * FROM abc GROUP BY col1) AS a;
Run Code Online (Sandbox Code Playgroud)
然后我考虑了以下内容.
- 代码
$sql = Abc::from('abc AS a')->groupBy('col1')->toSql();
$num = Abc::from(\DB::raw($sql))->count();
print $num;
Run Code Online (Sandbox Code Playgroud)
我正在寻找更好的解决方案.
请告诉我最简单的解决方案.