Everything works fine without basket-add button, but when I added it i have error "Call to a member function trashed() on null". Here is my code:
card.blade.php
<p>{{ $sku->price }} {{ $currencySymbol }}</p>
<p>
<form action="{{ route('basket-add', $sku) }}" method="POST">
#if($sku->isAvailable() && Auth::check())
<button type="submit" class="btn btn-primary" role="button">
#lang('main.add_to_basket')
</button> -->
#else
#lang('main.not_available')
#endif
<a href="{{ route('sku', [
isset($category)
? $category->code
: !empty($sku->product->category)
? $sku->product->category->code
: '' ,
$sku->product->code, $sku->id
]) }}" class="btn btn-default" role="button">#lang('main.more')</a>
#csrf
</form>
Sku.php
public function isAvailable()
{
return !$this->product->trashed() && $this->count > 0;
}
What is a problem? all pages work fine (I am doing pagination) except 5 and 7.
Models:
class Sku extends Model
{
use SoftDeletes;
protected $fillable = ['product_id', 'count', 'price'];
protected $visible = ['id', 'count', 'price', 'product_name'];
public function product()
{
return $this->belongsTo(Product::class);
}
Product.php
class Product extends Model
{
use SoftDeletes, Translatable;
protected $fillable = [
'name', 'code', 'price', 'category_id', 'description', 'image', 'hit', 'new', 'recommend', 'count', 'name_en',
'description_en'
];
public function category()
{
return $this->belongsTo(Category::class);
}
when calling the QueryBuilder methods you need to access via method names. Acsessing via property will not work. Change product property to method product()
public function isAvailable()
{
return !$this->product()->trashed() && $this->count > 0;
}
Seems like in places you use $this->product or $sku->product, the related product might not exist, meaning either the product is deleted in your database or the Sku model's product_id is null. So wherever you're trying to access the product relationship of a Sku model, you should check for its existence.
Related
Try access to show method in VehicleController() and return error: Attempt to read property 'customer' on null.
*ERROR: Property [full_name] does not exist on this collection instance
VEHICLE MODEL
class Vehicle extends Model
{
use HasFactory;
protected $table = 'vehicles';
protected $fillable = ['customer_id', 'model_name', 'brand',
'color', 'year', 'plate_number','engine_number', 'mileage', 'license_number',
'vehicle_type', 'photo_cover'
];
public function customer ()
{
return $this->hasMany(Customer::class);
}
}
CUSTOMER MODEL
class Customer extends Model
{
use HasFactory;
protected $table = 'customers';
protected $fillable = ['full_name', 'customer_type', 'email',
'phone_main', 'phone_mobile', 'doc_cpf', 'doc_cnpj', 'city', 'state',
'address', 'neighborhoods', 'number', 'zip_code'
];
public function vehicle ()
{
return $this->hasOne(Vehicle::class);
}
}
VEHICLE CONTROLLER (show)
public function show(Vehicle $vehicle)
{
$vehicle = Vehicle::select(
'vehicles.id', 'vehicles.model_name', 'vehicles.brand', 'vehicles.year', 'vehicles.color', 'vehicles.mileage',
'customers.full_name', 'vehicles.plate_number', 'vehicles.vehicle_type', 'vehicles.photo_cover'
)
->join('customers', 'customers.id', '=', 'vehicles.customer_id')
->get();
return view('admin.cars.show')->with('v', $vehicle);
}
VIEW (show.blade.php)
<?php
<td>CUSTOMER:</td>
<td class="view-row">
{{ $v->customer->full_name }}
</td>
<td>MODEL:</td>
<td class="view-row">
{{ $v->model_name }}
</td>
<td>BRAND:</td>
<td class="view-row">
{{ $v->brand }}
</td>
I don't know how to solve this, I made this change, but it didn't work. See below:
<td>CUSTOMER:</td>
<td class="view-row">
{{ $v->customer->full_name ?? 'Customer Empty' }}
</td>
VEHICLE MODEL replace this
public function customer ()
{
return $this->hasMany(Customer::class);
}
to
public function customer ()
{
return $this->belongsTo(Customer::class);
}
and VEHICLE CONTROLLER (show) replace to
$vehicle = Vehicle::with('customer')
->paginate(10);
All pages worked fine. After deleting 4 products from admin panel I've got error:
Laravel Trying to get property 'image' of non-object
How can I check if this page have no items or other suggestions?
Actually, it is my problem for 1 week and I am really need help
card.blade.php:
<img src="{{($sku->product->image) }}" alt="{{ $sku->product->__('name') }}">
<div class="caption">
<h3>{{ $sku->product->__('name') }}</h3>
#isset($sku->product->properties)
#foreach ($sku->propertyOptions as $propertyOption)
<h4>{{ $propertyOption->property->__('name') }}: {{ $propertyOption->__('name') }}</h4>
#endforeach
#endisset
Sku:
class Sku extends Model
{
use SoftDeletes;
protected $fillable = ['product_id', 'count', 'price'];
protected $visible = ['id', 'count', 'price', 'product_name'];
public function product()
{
return $this->belongsTo(Product::class);
}
Product:
class Product extends Model
{
use SoftDeletes, Translatable;
protected $fillable = [
'name', 'code', 'price', 'category_id', 'description', 'image', 'hit', 'new', 'recommend', 'count', 'name_en',
'description_en'
];
public function category()
{
return $this->belongsTo(Category::class);
}
public function skus()
{
return $this->hasMany(Sku::class);
}
public function properties()
{
return $this->belongsToMany(Property::class, 'property_product')->withTimestamps();
}
MainController:
public function index(ProductsFilterRequest $request)
{
$skusQuery = Sku::with(['product', 'product.category']);
if ($request->filled('price_from')) {
$skusQuery->where('price', '>=', $request->price_from);
}
if ($request->filled('price_to')) {
$skusQuery->where('price', '<=', $request->price_to);
}
$skus = $skusQuery->paginate(6)->withPath("?".$request->getQueryString());
return view('index', compact('skus'));
}
whenever you call a relationship data make sure you check with null coalescing. So, if someone delete a data or failed to load the eloquent relation it doesn't break the system. You can do that by following this:
{{$data->relation?$data->relation->property:''}}
You can also try this alternative method
#if($data->relation)
//here are the properties you are tring to get
<span>{{$data->relation->name}}</span>
<span>{{$data->relation->phone}}</span>
#endif
In your case you can do these
<img src="{{($sku->product?$sku->product->image:'') }}" alt="{{ $sku->product?$sku->product->__('name'):'' }}">
<div class="caption">
<h3>{{ $sku->product?$sku->product->__('name'):'' }}</h3>
#isset($sku->product->properties)
#foreach ($sku->propertyOptions as $propertyOption)
<h4>{{ $propertyOption->property->__('name') }}: {{ $propertyOption->__('name') }}</h4>
#endforeach
#endisset
I am have some issue with eloquent relationship my first time working with it, after selecting and displaying list of selected events, I want to also select the ticket_id base on the event_id common between both(events and events_ticket) table, thanks for the help
Error Showing ->
Trying to get property 'ticket_id' of non-object (View: C:\wamp64\www\mahive\resources\views\tickets\index.blade.php)
Both Model
class EventsTicket extends Model
{
public $table = "events_ticket";
use HasFactory;
protected $fillable = [
'event_id',
'ticket_id',
'ticket_name',
'ticket_amount',
'ticket_sold',
];
public function event() {
return $this->belongsTo('App\Models\Event');
}
}
class Event extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'event_id',
'event_name',
'event_category',
'event_type',
'event_mode',
'event_description',
'event_image'
];
public function userModel() {
return $this->belongsTo('App\Models\User');
}
public function eticket() {
return $this->hasMany('App\Models\EventsTicket');
}
}
Controller
public function index()
{
$events = Event::where('ticket_statue', 'active')->with('eticket')->get();
return view('tickets.index', ['event_data' => $events]);
}
View
#foreach($event_data as $event)
{{ ucwords($event->event_name) }}
{{ $event->eticket->ticket_id }}
#endforeach
In this code every Event has many eticket. Try this:
#foreach($event_data as $event)
{{ ucwords($event->event_name) }}
{{ optional($event->eticket->first())->ticket_id }}
#endforeach
Use optional because there may not be any etickets.
I tried changing the routes and using a specific name and several things in the action attribute but there is no way the data doesn't appear when I use the form for the delete and the dd() function on my delete route
here is what the dd shows me, no info at all
My routes:
Route::get('/home/Collections', 'CollectionController#index')->name('collection.index');
Route::get('/home/Collections/{Collection}', 'CollectionController#show')->name('collection.show');
Route::get('/home/Collections/{Collection}/edit', 'CollectionController#edit')->name('collection.edit');
Route::put('/home/Collections/{Collection}', 'CollectionController#update')->name('collection.update');
Route::get('/home/Collections/crear', 'CollectionController#create')->name('collection.create');
Route::delete('/home/Collections/{Collection}', 'CollectionController#destroy')->name('collection.destroy');
Route::post('/home/Collections', 'CollectionController#store')->name('collection.store');
My controller:
public function destroy(Collection $collection)
{
$collection->delete();
return redirect('/home'.'/Collections');
}
and my form:
#foreach ($collections as $collection)
<div id="{{$collection->id}}">
<img/>
<p>{{$collection->name}}</p>
<p>{{$collection->description}}</p>
<form action="/home/Collections/{{$collection->id}}" method="POST">
#csrf
#method('DELETE')
<input type="submit" value="ELIMINAR" class = "btn btn-outline-danger mt-2">
</form>
</div>
#endforeach
my collection model:
class Collection extends Model implements Searchable
{
protected $fillable = ['id', 'name', 'description', 'user_id', 'category_id', 'certificate_id', 'img_id'];
public function items()
{
return $this->hasMany(Item::class);
}
public function certificate()
{
return $this->hasOne(Certificate::class);
}
public function image()
{
return $this->hasOne(Image::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function getSearchResult(): SearchResult
{
$url = route('collection.show', $this->id);
return new SearchResult(
$this,
$this->name,
$url
);
}
}
Problem solved a while ago, i forgot to reply to this post.
Solved it changing the Routes URL so they dont match, it seems there was a problem with them even tho it was working like exposed on another project.
I have a Recipe Model and FoodComponent Model which belongsTo Recipe.
RecipeModel :
class Recipe extends Model
{
protected $table = 'recipes';
protected $fillable = [
'details'
];
public function foods(){
return $this->hasMany('App\FoodComponent','id','food_id');
}
}
FoodComponent model:
class FoodComponent extends Model
{
protected $table = 'food_components';
protected $fillable = [ 'title',
'menu_id',
'image'
];
public function recipes(){
return $this->belongsTo('App\Recipe','id','food_id');
}
}
RecipeController:
public function index()
{
$recipes = Recipe::all();
return view('recipes.index')->with('recipes',$recipes);
}
And in the view I have
#foreach($recipes as $recipe)
{{ $recipe->foods }}
#endforeach
I get something like this :
[{"id":1,"menu_id":1,"title":"Pollo Locco","image":"images\/uploads\/foodcomponents\/99254.jpg","created_at":"2016-08-12 10:01:38","updated_at":"2016-08-12 10:01:38"}]
But I want just the 'title'.
I've tried with $recipe->foods->title. But is not working.
Can you give me an idea how can I display just the title please ?
Thank you !
Try this
#foreach($recipes as $recipe)
#foreach($recipe->foods as $food)
{{ $food->title }}
#endforeach
#endforeach