can somebody help with this? I'm trying to pass some data from my Controller page to my index, but I'm getting this error "Invalid argument supplied for foreach()"
Here's my model
class Bookings extends Model
{
//Table name
protected $table = 'bookings';
//Primary key
public $primaryKey = 'book_id';
}
My Controller
use Illuminate\Http\Request;
use App\Bookings;
class PagesController extends Controller
{
public function index(){
$data = Bookings::orderBy('NameENG', 'DetailsENG')->get();
$data = Bookings::all();
$marks = 'NameENG';
$briefs = 'DetailsENG';
return view('pages/index', compact(['data', 'marks', 'briefs']));
}
}
and part of my index
<div class = "container">
#if(count($data) > 0)
#foreach($marks as $mark)
#foreach($briefs as $brief)
<div class="row">
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<img class="card-img-top" alt="Thumbnail [100%x225]" style="height: 225px; width: 100%; display: block;" src="images/calle_yungay.png">
<div class="card-body">
<h5>{{$mark}}</h5>
<p>{{$brief}}</p>
Can someone help please?
On controller function:
$routes = 'tourNameENG'; // is a string
and on view, you are trying to iterate it:
#foreach($routes as $route)
that's why the error Invalid argument supplied for foreach().
foreach(): works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.
Related
Hello Guys, I am just passing my query to notification blade, but its gave error. I dont know what i did wrong with bellow code. If you guys fix this issue i will be very glad. Thanks in advance
Notification seen model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class notificationseen extends Model
{
use HasFactory;
protected $table = 'notificationseens';
public function Notification()
{
return $this->belongsTo(Notification::class, 'notificationID');
}
}
Notification Model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
use HasFactory;
protected $table = 'notifications';
public function notificationseen()
{
return $this->belongsTo(notificationseen::class, 'notificationID');
}
}
View Blade
#foreach( $notification as $notify )
#if($notify->Notification->seen == 0)
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}" id="notifysee" data-id="{{ $notify->id }}">
<div class="alert unread custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
#else
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}">
<div class="alert custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
#endif
#endforeach
Table structure
$table->increments('id');
$table->integer('userid');
$table->integer('notificationID');
$table->integer('seen')->default('0')->comment("0 for unseen 1 for seen");
$table->timestamps();
Can you please help me out. I cant see any issue but its me error "Attempt to read property "seen" on null"
Ok, some things:
I would use belongsTo in the notificationseen class unless one notificationseen could have more than one Notifications to belong to ;)
Do a Notification have more than one notificationseen references? I do not think so, so in your Notification change the reference to hasOne.
In your blade, use #if($notify->notificationseen->seen == 0) or you call better "Notification::with('notificationseen')->get()" in your controller and then pass it to your view.
You can try to add an isset to avoid the error :
#if(isset($notify->Notification->seen) && $notify->Notification->seen == 0)
(...)
#else
(...)
#endif
EDIT : in your code, you defined 2 belongsTo methods, but according to the official Laravel documentation, you must define a hasOne method and the inverse of it, the belongsTo method.
I am working on a laravel project. In this project, when i try to view and edit the profile page. I am getting this error.
ErrorException
Trying to get property 'image' of non-object (View: C:\xampp\htdocs\HomeServices\resources\views\livewire\sprovider\sprovider-profile-component.blade.php)
http://127.0.0.1:8000/sprovider/profile
SproviderProfileComponent.php :-
<?php
namespace App\Http\Livewire\Sprovider;
use App\Models\ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class SproviderProfileComponent extends Component
{
public function render()
{
$sprovider = ServiceProvider::where('user_id',Auth::user()->id)->first();
return view('livewire.sprovider.sprovider-profile-component',['sprovider'=>$sprovider])->layout('layouts.base');
}
}
Models/ServiceProvider.php :-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ServiceProvider extends Model
{
use HasFactory;
protected $fillable = ['user_id'];
public function category()
{
return $this->belongsTo(ServiceCategory::class,'service_category_id');
}
}
sprovider-profile-component.blade.php
<div class="panel-body">
<div class="row">
<div class="col-md-4">
#if($sprovider->image)
<img src="{{asset('images/sproviders')}}/{{$sprovider->image}}" width="100%" />
#else
<img src="{{asset('images/sproviders/default.jpg')}}" width="100%" />
#endif
</div>
<div class="col-md-8">
<h3>Name: {{Auth::user()->name}}</h3>
<p>{{$sprovider->about}}</p>
<p><b>Email: </b>{{Auth::user()->email}}</p>
<p><b>Phone: </b>{{Auth::user()->phone}}</p>
<p><b>City: </b>{{$sprovider->city}}</p>
<p><b>Service Category: </b>
#if($sprovider->service_category_id)
{{$sprovider->category->name}}
#endif
</p>
<p><b>Service Locations: {{$sprovider->service_locations}}</b></p>
Edit Profile
</div>
</div>
</div>
The only reason this could happen is because $sprovider is null.
When it happens, $sprovider->image will translate to (null)->image, which is indeed Trying to get property 'image' of non-object.
You could do this to prevent $sprovider from being null:
$sprovider = ServiceProvider::where('user_id',Auth::user()->id)->firstOrFail();
By using firstOrFail instead of first, you ensure $sprovider will never be null (like the name suggests, if it doesn't find any provider, it will fail).
You will have another error saying that no provider could be found, this is another issue, probably because you don't have any provider for this user or something like that.
Two possibilities
$sprovider is null and
image key is not available in $sprovider ($sprovider->image) may be you mis-spelled image in database table so for that just use dd($sprovider); or print_r($sprovider); and check image key is available or not.
I have a question about Laravel - Documentation - Accessing Attributes & Slots Within Component Classes
I read this section, and did some experiments. What I've found is that the closure only can return string but component.view, and what it returns would overwrite what is defined on the component view, which leads me to a question What use case is this feature for?
Could anyone make some examples of using this feature for me?
Anyone could help me with it will be so much appreciated.
If the string matches an existing view then it will render that view and not overwrite it. You can return an existing view as follows:
// app/View/Components/Post.php
public function render()
{
return function (array $data) {
// $data['componentName'];
// $data['attributes'];
// $data['slot'];
return 'components.post'; // /resources/views/components/post.blade.php
};
}
// views/components/post.blade.php
<div class="item">
<div class="title">
New Post
</div>
<div class="content">
<p>Content post...</p>
</div>
</div>
Example:
// component class
class Link extends Component
{
public $path = "";
public function __construct()
{
$this->path = request()->path();
}
public function render()
{
return function (array $data) {
if(isset($data['attributes']['href'])) {
$data['attributes']["link"] = $data['attributes']['href'];
if ($data['attributes']['href'] == "/".$this->path) {
$data['attributes']['active'] = $data['attributes']['class']." link-active-class";
} else {
$data['attributes']['active'] = $data['attributes']['class'];
}
}
return 'components.commons.link';
};
}
}
// component view
<a href="{{ $attributes['link'] }}" class="{{ $attributes['active'] }}">
{{ $slot }}
</a>
Usage:
<x-commons.link href="/module/news" class="primary-action">
<i class="fas fa-newspaper"></i> News
</x-commons.link>
In the show function in my controller I passed the variable $professors
In the controller method I am passing the variable like this -
public function show(Professor $professor)
{
$professors = Professor::all();
return view('professor.show',compact('professors'));
}
I am trying to display the professor name in the show.blade.php like this
<div class="form-group">
<div class="row">
<strong class="col-sm-2">Name:</strong>
{{$professors->name}}
</div>
</div>
I'm getting this error-
Property [name] does not exist on this collection instance.
Where am I doing wrong ?
I found the solution
public function show(Professor $professor)
{
$professors = Professor::find($professor->id);
return view('professor.show',compact('professors'));
}
I forgot the find($professor->id); works for me now guys
I'm still a novice to Laravel 4 and trying to figure out why I'm getting an error saying that Method [inventoryNotFound] does not exist. It has to do when I am attempting to use the InventoryController#show function.
On my Index page I have a href that I'm trying to send to the show views. The code looks like this:
#foreach($inventory as $item)
<div class="col-md-4 inventory">
<img class="img-responsive" src="{{{$item->image}}}">
<a class="description" href="">{{{$item->title}}}</a>
#foreach($item->size_details as $details)
<p>{{ $details->size }}: {{ $details->price }}</p>
#endforeach
</div>
#endforeach
My show function is:
class InventoryController extends BaseController {
public function index()
{
$inventory = Inventory::all();
return View::make('inventories.index')->with('inventory', $inventory);
}
public function show($id)
{
$inventory = Inventory::find($id);
if(is_null($inventory)){
return $this->inventoryNotFound();
}
return View::make('inventories.show',['inventory' => $inventory]);
}
}
And I have a resource controller as my routes:
Route::resource('inventories', 'InventoryController');
Even when I attempt to run my routes manually, I run into the same problem.
Any ideas as to why?