i have a function that displays tasks of current user
public function test(Request $request)
{
$user= auth()->user();
return view('welcome')->with('tasks',$user->tasks);
}
This is the code that displays the posts in home view
#foreach($tasks as $task)
<tr>
<td class="table-text">
<div>{{$task->item}}</div>
</td>
</tr>
#endforeach
Am getting the error above
relationship in user model
public function tasks(){
return $this->HasMany('App\Task');
}
I think you need to fix your code lower case h in the hasMany() function
It should be
public function tasks(){
return $this->hasMany('App\Task');
}
Related
I am new to laravel 8 and blade syntax.
I am working on a project where users (agents) can upload apartments.
i have agent table with corresponding model relationship
agent model
public function property()
{
return $this->hasMany(Property::class, property_id, id);
}
property model
public function agents()
{
return $this->belongsTo(Agent::class, agent_id, id);
}
I used breeze to build up my registration and login
i want when an agent is logged in, i will display in his dash board list of his properties.
#foreach(Auth::user()->properties as $property)
#if(($loop->count) > 0)
<td class="py-3 px-2">
<div class="inline-flex space-x-3 items-center">
<span>{{$property->propertyId}}</span>
</div>
</td>
<td class="py-3 px-2">{{$property->building}}</td>
<td class="py-3 px-2">{{$property->rent->name}}</td>
#else
You have not uploaded any apartment
#endif
#endforeach
but i get error "Undefined variable $properties".
Meanwhile, {{ Auth::user()->othernames }} works fine.
AgentController
public function index()
{
return view ('dashboard', [
'agent' => Agent::all(),
'appointment'=>Appointment::all(),
'properties'=>Property::all(),
'schedule'=>AgentSchedule::all()
]);
}
propertyController
public function create()
{
return view('pages.uploadapartment', [
'states'=> State::all(),
'areas'=>Area::all(),
'buildings'=>building::all(),
'buildingtypes'=>BuildingType::all(),
'rentpaymentmethods'=>rentpaymentmethod::all(),
'flattypes'=>flattype::all(),
]);
}
Since you are not using user model to define the relationship!
Your relationship should be like this:
//In your agent model
public function properties()
{
return $this->hasMany(Property::class, 'property_id', 'id');
}
//In property model
public function agent()
{
return $this->belongsTo(Agent::class, 'agent_id', 'id');
}
And when you want properties from a logged-in user, you grab them like this: $properties = Auth::user()->properties;
I got stuck with one to one relation
Facade\Ignition\Exceptions\ViewException
Class 'App\App\User' not found (View: /var/www/html/project1/resources/views/pengajuan/datauser/index.blade.php)
http://app.web/pengajuan/datauser
my User Model
public function dataUser()
{
return $this->hasOne(App\DataUser::class);
}
my DataUser Model
public function User()
{
return $this->belongsTo(App\User::class);
}
my controller
public function index()
{
$datauser = DataUser::all();
return view('pengajuan.datauser.index', compact('datauser'));
}
and my index view
#foreach ($datauser as $d)
<td>{{$d->masa_penilaian}}</td>
<td>{{$d->user->nama}}</td>
<td>{{$d->nip}}</td>
<td>{{$d->nuptk}}</td>
#endforeach
Can Help Me?
Thanks
Try DataUser::class instead of App\DataUser::class and User::class instead of App\User::class?
I have the following tables :
orders : id, etc...
order_lines : id, order_id, product_id, etc...
products : id, name, etc...
Foreign keys are defined.
My Laravel Models are defined as :
class Order
public function orderLine()
{
return $this->hasMany('App\OrderLine');
}
class OrderLine
public function order()
{
return $this->belongsTo('App\Order');
}
public function product()
{
return $this->belongsTo('App\Product');
}
class Product
public function orderLine()
{
return $this->hasMany('App\OrderLine');
}
I've tried many things, but nothing is working. Here is the best solution for me, but it's not working.
class OrderController
public function show($id)
{
$user = Auth::user();
$order = Order::where('user_id', '=', $user->id)->with(['orderLine.product'])->findOrFail($id);
return view('layouts/order/index', compact('order'));
}
I struggle to display the following data in the view :
#foreach($order->orderLine as $key => $orderLine)
<tr>
<td>{{$orderLine->product->name}}</td>
<tr>
#endforeach
Product object is not loaded. I want to display the product name in the above loop.
Try to do like this:
public function show($id)
{
$user = Auth::user();
$order = Order::with(['orderLines', 'orderLines.product'])
->where('user_id', '=', $user->id)
->findOrFail($id);
return view('layouts/order/index', compact('order'));
}
class OrderLine
public function order()
{
return $this->belongsTo(\App\Order::class, 'order_id');
}
public function product()
{
return $this->belongsTo(\App\Product::class, 'product_id');
}
class Order
public function orderLines()
{
return $this->hasMany(\App\OrderLine::class);
}
Change name of orderLine to orderLines because order has many orderLines.
And in your blade:
#foreach($order->orderLines as $orderLine)
<tr>
<td>{{$orderLine['product']->title}}</td>
<tr>
#endforeach
Hello Dezaley and welcome to StackOverflow!
Let's investigate your problem.
As far as I can see you are selecting the model in the wrong way. Let me help you:
$order = Order::where(['id' => $id, 'user_id' => $user->id])->with('orderLine.product')->firstOrFail();
The answer from mare96, who was very friendly to help me, is working. However, I found out someting.
You can implement as (mare96 solution)
public function show($id)
{
$user = Auth::user();
$order = Order::with(['orderLines', 'orderLines.product'])
->where('user_id', '=', $user->id)
->findOrFail($id);
return view('layouts/order/index', compact('order'));
}
#foreach($order->orderLines as $orderLine)
<tr>
<td>{{$orderLine['product']->title}}</td>
<tr>
#endforeach
In the view, I don't like the array syntax "$orderLine['product']->title". The following solution without the array is also working.
Solution is below :
public function show($id)
{
$user = Auth::user();
$order = Order::with('orderLines', 'orderLines.product')
->where('user_id', '=', $user->id)
->findOrFail($id);
return view('layouts/order/index', compact('order'));
}
#foreach($order->orderLines as $orderLine)
<tr>
<td>{{$orderLine->product->title}}</td>
<tr>
#endforeach
In fact, my issue was that product was defined to null in the model, so Product was always null in the view.
Class OrderLine extends Model {
public $product = null
I remove line "public $product = null" and it's working as expected. Thanks to the people who helped me.
give it a try -
#foreach($order as $ordr)
<tr>
<td>{{$ordr->product_id->name}}</td>
<tr>
#endforeach
I'm trying to get a relationship between models in a laravel template.
I want to get the product fields respective to an ad.
This is my AdController:
public function ads(Request $request)
{
$this->adRepository->pushCriteria(new RequestCriteria($request));
$hash = Auth::user()->environment_hash;
$ads = $this->adRepository->findByHash($hash);
return view('ads.index')
->with('ads', $ads);
}
This how I'm parsing to my blade template:
#foreach($ads as $ad)
<tr>
<td>{!! $ad->product->name !!}</td>
</tr>
#endforeach
But I keep getting this error:
Trying to get property of non-object
This is the adRepository file:
public function findByHash($hash = null)
{
$model = $this->model;
if($hash)
$this->model->where('environment_hash', $hash);
else
$this->model->where('environment_hash', Auth::user()->environment_hash);
return $model;
}
This my Ad Model: Ad.php
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Ad extends Model
{
public function product()
{
return $this->belongsTo(Product::class);
}
}
This is my ad table:
id
product_id
environment_hash
And my product table:
id
environment_hash
name
What is wrong with the code?
Not sure but there must be some ad which has no product in your database. So this may be the reason you are getting error. Make sure that all of your ad has related product in database.
You can test it by running following code:
#foreach($ads as $ad) <tr> <td>Test {!! $ad->product !!}</td> </tr> #endforeach
If you get result like this:
Test some_name
Test
Test some_another_name
Then this is confirm that some ad has no related product.
If this is the case then you need to if() check before accessing the attributes of product.
Update
The problem is that you are missing get() in your findByHash().
It should be as:
public function findByHash($hash = null)
{
$model = $this->model;
if($hash)
$this->model->where('environment_hash', $hash);
else
$this->model->where('environment_hash', Auth::user()->environment_hash);
return $model->get();
}
I have nested looping view on laravel and i am having problem when i try to show the list of my items.
Here is the code where i have the error
#foreach($project->codes->items as $item)
<tr>
<td>{!! $item->item !!}</td>
</tr>
#endforeach
and it produces this error.
Cannot access protected property Illuminate\Database\Eloquent\Collection::$items
I have setup the models for project, code and items as.
Project Model
class Project extends Model
{
public $table = 'projects';
public function codes() {
return $this->hasMany('App\Models\Code');
}
}
Code Model
public function code() {
return $this->belongsTo('App\Models\Project');
}
public function items() {
return $this->hasMany('App\Models\Item');
}
item model
public function item() {
return $this->belongsTo('App\Models\Code');
}
And here's how i implement it on the controller
public function index(Request $request)
{
$this->projectRepository->pushCriteria(new RequestCriteria($request));
$projects = $this->projectRepository->with('codes.items')->all();
return view('projects.index')
->with('projects', $projects);
}
I have run the code on artisan tinker and it shows the correct data. But not loading on the output php.
Thank You for your support.
Please share the output
i think it should be like this
#foreach($project->codes as $code)
#foreach($code->items as $it)
<tr>
<td>{!! $it->item !!}</td>
</tr>
#endforeach
#endforeach