我很擅长使用Laravel,因此对Eloquent来说.我对Eloquent的桌子关系感到困惑.
现在我了解了如何实现简单连接,例如Laravel 4.2文档中的示例,该文档对于一对多关系,其中a comment属于一个post,但a post可以有很多comments.他们使用这种语法从一篇文章中获取评论:
Post::find(1)->comments;
Run Code Online (Sandbox Code Playgroud)
在MySQL中,它可能是:
SELECT * FROM comments
JOIN posts ON posts.id=comments.post_id
WHERE posts.id=1
Run Code Online (Sandbox Code Playgroud)
如果我想要得到的结果是这样的,不仅仅是一行:
SELECT * FROM comments
JOIN posts ON posts.id=comments.post_id
Run Code Online (Sandbox Code Playgroud)
根据我上面给出的例子,我知道它没有多大意义.但是我如何在Eloquent中做到这一点?
为了提供更多细节,我实际上要做的是显示我的两个表的连接结果,assets以及asset_classifications.一个asset属于一个asset_classification,并且asset_classification有许多assets.
我正在尝试显示assets包含其中的表格数据asset_classifications.在MySQL中,它是这样的:
SELECT * FROM assets
JOIN asset_classifications ON asset_classifications.id=assets.classification_id
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Eloquent中执行它?
我的routes.php我有以下内容:
<?php
Route::group(array(), function () {
View::share('roots', Category::roots()->get());
$tree = Category::get()->toHierarchy()->toArray();
View::share('categories', $tree);
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'));
});
Run Code Online (Sandbox Code Playgroud)
当我的数据库还没有表,我想做php artisan migrate时,结果是:SQLSTATE [42S02]:找不到基表或视图:1146表'ae_dev.categories'不存在
我的迁移文件:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('categories', function(Blueprint $table) {
$table->increments('id');
$table->integer('parent_id')->nullable()->index();
$table->integer('lft')->nullable()->index();
$table->integer('rgt')->nullable()->index();
$table->integer('depth')->nullable();
$table->string('name', 255);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('categories');
} …Run Code Online (Sandbox Code Playgroud) 我对以下问题感到困惑:
我有一个看起来像这样的代码
<form action="{{ URL::route('user-send-message') }}" method="post" id="form-user-send-message">
@if( $user_message_block )
<input id="msg-id" name="msg-id" type="hidden" value="{{ $user_message_block->id }}">
@else
<input id="msg-id" name="msg-id" type="hidden" value="0">
@endif
<input id="getter" name="getter" type="hidden" value="{{ $profile->user->username }}">
<textarea id="user-message" name="user-message"></textarea>
<button class="btn btn-success btn-xs pull-right" type="submit" name="btn-send-message">Send</button>
<div class="clearfix"></div>
{{ Form::token() }}
</form>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#form-user-send-message').on('submit', function(){
// ajax post method
$.post(
$(this).prop('action'),{
"_token": $( this ).find( 'input[name=_token]' ).val(),
"msg-id": $( '#msg-id' ).val(),
"getter": $( '#getter' ).val(),
"user-message": $( '#user-message' ).val()
},
function(data){
$(".message-area").append('<div …Run Code Online (Sandbox Code Playgroud) 默认的Vesta CP 0.9.8附带PHP5.4,这使我在Laravel 4.2网站上出现问题.如何将自托管Vesta CP的php版本从5.4升级到最新5.6?
我正在尝试为以下sql进行elqouent查询,
select sum(w.total_item_count) as Day_count, w.created_at as created_at from `sales_flat_orders` as `w` group by DATE(`w`.`created_at`) order by `w`.`created_at` asc
Run Code Online (Sandbox Code Playgroud)
我试过这个
$orderbydate = DB::table('sales_flat_orders as w')
->select(array(DB::Raw('sum(w.total_item_count) as Day_count'),DB::Raw('DATE(w.created_at) as created_at')))
->groupBy('w.created_at')
->orderBy('w.created_at')
->get();
Run Code Online (Sandbox Code Playgroud)
我在sql查询中得到正确的输出,但没有在雄辩的查询中,请帮助,谢谢.
我正在用Laravel 4开发一个应用程序我需要做的是:让我说我有以下路线:
Route::get('/myroute/{entity}/methodname',
);
Run Code Online (Sandbox Code Playgroud)
在其中我需要根据实体变量来决定应该调用哪个Controller和方法,例如:
'MyNameSpace\MyPackage\StudentController@methodname'
Run Code Online (Sandbox Code Playgroud)
如果
entity == Student
Run Code Online (Sandbox Code Playgroud)
并打电话给
'MyNameSpace\MyPackage\StaffController@methodname'
Run Code Online (Sandbox Code Playgroud)
如果
entity == Staff
Run Code Online (Sandbox Code Playgroud)
如何在Laravel 4中完成路由是否有可能或者我必须想出两条不同的路线呢?
Route::get('/myroute/Student/methodname') and Route::get('/myroute/Staff/methodname')
Run Code Online (Sandbox Code Playgroud) 我开始和Laravel做同样的事情,我真的很喜欢Model已经为你的查询做好准备的方式,但我在这种情况下发现了一些问题.
我为一个非常简单的消息传递系统创建了一个模型:
领域是:
id [int] id message
id_user_from [int] id sender user
id_user_to [int] id recipient user
cnt [text] content of message
Run Code Online (Sandbox Code Playgroud)
我想在id_user_from和User(Laravel上的标准)以及id_user_to(相同)之间添加关系.
我能够使用id_user_from或id_user_to创建单个关系,但不能同时创建一个.
我需要删除相册中的照片,所以我认为我需要2个参数路由但我收到一些错误请给我解决方案或其他方式从相册中删除照片
这是我的Routes.php:
Route::get('/admin/album/{albumid}/{id}/delete-photo', array(
'as' => 'admin-photo-delete',
'uses' => 'GalleryController@deletePhoto'
));
Run Code Online (Sandbox Code Playgroud)
并在GalleryController上调用函数deletePhoto,这是GalleryController:
public function deletePhoto($albumID,$photoID){
$photo = Album::find($albumID)->photo()->where('id','=',$photoID)->get();
if($photo){
File::delete(public_path().'/assets/images/gallery/'.$photo->images);
$photo->delete();
return Redirect::route('admin-gallery')->with('message','Photo was deleted successfully');
}
return Redirect::route('admin-gallery')->with('message','Photo delete failed');}
Run Code Online (Sandbox Code Playgroud)
在这里我如何称呼路线:
<a href="{{URL::route('admin-photo-delete',$id,$photo->id)}}">Delete Photo</a>
Run Code Online (Sandbox Code Playgroud)
我已经确定$ id和$ photo-> id不是null但是看看哪个url显示没有第二个参数值所以我得到一些错误:

我有一条路线实际上是运行Artisan命令的“钩子”,它接受一些get参数作为参数:
Route::get('hook', function()
{
$param = Input::get('param');
Artisan::call('myCommand', array('param' => $param));
}
Run Code Online (Sandbox Code Playgroud)
myCommand只需在名为root的目录中创建一个目录,然后创建param一个hello.txt文件。
我可以myCommand很好地使用php artisan myCommand param1它,并且按预期工作。我还可以使用Artisan的tinker命令,并且运行Artisan::call()得也很好。
但是,当我通过访问URL(例如example.com/hook¶m=hello)尝试命令时,我的命令未按预期创建任何内容。如果也有帮助,我正在Laravel Forge上尝试。在我本地的开发机器上,一切正常。
知道有什么问题吗?
我试图从相关的表中获取行:
$Product->where('name', 'LIKE', '%' . $filters['search'] . '%')
->orWhere('brand', 'LIKE', '%' . $filters['search'] . '%');
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为它只是从products表中选择.
我的产品型号是:
class Product extends Eloquent {
protected $table = 'products';
public function brands() {
return $this->hasOne('ProductBrands', 'id', 'brand_id');
}
public function ages() {
return $this->hasOne('ProductAges', 'id', 'age_id');
}
public function types() {
return $this->hasOne('ProductTypes', 'id', 'type_id');
}
public function images() {
return $this->hasMany('ProductImages');
}
public function toArray() {
$ar = $this->attributes;
$ar['brand'] = $this->brand;
$ar['age'] = $this->age;
$ar['type'] = $this->type;
return $ar; …Run Code Online (Sandbox Code Playgroud)