# TABLES:
# kids: name, birthday, active
# chores: name, start_age, number_of_kids_required
# chore_kid: chore_id, kid_id, chore_date
Kid.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Kid extends Model
{
public function chores(){
return $this->belongsToMany('App\Chore')
->withPivot('chore_date')
->withTimestamps();
}
}
Chore.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Kid;
class Chore extends Model
public function kids(){
return $this->belongsToMany('App\Kid')
->withPivot('chore_date')
->withTimestamps();
}
}
With belongs to many I can grab the chores with assigned kids and the pivot table having the requested chore date:
$chore->kids()->wherePivot('chore_date', $date);
How can I get all the chores with the same chore_date without limiting it to the kids that are assigned to a specific chore.
How do I do this? This is my guess at this but it doesn't work:
Chore::wherePivot('chore_date', $date);
I have tried other combinations, but can't seem to find the right syntax.
controller
$chore = Chore::whereHas('kids', function ($query) use ($date){
$query->where('chore_date','=', $date );
})->get();
with that you'll get chore with specific date. hope that help.
Related
Like you can do Project::with('events.contacts') can you also do Project::withCount('events.contacts') ? It doesn't seem to work. Is there another way to find the total count of contacts for all events for a certain project where project.id = event.project_id and event.id = contact.event_id
You can use relationship Has Many Through
Docs: https://laravel.com/docs/8.x/eloquent-relationships#has-many-through
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
public function contacts()
{
return $this->hasManyThrough(Contact::class, Event::class);
}
}
Then you can do
Project::withCount('contacts')->get();
Each industry has many projects
each project belongs to only one industry
industries table has columns as below:
id, industry_name, industry_code, created_at, updated_at
projects table has columns as below:
id, industry_id (foreign key), user_id, project_name, project_start_date, project_end_date, created_at, updated_at
I want to show project name and industry name in my index.blade.php (view)
I have created Project & Industry models. How do I show the result in my view?
Industry.php model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Industry extends Model
{
public function project()
{
return $this->hasMany('App\Models\Project');
}
}
Project.php model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
public function industry()
{
return $this->belongsTo('App\Models\Industry');
}
}
like this ...
$Industry = App\Industry ::find(1);
echo $Industry ->Project ->project_name;
https://laravel.com/docs/5.5/eloquent-relationships#one-to-many
do try renaming your method "project" to "projects" in Industry.php since it is a "hasMany" relationship. this would prevent you from making errors.
in your controller, say PagesController.php
public function index() {
// find an industry with id of 1
$Industry = App\Industry ::find(1);
return view('some_view',compact('industry'));
}
therefore in your view, since an industry has many projects you can access them and loop over them by $Industry->projects. However you should consider eager loading them in order to prevent the n + 1 problem. there edit your query to $Industry = App\Industry ::with('projects')->find(1);
read more on eager loading https://laravel.com/docs/5.5/eloquent-relationships#eager-loading
https://laravel.com/docs/5.5/eloquent-relationships#one-to-many
If specifically you want only project_name and industry_name then
try this -
$result = App\Industry::with(array('projects'=> function($query){
$query->addSelect(['project_name']);
}))
->where('industry.id',$id)
->get([industry_name]);
else for all records try this -
$result = App\Industry::with('projects')
->where('industry.id',$id)
->get();
Hope this will help you.
I have the database structure that have a classes table, a users table and users_classes table that matches the other two, because a user can belong to multiple classes. I have a problem now. I have code like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Classes extends Model
{
protected $table = 'classes';
public function students()
{
}
}
And I want to be able to access the students of the class by typing $class = Classes::find(1) and then $class->students to access the students. How do I define the relationship without using the query builder? I want to use eloquent. Im a noobie in Laravel pls dont downvote.
You use a belongsToMany relation.
If your users_classes table has the fields user_id and class_id you can do the following:
public function students()
{
return $this->belongsToMany(Student::class, 'users_classes', 'class_id', 'user_id');
}
When I save a post it saves id of user which posted it.
That is great but I don't understand how.
Post controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function postCreatePost(Request $request)
{
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post); //i cant understand this line
return redirect()->route('dashboard');
}
}
?>
User model
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('App\Post'); // ?
}
}
Post model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User'); // ?
}
}
I don't understand how belongs to and has many relations work.
If someone could explain it to me I would be very grateful.
In this case, the hasMany indicates that it will find many rows with the user id on the user_id column in the posts table.
The belongsTo simply means that it will find a user with an id in the users table that matches the user_id column on the posts related row.
Finally, $request->user()->posts()->save($post);:
Get the request
Get the user from the request
Get the posts from the user
The ->save($model) is a bit tricker. This requires that the object passed to the ->save($object) function is an instance of the relationship. In our case, the relationship is posts() and therefore the $post simply has to be an instance of App\Post (or wherever your Models namespace is).
You can find an explanation in the official documentation and in the chapter "Eloquent Relationships" from the book "Code Smart" (Laravel 4).
You can find a lot more resources in Google.
I have two models (and two tables in the database) in Laravel 5: Artwork and Option (table names are artworks and options). A single artwork can have many options, so my models are as follows:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Artwork extends Model {
public function options(){
return $this->hasMany('App\Option');
}
}
and
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Option extends Model {
//
}
I'm trying to output an array that will contain ALL the artworks that will have their options nested inside them. Outputting only artworks works fine ($artworks = $artwork->get() as shown below), but I cannot figure out how to merge / nest options within each artwork.
Here is my Route for this
get('api/artworks','ArtworksController#index');
And here is my controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Artwork;
class ArtworksController extends Controller {
public function index(Artwork $artwork)
{
$artworks = $artwork->merge($artwork->options())->get();
return $artworks;
}
}
I was playing around with that merge() function and I just couldn't get it right. Is it possible to achieve this with merge() at all?
You just need to eager load the options relationship:
$artworks = $artwork->with('options')->get();