I am new to Eloquent, and still getting my head round it, before i put the 'where' clause it it was working fine now i cant seem to get it to work.
$filesRelationship = self::user()->customer->submissions->where('period_timestamp', '=', Input::get('period_timestamp'))->customerfile();
$customerFile = new Customerfile(
array('path' => $savepath, 'document_id' => Input::get('document_id')
)
);
$customerFileResult = $filesRelationship->save($customerFile);
Depending on the relationships from your Models (you did not specify them), but this could work:
$filesRelationship = self::user()
->customer()
->submissions()
->where('period_timestamp', '=', Input::get('period_timestamp'))
->customerfile()
->first();
Related
I am using Laravel with MongoDB and jenssegers package. I have a collection called ProductDetails in which I have three fields for which I need to retrieve the average value: price, margin and weight. I can make three different queries but I would like to get that in one query. I think it would be cleaner.
I tried using raw expressions but I cannot make it work. I can't even retrieve the average of a single value. I have seen many people using mongoDB aggregation but I don't see why and how it would be useful here.
Here's what I'd love to achieve:
$productDetails = ProductDetails::select('AVG(price) as avg_price','AVG(margin) as avg_margin','AVG(weight) as avg_weight')
->where('id', '=', $id)
->where('active', '=', true)
->get();
It works when I try to retrieve the values (not the average) but the select does not work with AVG() which is why I wanted to use raw expressions
This is an unfruitful attempt:
$productDetails = ProductDetails::raw(function($collection)
{
return $collection ->find([
'name' => 'First product',
'avg_price' => ['$avg' => "price"]
]);
})
you just have to use selectRaw :
$productDetails = ProductDetails::selectRaw('AVG(price) as avg_price, AVG(margin) as avg_margin, AVG(weight) as avg_weight')
->where('id', '=', $id)
->where('active', '=', true)
->get();
be careful about the ' location, it should be around the hole statement in select raw expressions.
but :
->where('id', '=', $id)
this statment will make the query return only one row, I think you mean
->where('product_id', '=', $id)
is not?
This is the query I tried to do:
$products = Product::with(['cross_selling.product_related' =>
fn($query) => $query->where('status_id', '=', '2')])
->where('status_id',2)
->get();
I get this results:
In cross_selling I still get id:12 that doesn't has any product_related. I need some help for making that the query doesn't give me that item if product_related is null.
Tks.
First get only those cross selling which has any related products after that get related products. Use the whereHas condition on cross_selling.
$products = Product::with(['cross_selling' => function($qry){
$qry->whereHas('product_related', function ($q) {
$q->where('status_id', 2);
});
}, 'cross_selling.product_related' =>
fn($query) => $query->where('status_id', '=', '2')])
->where('status_id',2)
->get();
Try this
$products = Product::with(['cross_selling.product_related' =>
fn($query) => $query->where('status_id', '=', '2')])
->has('cross_selling.product_related')
->where('status_id',2)
->get();
If you don't want cross selling products with no related products you can add
->whereHas('product_related')
Which basically says: Get me only cross selling which have 'product_related'.
In order to do it properly, you can:
$callback = fn($query) => $query->where('status_id', 2);
$products = Product::whereHas('cross_selling.product_related', $callback)
->with(['cross_selling.product_related' => $callback])
->where('status_id',2)->get();
Now the callback is applied to the whereHas() and the with() plus you can pass additional constraints using $callback.
I have a get formule that returns some nested relationships in an array. I was wondering how to access them in a where statement.
The initial get
$taken = UserWork::with('work.place')
->with('user')
->with('work.timeslot')
->get();
I tried something like this
$taak = $taken->where('work.timeslot[0].start_hour',"17:00:00")->first();
json result from $taken
Using with will endup with two queries. if you want to bring the user with timeslot null then there no need to add whereHas
$callback = function($query) {
$query->where('start_hour',"17:00:00");
};
$taken = UserWork::whereHas('work.timeslot', $callback)
->with(
['work.place', 'user', 'work.timeslot' => $callback]
)->get();
Hey guys how you doing?
I'm trying to simply find by id and at the same time guarantee that a column from a relationship table is with a value.
I tried a few things but nothing works.
$tag = Tag::find($id)->whereHas('posts', function($q){
$q->where('status','=', 1);
})->get();
Also:
$tag = Tag::whereHas('posts', function($q) {
$q->where('status','=', 1);
})->where('id','=', $id)->get();
Can you help me?
It is a simple thing but I can't manage to do it...
You need to read on Eloquent docs. Learn what's find, first, get for that matter.
Your code does what you need, and more (a bit wrong though) ;)
$tag = Tag::find($id) // here you fetched the Tag with $id
->whereHas('posts', function($q){ // now you start building another query
$q->where('status','=', 1);
})->get(); // here you fetch collection of Tag models that have related posts.status=1
So, this is what you want:
$tag = Tag::whereHas('posts', function($q){
$q->where('status','=', 1);
})->find($id);
It will return Tag model or null if there is no row matching that where clause OR given $id.
Have you checked Query Scope ?
You can do this:
$tag = Tag::where('status', '=', 1)
->where('id', '=', 1, $id)
->get();
This is related to one of my question earlier where:
Update table1 field with table2 field value in join laravel fluent
But since this is a different approach now, I will just ask another question:
How do you properly do an update using DB:raw?
I want to update the favorite_contents.type with the value of contents.type, but it doesn't do anything, the static setting of 1 to favorite_contents.expired is working.
This is my code which still doesn't update the type even when the DB::raw was used:
$table = 'favorite_contents';
$contents = DB::table($table)
->join('contents', function($join) use($table){
$join->on("$table.content_id", '=', 'contents.id');
})
->whereIn("$table.content_id",$ids)
->update(array(
"$table.expired" => 1
));
DB::raw("UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id");
This is the first code that doesn't update before I resorted to the above code that doesn't work as well:
$table = 'favorite_contents';
$contents = DB::table($table)
->join('contents', function($join) use($table){
$join->on("$table.content_id", '=', 'contents.id');
})
->whereIn("$table.content_id",$ids)
->update(array(
"$table.expired" => 1,
"$table.type" => "contents.type"
));
P.S:
This is working when done on an sql editor:
UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id
code raw updates like this:
...->update( array(
'column' => DB::raw( 'column * 2' )
) );
DB::statement("UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id");
Try DB::statement for raw queries that does not involve outputting something (select).
Will be work such similar, simple realization in Laravel 5.2 ,
Query Builder:
DB::table('stores')
->where('id', $request)
->update(['visibility' =>DB::raw($value)]);
This response is tested real site and working properly