我正在关注Laracasts的视频:基本模型/控制器/查看工作流程.
我有一张表保存联系信息.
CREATE TABLE `about` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Run Code Online (Sandbox Code Playgroud)
我试图使用控制器文件中的以下代码将数据传递给视图:
public function index()
{
$about = Page::where('page', 'about-me')->get(); //id = 3
return view('about', compact('about'));
}
Run Code Online (Sandbox Code Playgroud)
当我尝试显示如下所示的代码时,
@section('title')
{{$about->title}}
@stop
@section('content')
{!! $about->content !!}
@stop
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
此集合实例上不存在属性[title].(查看:E:\ laragon\www \newsite\resources\views\about.blade.php)
但是如果我在控制器文件中更改检索方法,它就可以工作.
public function index()
{
$about = Page::find(3);
return view('about', compact('about'));
}
Run Code Online (Sandbox Code Playgroud)
当我dd($about)在第一个case(where()->get())中使用时,数据由数组封装.在第二种情况(find(3))中,它按预期显示数据.
我究竟做错了什么?
嗨,我正在学习laravel.我使用Eloquent ORM删除方法但我得到了不同的结果.不是true或false但是null.我设置了一个资源路由,在UsersController中有一个destroy方法.
public function destroy($id){
$res=User::find($id)->delete();
if ($res){
$data=[
'status'=>'1',
'msg'=>'success'
];
}else{
$data=[
'status'=>'0',
'msg'=>'fail'
];
return response()->json($data);
Run Code Online (Sandbox Code Playgroud)
但我总是得到一个响应{"status":"0","msg":"failed"},删除数据库中的记录.
然后我使用dd($ res).它在页面中显示为null.
但是从我学习的课程中它返回一个布尔值true或false.
我的代码中有错误吗?
你能告诉我一些其他方法,当我从数据库中删除数据时,我可以得到一个布尔结果吗?
产品型号
public function country() {
return $this->hasMany('App\Models\ProductCountry', 'product_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)
控制器
$product = Product::where('mall_' . $this->mall_id, '=', 1)
->whereHas('country', function ($i) use ($me) {
return $i->where('country_id', $me->country_id);
})
->find($key);
Run Code Online (Sandbox Code Playgroud)
原始SQL:
select * from `product` where `mall_3` = '1' and exists (select * from `product_country` where `product_country`.`product_id` = `product`.`id` and `country_id` = '109') and `product`.`id` = '3' and `product`.`deleted_at` is null limit 1
Run Code Online (Sandbox Code Playgroud)
上面的SQL返回没有结果(当产品id = 3时
在SQL下面返回正确的结果(当产品id = 6时)
select * from `product` where `mall_3` = '1' and exists (select * …Run Code Online (Sandbox Code Playgroud) 我没有太多的运气从Laravel的方式解决这个问题.所以我提出了两个问题.
鉴于我有一辆汽车并且该汽车可以有许多功能,但功能也按功能类型分隔,如何为所述汽车返回所有功能,将功能类型分开?
我有四个表,listing_features是数据透视表:
我有以下代码,它产生我需要的东西,但是当我使用它时,我得到一个Laravel错误......"在字符串上调用成员函数addEagerConstraints()"...因为我这样调用它:
Listing::with(
'features',
)->get();
Run Code Online (Sandbox Code Playgroud)
我用来生成我想要的格式(不是坚定的)数据的代码是
public function features()
{
$out = array();
$features = $this->hasMany('App\Models\ListingFeature', 'listing_id', 'id')->select('feature_id')->get();
$types = ListingFeatureType::all();
foreach($types as $key => $obj){
$out[$key]['listing_feature_type_id'] = $obj->id;
$out[$key]['name'] = $obj->listing_feature_type;
$out[$key]['features'] = ListingFeatureValue::whereIn('id', $features->toArray())->where('listing_feature_type_id', '=', $obj->id)->get()->toArray();
}
return json_encode($out);
}
Run Code Online (Sandbox Code Playgroud)
哪个回报:
[
{
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"name": "Safety",
"features": [
{
"id": "0ed0ad63-a6ed-4818-8c6f-ee048694dcd9",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_text": "Anti-Lock Brakes"
},
{
"id": "37abeef2-dc22-4995-8503-f89962242ea6",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_text": "Collision Detection"
},
{
"id": "3c0728e1-91f7-4f44-ac0b-429eda816692",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6", …Run Code Online (Sandbox Code Playgroud) 我的laravel雄辩是这样的:
$products = Product::where('status', 1)
->where('stock', '>', 0)
->where('category_id', '=', $category_id)
->groupBy('store_id')
->orderBy('updated_at', 'desc')
->take(4)
->get();
Run Code Online (Sandbox Code Playgroud)
执行时,会出现如下错误:
SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT列表的表达式#1不在GROUP BY子句中,并且包含非聚合列'myshop.products.id',它在功能上不依赖于GROUP BY子句中的列; 这与sql_mode = only_full_group_by不兼容(SQL:select*from
productswherestatus= 1 andstock> 0 andcategory_id= 5 group bystore_idorder byupdated_atdesc limit 4)
我该如何解决?
在我的应用程序已经更新从关系one-to-many来many-to-many,我试图找出一种方法来坚持相关的功能.
假设我有两张相关的桌子,例如狗和主人.如果我拥有一系列所有者,并且我正在尝试为这些所有者获取狗ID列表,我该如何雄辩地做到这一点?
类似的问题在这里被问到:https: //laracasts.com/discuss/channels/laravel/getting-many-to-many-related-data-for-an-array-of-elements
那么,我如何获得阵列中的Dog模型Owner呢?
同样的事情,$associatedDogs = Dog::whereIn('owner_id',$ListOfOwners)->get();是一个One-To-Many关系,但对Many-to-Many.
class User extends Authenticatable
{
public function company() {
return $this->belongsTo('App\Company');
}
}
Run Code Online (Sandbox Code Playgroud)
class Company extends Model
{
public function users() {
return $this->hasMany('App\User');
}
}
Run Code Online (Sandbox Code Playgroud)
id name companies_id
id名称
我正在尝试将公司的名称附加到用户身上.
$user = User::findOrFail(Auth::user()->id);
$companyName = $user->company()->first()->name;
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息 Trying to get property of non-object
我没有得到我所缺少的东西......提前谢谢你
当我在生产中运行我的DB种子脚本时,会发生内存耗尽.
下面是我的种子脚本.
class MembershipTableSeeder extends Seeder
{
public function run()
{
DB::table('members')->delete();
foreach (range(1, 99) as $days){
Members::create(array('membership_code' => 'test'.$days));
}
DB::unprepared(file_get_contents(app_path()."/database/seeds/members.sql"));
}
}
Run Code Online (Sandbox Code Playgroud)
所以我所做的是在我的种子脚本上添加一个无限制.
ini_set('memory_limit', '-1');
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我运行脚本时,它会将输出记录到终端中,SQL脚本的内容(非常非常大).
有没有一种在我的数据库种子中运行SQL转储的好方法,它不会消耗太多内存?我现在做的是手动运行:
mysql -uuser -p db < script.sql
Run Code Online (Sandbox Code Playgroud) 我正在将项目移植到Laravel.
我有两个数据库表,彼此之间处于一对多关系.它们有三个条件.如何在Eloquent中建立这种关系模型?
我不应该修改数据库模式,因为它必须保持向后兼容其他东西.
我尝试了以下,但它不起作用.
拥有方:
use Illuminate\Database\Eloquent\Model;
class Route extends Model
{
public function trips()
{
return $this->hasMany('Trip', 'route_name,source_file', 'route_name,source_file')
}
}
Run Code Online (Sandbox Code Playgroud)
反面:
use Illuminate\Database\Eloquent\Model;
class Trip extends Model
{
public function routes()
{
return $this->belongsTo('Route', 'route_name,source_file', 'route_name,source_file');
}
}
Run Code Online (Sandbox Code Playgroud)
示例Route数据库值:
id | route_name | source_file
---------------------------------------
1 | Berlin - Paris | file1.xls
2 | Madrid - London| file2.xls
3 | Berlin - Paris | file3.xls
Run Code Online (Sandbox Code Playgroud)
示例Trip数据库值:
id | route_name | source_file | duration
--------------------------------------------------------- …Run Code Online (Sandbox Code Playgroud) 以下文章如何使用Laravel Eloquent创建多个where子句查询?
我试图插入多个'和'条件:
$matchThese = ['destination.country' => 'china', 'doc.description' => 'business'];
return $collection->where($matchThese);
Run Code Online (Sandbox Code Playgroud)
但是我收到这个错误:
Too few arguments to function Illuminate\Support\Collection::where(), 1 passed . . . but two expected
Run Code Online (Sandbox Code Playgroud) laravel-eloquent ×10
laravel ×9
php ×7
eloquent ×3
laravel-5 ×3
mysql ×2
laravel-4 ×1
laravel-5.3 ×1
many-to-many ×1
sql ×1