I have two table
Email (id,content,folder_id)
Folder (id,name)
In model
public function folder() {
return $this->belongsTo('App\Folder');
}
public function email() {
return $this->hasMany('App\Email');
}
In Controller, I want list all email for each folder from a request
public function mailbox ($foldername) {
$data=Email::with('folder')->orderBy('id','DESC')->where('??????',$foldername)->get()->toArray();
return view('dashboard.mailbox.mailbox',compact('data'));
}
The problem is: how can i get name (with ???? in code below) from Folder table to where with input request ->
It's OK if i go with $id request, but I want a beautiful response, so please help me
Related
I'm learning Laravel and need to get a list of nested relationships. My foreign keys seem to be set up correctly.
products->servers->configs
Product Controller
$products = Product::with('servers')->get();
Product Model
public function servers()
{
return $this->hasManyThrough(Config::class, Server::class);
}
I'm only getting a list of servers that are the configs. Eg
products:{
id:1,
servers:[
ram:16gb //this is the config not the server
]
}
How can I get the list of configs inside the servers inside the products? Eg
products:{
id:1,
server:{
id:1,
name:'big server',
config:{
ram:16gb
}
}
}
In Product Modal Use hasMany Method
public function servers()
{
return $this->hasMany(Server::class);
}
In Server Modal Use hasMany(for many rows get) or hasOne(for single row get) method
public function configs()
{
return $this->hasMany(Config::class);
}
public function config()
{
return $this->hasOne(Config::class);
}
Now In ProuctController see how to get nested relationship data
$products = Product::with('servers.configs')->get();
I have the following table structure:
default_tasks
id
...
locations
id
...
users
id
...
default_task_location
id
location_id
default_task_id
default_assignee_id (FK to users.id)
In my blade file I'm looping through the default tasks for the location, and trying to output the default assigned user. I can get the ID by doing $task->pivot->default_assignee_id, but I want to get the actual user model so I can output the user's name in my blade file
I've gathered from searching that the way to accomplish this is to create a pivot table model. So I've done this:
class DefaultTaskLocationPivot extends Pivot
{
public function user()
{
return $this->belongsTo(User::class, 'default_assignee_id');
}
public function defaultTask()
{
return $this->belongsTo(DefaultTask::class);
}
public function location()
{
return $this->belongsTo(Location::class);
}
}
and in my location model I've done this:
public function taskDefaultAssignee()
{
return $this->belongsToMany(User::class)
->using(DefaultTaskLocationPivot::class)->withPivot(['default_task_id', 'frequency_type_override', 'create_on_day_override', 'due_on_day_override', 'default_assignee_id']);
}
However, I'm still unable to access the user associated with the default_assignee_id. I've tried things like $task->location->taskDefaultAssignee, but it doesn't seem like the relationship is even available if I dump the object.
I have this tables:
clients
id - integer
name - string
wineshops
id - integer
client_id - integer
name - string
wines
id - integer
wineshop_id - integer
name - string
I need to print on the view that contains all the wines the name of the client and the name of the wineshop referring to that wine.
As I initially needed the wine count to view it in the client index, I used the hasManyThrough relationship as the code below shows.
To be clearer in the view where the wines of a specific wine shop are present I want to see the following line:
List of wines belonging to the wine shop (wine shop name) of (client name)
Obviously as a title (h1), so not repeated through a foreach loop
Relationship defined in the Client model:
public function wineshops()
{
return $this->hasMany(Wineshop::class);
}
public function wines()
{
return $this->hasManyThrough(Wine::class, Wineshop::class);
}
Relationship defined in the Wineshop model:
public function client()
{
return $this->belongsTo(Client::class);
}
public function wines()
{
return $this->hasMany(Wine::class);
}
Relationship defined in the Wine model:
public function wineshop()
{
return $this->belongsTo(Wineshop::class);
}
ROUTE:
Route::get('/wine/index/{id}', [WineController::class, 'index'])->name('wine.index');
CONTROLLER:
public function index($id)
{
$wines = Wine::where('wineshop_id', $id)->get();
$wines->load('wineshop');
return view('wine.index', compact('wines'));
}
I've tried this but can't work, I get the error Undefined constant "wines" when I try to pass the name of the wineshop into the view.
Anyone who can kindly help me?
write the return view('wine.index', compact('wines')); inside the index function...
Trying to build an eloquent query to return all results in the database where a relationship exists.
$deliveries = Deliverer::has('deliveries')->get()->toArray();
dd($deliverer);
This is returning results which include multiple deliverers without any deliveries.
Deliverer Model includes:
public function deliveries() {
return $this->hasMany(Deliveries::class)->latest();
}
Delivery Model includes:
public function deliverer() {
return $this->belongsTo(Deliverer::class);
}
Can anybody see what is going wrong here please?
i have 4 table
inst
id
subject
report
id
subject
object
id
type
container
id
obj_id
i want with obj_id of container table, achieve the The corresponding object record.
then if type of object = 1, fetch data from inst table or type = 2, fetch data from report table
based on your information,
you can do like #Mahfuz Shishir answer
or
create new appends attribute inside Object Model
protected $appends = ['data'];
public function getDataAttribute()
{
if($this->attributes['type'] == 1) {
return Inst::where('inst_column', $this->attributes['object_column_to_match']); //this just sample
} else {
return Report::where('inst_column', $this->attributes['object_column_to_match']); //this just sample
}
}
The relation looks like.
In your Object.php Model
public function containers()
{
return $this->hasMany('App\Container', 'foreign_id');
}
And in your Container.php Model
public function object()
{
return $this->belongsTo('App\Object', 'foreign_id');
}
You provide little information. Implement rest of the controller code in your own sense. Or put some code that you have tried.