i have a favorite list in my website for the users and they can add their favorite house to the wishlist
it goes well but he can not see the wishlist page and an error comes like this:
Trying to get property 'image' of non-object
it's my relations
class Home extends Model
{
protected $guarded = [];
public function favorite()
{
return $this->hasMany(favorite::class,'house_id');
}
}
class favorite extends Model
{
protected $guarded = [];
public function house()
{
return $this->belongsTo(home::class);
}
}
my index function in controller:
public function index()
{
$favorite = favorite::where('user_id',auth()->user()->id)->get();
return view('favorite.index',compact('favorite'));
}
my index:
#foreach($favorite as $fav)
<tr>
<td>
<a href="property-detail.html"><img src="{{$fav->home->image}}" alt=""
width="100"></a>
</td>
<td>{{$fav->home->title}}</td>
<td>خانه خانواده</td>
<td>اجاره</td>
<td>
<div class="price"><span>{{number_format($fav->home->price)}}</span><strong>تومان</strong>
</div>
</td>
<td>
<i class="fa fa-ban"></i> <span>حذف</span>
</td>
</tr>
#endforeach
First thing that your favorite model doesn't have home relation your relation is house and when you want to get its value you can use optional helper function like this:
optional($fav->house)->title
You are trying to access a relationship with a wrong name. You defined the relationship with name house:
public function house(){
return $this->belongsTo(home::class);
}
So you need to use this name to access:
#foreach($favorite as $fav)
<tr>
<td>
<img src="{{$fav->house->image}}" alt="" width="100">
</td>
<td>{{$fav->house->title}}</td>
<td>خانه خانواده</td>
<td>اجاره</td>
<td>
<div class="price"><span>{{number_format($fav->house->price)}}</span><strong>تومان</strong>
</div>
</td>
<td>
<i class="fa fa-ban"></i> <span>حذف</span>
</td>
</tr>
#endforeach
But you will have a problem too if the house relation come null. To avoid you can use the solution proposed by #Mohammed Aktaa:
optional($fav->house)->title
Related
the image from the database becomes null and does not appear on the website. in other case, the same image from the same database are appear. there are no mistyped variables, but can't load on the web. I've been trying to put the namespace where the image is, but still not working.
I'm still very new to codeigniter, i've tried my best to explain with my poor english.
please response if you have the answer, thankyou
InvenModel getinfo() function
<?php
namespace App\Models;
use CodeIgniter\Model;
class InvenModel extends Model
{
public function getinfo($slug = false)
{
if ($slug == false) {
return $this->findAll();
}
return $this->where(['slug' => $slug])->first();
}
}
?>
<?php
namespace App\Controllers;
use App\Models\InvenModel;
class inventori extends BaseController
{ public function detail($slug)
{
$data = [
"title" => 'Detail Produk',
"invent" => $this->InvenModel->getinfo($slug)
];
return view('inventori/detail', $data);
}
}
?>
<tr>
<td> <img src="<?= $invent['img']; ?>" class="img"></td> <!-- here the error message appears -->
<td> <?= $invent['nama']; ?> </td>
<td> <?= $invent['stok']; ?> </td>
<td> <?= $invent['terjual']; ?> </td>
<td> Edit </td>
<td> hapus </td>
</tr>
I have 3 models connected together Vendors has many Bills and bills has many bill_products
what do I need is to display the data with this relation in the index view here is my controller index function
public function index()
{
$vendor = $this->vendor();
$data['vendor_id'] = $vendor->id;
$data['bills'] = $vendor->bills()->get();
return view('vendors.bills.index', $data);
}
this bills relation in vendor model
public function bills()
{
return $this->hasMany(VendorBill::class);
}
Vendor bills model where I put my relation to bill products
class VendorBill extends Model
{
const FILLABLE = [
'making_cost',
'gram_price',
'customer_name',
'customer_id',
'vendor_id',
'id_no',
'phone',
'bill_no',
'total',
];
protected $fillable = self::FILLABLE;
public function vendor()
{
return $this->belongsTo(Vendor::class);
}
public function billProducts()
{
return $this->hasMany(VendorBillProduct::class);
}
}
and this is the last Model that belongs to Vendor Bills where i need to get data from
class VendorBillProduct extends Model
{
use SoftDeletes;
protected $table = "bill_products";
const FILLABLE = [
'vendor_bill_id',
'cate_code',
'product_description',
'metal_weight',
'total_metal_weight',
'caliber',
'gem_stone_weight',
'diamond_weight',
'purity',
'color',
'dimond',
];
protected $fillable = self::FILLABLE;
public function vendorBill()
{
return $this->belongsTo(VendorBill::class);
}
}
this is the index blade view where I want to get data stored in BillProducts the belongs to the VendorBill
<div class="table-responsive">
<table id="myTable" class="table table-striped ">
<thead>
<tr>
<th scope="col">#lang('vendors.bill_no')</th>
<th scope="col">#lang('vendors.customer')</th>
<th scope="col">#lang('vendors.phone')</th>
<th scope="col">#lang('vendors.cate_code')</th>
<th class="text-right" scope="col">#lang('vendors.action')</th>
</tr>
</thead>
<tbody>
#if($bills->count())
#foreach(#$bills as $bill)
<tr>
<th scope="row">{{#$bill->bill_no}}</th>
<th scope="row">{{#$bill->customer_name}}</th>
<td>{{#$bill->phone}}</td>
<td>{{#$bill->cate_code}}</td>
<td class="text-right">
<div class="dropdown">
<a href="#" class="btn btn-outline-light tn-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fal fa-ellipsis-h" aria-hidden="true"></i>
</a>
<div class="dropdown-menu dropdown-menu-right">
<a href="{{route('vendors.bills.edit',$bill->id)}}" class="dropdown-item" type="button">
#lang('vendors.edit')
</a>
<a class="dropdown-item" href="{{route('vendors.bills.show',$bill->id)}}">#lang('vendors.details')</a>
{{--<button class="dropdown-item" type="button">
#lang('vendors.delete')
</button>--}}
</div>
</div>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
enter image description here
in this image, I highlighted the column that I need to get in relation
I think there is a clever solution within your problem. Create a method on your VendorBill.php model, that concats the codes.
class VendorBill
{
function concatCodes(): string
{
return $this->billProducts->pluck('cate_code')->implode(', ');
}
}
Now in the context of a bill, you can convert all its vendor bills into that string and then implode that.
$bill->map->concatCodes()->implode(', ');
This will then concat the codes into the a string similar to this
code1, code2, code3
Background information:
I'm building an asset tracking system. I have a Product that has many Stock. Now, a Stock can be a container or a component inside of a container. Please see example below.
Example
In this picture, you can see two Products
L-Acoustics LA8 (Has 40 in Stock) [Each Stock is a component inside of the container]
L-Acoustics LA-RAK II (Has 4 in Stock) [Each Stock is a container]
My goal:
I want to be able to show all of the containers I have with the last time it was updated. Like the picture below
The problem:
I can't show the last time the container was updated. Because of the way I'm accessing the relations, don't allow me to get inside the pivots and get the last updated.
What I've tried:
I created a Many-to-Many relationship with the Stock model itself and called the pivot table "component_container".
Here is the database relation pertinent to my problem:
Here is my Stock model:
// Stock.php
class Stock extends Model {
protected $guarded = [];
protected $casts = [
'product_id' => 'integer',
'quantity' => 'integer'
];
public function getContainerAttribute()
{
return $this->container()->first();
}
public function getComponentsAttribute()
{
return $this->components()->get()->flatten()->all();
}
public function product()
{
return $this->belongsTo(Product::class);
}
public function container()
{
return $this->belongsToMany(Stock::class, 'component_container', 'component_id', 'container_id')->withTimestamps();
}
public function components()
{
return $this->belongsToMany(Stock::class, 'component_container', 'container_id', 'component_id')->withTimestamps();
}
}
Here is my controller for showing my containers:
// ContainerController.php
class ContainerController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$containers = Stock::has('components')->paginate(20);
return view('containers.index', compact('containers'));
}
}
Here is the containers/index.blade.php
// index.blade.php
<div class="table-responsive">
<table class="table table-borderless table-striped table-vcenter">
<thead>
<tr>
<th>Barcode</th>
<th>Name</th>
<th class="d-none d-sm-table-cell">Updated</th>
</tr>
</thead>
<tbody>
#forelse($containers as $container)
<tr>
<td>
<a href="{{route('products.stocks.show', [$container->product->slug, $container->barcode])}}">
<strong>{{ $container->barcode }}</strong>
</a>
</td>
<td>
{{ $container->product->name }}
</td>
<td class="d-none d-sm-table-cell">
</td>
</tr>
#empty
<tr>
<td colspan="100" class="text-center">Nothing Available</td>
</tr>
#endforelse
</tbody>
</table>
</div>
My View:
<table border="1" style="text-align: left;">
<td>
<b>Firstname</b>
</td>
<td>
<b>Actions</b>
</td>
#foreach($Newslist as $News)
<tr>
<td>
{{ $News->lastname }}
</td>
<td>
<button type="button" class="btn btn-primary">Edit</button>
</td>
</tr>
#endforeach
</table>
My routes/web.php:
Route::Post('NewsEdit/{{$News->id}}/EditForm','NewsControllere#EditForm');
My Controller.php:
<?php
namespace App\Http\Controllers;
use App\Newsmodel;
use Illuminate\Http\Request;
class NewsControllere extends Controller
{
public function EditForm($NewsId)
{ dd(request()->all());
echo "deepakkeynes";exit();
//$Newsmodel = Newsmodel::find($NewsId);
//return view('/News')->with('News',$Newsmodel);
}
}
While clicking the edit button in the view, the following result is obtained:
Result: 404 page
Expected Result: The Id of the edit button along with the echo value!
Can anyone help me out? I am a newbie in laravel..!
Have a look at this LaraCast tutorial on Route Model Binding. This explains the underlying documentation well.
Essentially:
routes/web.php:
Route::get('NewsEdit/{news}/EditForm','NewsControllere#EditForm');
newsController.php:
public function EditForm(Newsmodel $news)
{
return view('/News')->with('News',$news);
}
I have a problem, I made a support system, and when I want to enter the page where I watch the ticket, I will get 404 not found. Basically the route is that of the id in the database.
Routes:
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
Controller:
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate() {
$tickets = Ticket::latest()->get();
return view('updateTicket', compact('tickets'));
}
View:
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<form>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</form>
</tr>
#endforeach
</tbody>
I really don't understand the problem, a way to solve and did not receive error 404? Btw, I read the other topics and I couldn't find a solution.
The link you are setting in the href attribute is only the ticket id, but it should be, for example, "/viewTickets/1"
Try to put this:
href="{{route('updateTicket', ['ticket' => $ticket->id])}}"
In your route :
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
In your Controller :
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate(Request $request, $ticket) {
$tickets = Ticket::find($ticket);
return view('updateTicket', compact('tickets'));
}
In your Blade :
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</tr>
#endforeach
</tbody>