I am trying to add leftJoin to the hasOne method, but it seems that it is not working. Data from the main table is there, but not from the joined table. Code:
public function getUser()
{
return $this->hasOne(UserKeys::classname(), ['id' => 'userKey_id'])
->leftJoin('users', 'users.id = userKeys.user_id');
}
You should use viaTable instead of leftJoin
public function getUser(){
return $this->hasOne(UserKeys::classname(), ['id' => 'userKey_id'])
->viaTable('users', ['id'=>'user_id']);
}
Related
I have multiple campaigns, that can be assigned to multiple users. Basically, they have belongsToMany relation both ways.
I would like to print out the results on the page, that only belongs to that specific user, based on the pivot table.
Models:
User model:
public function campaign()
{
return $this->belongsToMany(Campaign::class, 'campaign_user');
}
Campaign model:
public function users()
{
return $this->belongsToMany(Gift::class, 'campaign_user');
}
Migration of pivot table:
public function up()
{
Schema::create('campaign_user', function (Blueprint $table) {
$table->foreignId('user_id')->constrained()->onDelete('cascade');;
$table->foreignId('campaign_id')->constrained()->onDelete('cascade');
});
}
Incorrect Controller:
public function index(Request $request)
{
$data = Campaign::withCount('gifts');
$data->where('user_id', $request->user()->id)->get();
return view('subscribes.index', ['data' => $data]);
}
Basically, all I need, is to return specific campaigns, that the client has subscribed only, based on the pivot table/user id. Thus, editing this Controller.
I still face lots of issues with Eloquent models and pivot tables, and I would be very thankful, for any assistance in regards to it.
This will make more sense if you rename the relationship campaign() to it's plural campaigns() on your User model.
Once you have the relationships set up, you can access campaigns for the user straight from the user object.
$user = $request->user();
$data = $user->campaigns;
Note also, if your user is authenticated, you can access them easily like:
$user = auth()->user();
So your index method in your controller would be
public function index(Request $request)
{
$user = $request->user();
$data = $user->campaigns;
return view('subscribes.index', ['data' => $data]);
}
This is my Controller
$categoryIds = Category::select('id')->with('childrenRecursive')->where('id', 1)->get();
Ad::whereIn('category_id', $categoryIds)->get();
This is my model
public function parent() {
return $this->belongsTo(Category::class, 'parent_id');
}
public function childs() {
return $this->hasMany(Category::class, 'parent_id');
}
public function Ads() {
return $this->hasManyThrough(Ad::class, Category::class, 'parent_id', 'category_id', 'id');
}
How get all childern categories ides
I solved this problem with this solution
My Controller
public function index()
{
$parent = Category::with('descendants')->find(1);
$descendants = $this->traverseTree($parent, collect([1]));
$ads = Ad::whereIn('category_id',$descendants)->get();
return response($ads);
}
protected function traverseTree($subtree, $des)
{
$descendants = $des;
if ($subtree->descendants->count() > 0) {
foreach ($subtree->descendants as $descendant) {
$descendants->push($descendant);
$this->traverseTree($descendant, $descendants);
}
}
return $descendants;
}
I'd do it with Laravel's Subqueries approach.
$parentId = 4;
Ad::whereIn('category_id', function($q) use ($parentId) {
$q->select('id')
->from('categories')
->where('parent_id', $parentId);
});
If you want to add the parent model, you can chain with():
Ads::whereIn('category_id', function($q) use ($parentId) {
$q->select('id')
->from('categories')
->where('parent_id', $parentId);
})
->with('category.parent')
->get();
Your code chunks are not clear so you may need to tweak my code example.
If I understand your question properly you need to get ads corresponding to id's of all related records also, for a given category record.
$category = Category::with('childs:id,parent_id')
->where('id', 1)
->firstOrFail();
$categoryIds = collect([$category->parent_id, $category->id]);
$category->childs->map(fn($child) => $categoryIds->push($child->id));
$ads = Ads::whereIn('category_id', $categoryIds->filter()->all())
// Can eager load the product(s) if needed
//->with('products')
->get();
I'm new to Laravel and I can write simple eloquent queries but have no idea how to convert this query to eloquent. Can anyone give any idea, is it possible to convert this to eloquent or I have to write raw query?
"Select categories.id, categories.name, Sum(likes.liked) as liked
FROM categories, likes
WHERE likes.material_id IN (SELECT category_material.material_id
FROM category_material
WHERE category_material.category_id = categories.id)
GROUP BY categories.id";
Here my Models
class Material extends Model
{
public function categories(){
return $this->belongsToMany(Category::class,'category_material');
}
public function likes(){
return $this->hasMany(Like::class);
}
////////////////////////////////////////////////////////////
class Like extends Model
{
protected $table = 'likes';
public function user(){
return $this->belongsTo(User::class);
}
public function material(){
return $this->belongsTo(Material::class);
}
//////////////////////////////////////////////////////
class Category extends Model
{
public function materials(){
return $this->belongsToMany(Material::class,'category_material');
}
You can define a likes relationship in your Category model like so:
public function likes()
{
return $this->belongsToMany(Like::class, 'category_material', 'category_id', 'material_id', 'id', 'material_id');
}
Then to achieve what you're after with Eloquent you can use a mixture of has() and withCount, however, we're going to modify the withCount call to return a sum() instead:
$catrgories = Category::has('likes')->withCount([
'likes as liked' => function ($query) {
$query->select(DB::raw('SUM(likes.liked)'));
},
])->get();
If you're wanting to return categories that don't have any likes you can remove the has() method, and introduce the COALESCE() function to your raw query:
$catrgories = Category::withCount([
'likes as liked' => function ($query) {
$query->select(DB::raw('COALESCE(SUM(likes.liked), 0)'));
},
])->get();
Alternatively, you could simply load the necessary relationships and then use that fact that Eloquent returns collection to get the value after you've retrieved the results from the database:
$categories = Category::with('materials.likes')->get()->map(function ($item) {
$item->setAttribute('liked', $item->materials->map(function ($item) {
return $item->likes->map->sum('liked')->sum();
})->first());
$item->unsetRelation('materials');
return $item;
});
This would mean that you don't have to add the custom relationship.
How do I update all rows in a table (all IDs) at once, with a database query in Laravel? My current controller code is as follows:
public function updateSchedule(Request $request, $id)
{
$timein = $request->input('timeIn');
$timeout = $request->input('timeOut');
DB::table('schedules')
->where('id', 1)
->update(['time_in' => $timein, 'time_out' => $timeout]);
}
To update the whole table (the WHOLE TABLE), you would do the following (removing the condition that you only want to update matches where id = 1):
public function updateSchedule(Request $request, $id)
{
$timein = $request->input('timeIn');
$timeout = $request->input('timeOut');
DB::table('schedules')
->update(['time_in' => $timein, 'time_out' => $timeout]);
}
If you want to see something afterwards, you either need to return that value, or a view, or a redirect to the prior page.
For example, the following (although it's not good practice to use back() as you don't know where they came from. It's better to go to a specific route / url).
public function updateSchedule(Request $request, $id)
{
$timein = $request->input('timeIn');
$timeout = $request->input('timeOut');
DB::table('schedules')
->update(['time_in' => $timein, 'time_out' => $timeout]);
return back();
}
! have two model classes
1-Usermaster
2-Userinstances
usermaster having multiple userinstances.
I want all the related record from both tables,i have done like that
Usermaster.php(model class)
public function relations()
{
return array(
'userinstances' => array(self::BELONGS_TO, 'Userinstances', 'Id')
);
}
userinstances.php(model class) -
public function relations()
{
return array(
'usermaster' => array(self::HAS_MANY, 'Usermaster', 'userMasterID')
);
}
In MyController.php
$resultSettmp = Usermaster ::model()->with('userinstances')->findAll();
but it is giving the record from only Usermaster and userinstances as blank array,Is anything wrong in my code?
I think your relations are not right. Try it like this:
In Ubermaster:
public function relations()
{
return array(
'userinstances' => array(self::BELONGS_TO, 'Userinstances', 'userMasterID')
);
}
And userinstances.php(model class) -
public function relations()
{
return array(
'usermaster' => array(self::HAS_MANY, 'Usermaster', 'userMasterID')
);
}
I changed the foreign key in the userinstances relation.