eloquent

How to create a Eloquent model instance from a raw Object?

徘徊边缘 提交于 2019-12-22 04:44:13
问题 I need to make a raw database query using Laravel: $results = DB::select("SELECT * FROM members INNER JOIN (several other tables) WHERE (horribly complicated thing) LIMIT 1"); I get back a plain PHP StdClass Object with fields for the properties on the members table. I'd like to convert that to a Member (an Eloquent model instance), which looks like this: use Illuminate\Database\Eloquent\Model; class Member extends Model { } I'm not sure how to do it since a Member doesn't have any fields set

Left join to get a single row in Laravel

泪湿孤枕 提交于 2019-12-22 04:39:36
问题 I have been unsuccessfully trying to leftjoin and get the required data Here is my code: $album = Albums::->where('users_id',$user_id) ->leftJoin('photos',function($query){ $query->on('photos.albums_id','=','albums.id'); $query->where('photos.status','=',1); // $query->limit(1); // $query->min('photos.created_at'); }) ->where('albums.status',1)->get(); The comments are some of my several trying... I want to get only a single record from the photos table matching the foreign key album_id which

Eloquent firstOrCreate documentation or usage

早过忘川 提交于 2019-12-22 04:39:20
问题 I'm attempting to seek a table to see if a column named "name" exists if so return the value and if not create that row with a null value i saw firstOrCreate but i cannot figure out how to use it for the life of me. This is what i currently have, can someone lend a hand? class Settings extends Eloquent { protected $table = 'settings'; protected $primaryKey = 'name'; public static function get($settingName) { return self::firstOrCreate(array('name' => $settingName)); // return self::select

Multiple Where In

℡╲_俬逩灬. 提交于 2019-12-22 04:19:13
问题 I'm using eloqent to filter a set of products: Product::whereIn('color', $color)->whereIn('size', $size)->whereIn('price', $price)->get(); Each of the above variables is an array of ids $color = [1,2,4,5] My question is, is this inefficient when the user fails to send through a set of variables, say they did not want any color filters so the array would be: $color = []; I've tried ->toSql and it produces the sql statement: select * from `products` where `color` in (?, ?) and 0 = 1 and `price`

Custom Laravel Relations?

妖精的绣舞 提交于 2019-12-22 03:58:28
问题 Hypothetical situation: Let's say we have 3 models: User Role Permission Let's also say User has a many-to-many relation with Role , and Role has a many-to-many relation with Permission . So their models might look something like this. (I kept them brief on purpose.) class User { public function roles() { return $this->belongsToMany(Role::class); } } class Role { public function users() { return $this->belongsToMany(User::class); } public function permissions() { return $this->belongsToMany

Manually add item to existing object [Laravel 5]

杀马特。学长 韩版系。学妹 提交于 2019-12-22 03:37:28
问题 Here is what I try to do: $q = Question::where('id',$id -> id)->get(); $q[] = $q->push([ 'test' => true]); dd($q); This will output: Collection {#220 ▼ #items: array:3 [▼ 0 => Question {#225 ▶} 1 => array:1 [▼ "test" => true ] 2 => null ] } So 'test' => true will append as a new key, but I want to insert it in Question so latter I can access to it like this with foreach $q -> test So here is how I want access to item: @foreach($q as $qq) {{ $qq->test }} @endforeach 回答1: It can be done by

How to order by pivot table data in Laravel's Eloquent ORM

那年仲夏 提交于 2019-12-22 03:17:49
问题 In my Database, I have: tops Table posts Table tops_has_posts Table. When I retrieve a top on my tops table I also retrieve the posts in relation with the top. But what if I want to retrieve these posts in a certain order ? So I add a range field in my pivot table tops_has_posts and I my trying to order by the result using Eloquent but it doesn't work. I try this : $top->articles()->whereHas('articles', function($q) { $q->orderBy('range', 'ASC'); })->get()->toArray(); And this : $top-

How to order by pivot table data in Laravel's Eloquent ORM

别说谁变了你拦得住时间么 提交于 2019-12-22 03:17:10
问题 In my Database, I have: tops Table posts Table tops_has_posts Table. When I retrieve a top on my tops table I also retrieve the posts in relation with the top. But what if I want to retrieve these posts in a certain order ? So I add a range field in my pivot table tops_has_posts and I my trying to order by the result using Eloquent but it doesn't work. I try this : $top->articles()->whereHas('articles', function($q) { $q->orderBy('range', 'ASC'); })->get()->toArray(); And this : $top-

How to soft delete related records when soft deleting a parent record in Laravel?

こ雲淡風輕ζ 提交于 2019-12-22 01:55:38
问题 I have this invoices table that which has the following structure id | name | amount | deleted_at 2 iMac 1500 | NULL and a payments table with the following structure id | invoice_id | amount | deleted_at 2 2 1000 | NULL Invoice Model class Invoice extends Model { use SoftDeletes; } here's the code to delete the invoice public function cance(Request $request,$id) { $record = Invoice::findOrFail($id); $record->delete(); return response()->json([ 'success' => 'OK', ]); } Payments model class

Using withTrashed with relationships in Eloquent

一世执手 提交于 2019-12-22 01:27:53
问题 Is there a way to use withTrashed with relationships in Eloquent. What I need is this. I have table and model Mark and another table User . User has many Mark and Mark belongs to User . So I defined this in Eloquent models. Now I need to get an instance of Mark that is soft deleted. This is not a problem if User isn't soft deleted, but if both Mark and User are soft deleted, I get an error Trying to get property of non-object , because $mark->user won't return actual user, cause it is soft