Undefined variable $services - php

I have this error. I did this a couple of times on a different pages with different subjects , but with the same method. It always worked. But for some reason now I'm getting this error.
Undefined variable $services (View:C\xampp\htdocs\accounting\resources\views\livewire\service-table.blade.php)
Possible type $services Did you mean $errors?
Web route
//Service Controller
Route::resource('service', ServiceController::class)->middleware('auth');
Model
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Service extends Model
{
public $table = 'service';
use HasFactory;
protected $fillable = [
'service_name',
'service_description',
'service_cost'
];
}
My controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Service;
use Illuminate\Support\Facades\DB;
class ServiceController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$services = Service::orderBy('id', 'asc')->paginate(5);
return view('service.index', compact('services'))->with(request()->input('page'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//Validate the input
$request->validate([
'service_name' => 'required',
'service_cost' => 'required',
'service_description' => 'required'
]);
Service::create($request->all());
return redirect()->route('service.index')->with('success', 'The service was created successfully');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Livewire table
<div>
<table class="table table-borderless">
<thead>
<tr>
<th style="width: 5%">Id</th>
<th style="width: 25%">Service Name</th>
<th style="width: 20%">Service Cost</th>
<th style="width: 25%">Service Description</th>
<th style="width: 10%">EDIT/DELETE</th>
</tr>
</thead>
<tbody>
#foreach($services as $service)
<tr>
<td class="table_row_middle">{{$service->id}}</td>
<td class="table_row_middle">{{$service->service_name}}</td>
<td class="table_row_middle">{{$service->service_cost}}</td>
<td class="table_row_middle">{{$service->service_description}}</td>
<td>
<form action="{{route('service.destroy', $service->id)}}" method="post">
#csrf
#method('DELETE')
<div class="flex float-right">
<a class="btn btn-primary" style="margin-right: 10px;" href="{{route('service.edit', $service->id)}}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
<path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456l-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>
<path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>
</svg>
</a>
<button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#delete_service">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-file-earmark-x" viewBox="0 0 16 16">
<path d="M6.854 7.146a.5.5 0 1 0-.708.708L7.293 9l-1.147 1.146a.5.5 0 0 0 .708.708L8 9.707l1.146 1.147a.5.5 0 0 0 .708-.708L8.707 9l1.147-1.146a.5.5 0 0 0-.708-.708L8 8.293 6.854 7.146z"/>
<path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5v2z"/>
</svg>
</button>
<div class="modal" tabindex="-1" id="delete_service">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete the service</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Do you want to delete: {{$service->service_name}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger flex " data-bs-toggle="modal" data-bs-target="#delete_service">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-file-earmark-x" viewBox="0 0 16 16">
<path d="M6.854 7.146a.5.5 0 1 0-.708.708L7.293 9l-1.147 1.146a.5.5 0 0 0 .708.708L8 9.707l1.146 1.147a.5.5 0 0 0 .708-.708L8.707 9l1.147-1.146a.5.5 0 0 0-.708-.708L8 8.293 6.854 7.146z"/>
<path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5v2z"/>
</svg>
Delete
</button>
</div>
</div>
</div>
</div>
</div>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="col-md-12 p-2">
{!! $services->links() !!}
</div>
</div>
And here is the view
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Services') }}
</h2>
</x-slot>
<div class="container mt-5 p-1" style="max-width: 1280px;">
<div class="row">
<div class="col-sm-3 mb-5">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
#if($errors->any())
<div class="alert alert-danger" style="margin:1rem;">
<ul>
#foreach($errors->all() as $error)
<li>
{{$error}}
</li>
#endforeach
</ul>
</div>
#endif
#if($message = Session::get('success'))
<div class="alert alert-success" style="margin:1rem;">
<p>{{$message}}</p>
</div>
#endif
<!--Add new Client-->
<form action="{{route('service.store')}}" method="post">
#csrf
<div class="col-md-12 p-1">
<label for="serviceName" class="form-label">Service</label>
<input type="text" class="form-control" id="serviceName" name="service_name">
</div>
<div class="col-md-12 p-1">
<label for="serviceCost" class="form-label">Service Cost (EUR/HOUR)</label>
<input type="text" class="form-control" id="serviceCost" name="service_cost">
</div>
<div class="col-md-12 p-1">
<label for="serviceDescription" class="form-label">Service Description</label>
<textarea type="text" rows="3" class="form-control" id="serviceDescription" name="service_description"></textarea>
</div>
<div class="col-md-12 p-1">
<button type="submit" class="btn btn-primary">Add New Service</button>
</div>
</form>
</div>
</div>
</div>
<div class="col-sm-9">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<livewire:service-table/>
</div>
</div>
</div>
</div>
</div>
</x-app-layout>

You are sending data to the wrong view. You are sending data to service.index view but trying to get it in livewire\service-table so change the view like below then you can get it.
as the error explains it
Undefined variable $services (View:C\xampp\htdocs\accounting\resources\views\livewire\service-table.blade.php)
the problem is here return view('service.index', compact('services'))->with(request()->input('page'));
Try this
public function index()
{
$services = Service::orderBy('id', 'asc')->paginate(5);
return view('livewire.service-table', compact('services'))->with(request()->input('page'));
}

So the answer is simple, I passed the $services variable to the services.index page however because I included the livewire table the table did not get the $services variable. I just forgot that I have to pass the $services variable to the services-table.view on the table controller.
The problem is solved. Thank you for your help !

Related

HTML form - submit reload the page only, but does nothing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
I'm experimenting on Laravel recently so I apologize in advance if the question may be stupid.
I'm trying to insert an item into one of my tables, but when I click the submit button the page just reloads. The data is not saved.
It looks like the store function isn't really executed, but the form doesn't seem incorrect. What am I doing wrong? i've put the #csrf in the code.
Thanks in advance to anyone who will try to help me.
<body>
<div class="container">
<h1> `Modifica Ingredienti `</h1>
<div class="row">
<div class="card">
<div class="card-header">
Aggiungi un ingrediente
</div>
<div class="card-body">
<table class="table">
<thead>`
<tr>
<th>Ingrediente </th>
<th>Quantità </th>
<th>Unità di misura </th>
<th>Modifica </th>
<th>Cancella </th>
</tr>
</thead>
<tbody>
#foreach($recipe_ingredient as $rp)
<tr>
<td> {{ $rp->ingredient_name}}</td>
<td> {{ $rp->quantity }} </td>
<td> {{ $rp->measure }} </td>
<td> <a href="{{ route('recipe_ingredient.single_edit', $rp->id) }}" class="text-decoration-none"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
<path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>
<path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>
</svg> </a> </td>
<td> <a href="{{ route('recipe_ingredient.delete', $rp->id) }}" class="text-decoration-none"> <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-trash3" viewBox="0 0 16 16">
<path d="M6.5 1h3a.5.5 0 0 1 .5.5v1H6v-1a.5.5 0 0 1 .5-.5ZM11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3A1.5 1.5 0 0 0 5 1.5v1H2.506a.58.58 0 0 0-.01 0H1.5a.5.5 0 0 0 0 1h.538l.853 10.66A2 2 0 0 0 4.885 16h6.23a2 2 0 0 0 1.994-1.84l.853-10.66h.538a.5.5 0 0 0 0-1h-.995a.59.59 0 0 0-.01 0H11Zm1.958 1-.846 10.58a1 1 0 0 1-.997.92h-6.23a1 1 0 0 1-.997-.92L3.042 3.5h9.916Zm-7.487 1a.5.5 0 0 1 .528.47l.5 8.5a.5.5 0 0 1-.998.06L5 5.03a.5.5 0 0 1 .47-.53Zm5.058 0a.5.5 0 0 1 .47.53l-.5 8.5a.5.5 0 1 1-.998-.06l.5-8.5a.5.5 0 0 1 .528-.47ZM8 4.5a.5.5 0 0 1 .5.5v8.5a.5.5 0 0 1-1 0V5a.5.5 0 0 1 .5-.5Z"/>
</svg> </a> </td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
`
`<!-- `Modal di inserimento` -->
<div class="modal fade" id="`ingredientModal" tabindex`="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Aggiungi:</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="{{ route('recipe_ingredient.add') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="modal-body">`
`
#foreach ($recipe_ingredient as $ri)
<input type ="hidden" id="recipe_id" class="recipe_id form-control" value="{{ $ri->recipe_id }}" >
#endforeach
<div class="form-group">
<label for="seleziona ingrediente"> </label>
<select id="ingredient_id" class="ingredient_id form-control" name="ingredient_id">
#foreach ($ingredients as $ingredient)
<option value="{{ $ingredient->id }}"> {{$ingredient->name_ingredient }} </option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="quantity"> </label>
<input type="number" id="quantity" min="1" max="5000" class="quantity form-control" placeholder="Quantità:">
</div>
<div class="form-group">
<label for="Unità di misura:"> </label>
<select id="measure" class="measure form-control" name="measure">
#foreach($measurements as $measurement)
<option value="{{ $measurement->name_measurement }}"> {{$measurement->name_measurement }} </option>
#endforeach
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Chiudi</button>
<button type="submit" class="btn btn-primary" > Aggiungi </button>
</div>
</form>
</div>
</div>
</div>
----- STORE IN CONTROLLER-----
<?php
public function edit($id)
{
$recipe_ingredient = `RecipeIngredient`::where('recipe_id', $id)->get();
foreach($recipe_ingredient as $ri){
$ing_id[] = $ri->ingredient_id;
}
foreach($ing_id as $ing_id){
$ing_names[] = app('App\Http\Controller\IngredientController')->name_ingredient($ing_id);
}
$i = 0;
foreach($recipe_ingredient as $ri){
$ri->ingredient_name = $ing_names[$i];
$i = $i + 1;
}
return view('/recipe_ingredient/edit', compact('recipe_ingredient'));
}
public function store(Request $request)
{
$request->validate([
'recipe_id' => 'required',
'ingredient_id' => 'required',
'quantity' => 'required',
'measure' => 'required'
]);
$recipe_ingredient = new RecipeIngredient();
$recipe_ingredient->recipe_id = $request-> recipe_id;
$recipe_ingredient->ingredient_id = $request-> ingredient_id;
$recipe_ingredient->quantity = $request-> quantity;
$recipe_ingredient->measure = $request-> measure;
$recipe_ingredient->save();
$id = $recipe_ingredient->recipe_id;
$recipe_ing = RecipeIngredient::where('recipe_id', $recipe_id)->get();
foreach($recipe_ingredient as $ri){
$ing_id[] = $ri->ingredient_id;
}
foreach($ing_id as $ing_id){
$ing_names[] = app('App\Http\Controllers\IngredientController')->name_ingredient($ing_id);
}
$i = 0;
foreach($recipe_ingredient as $ri){
$ri->ingredient_name = $ing_names[$i];
$i = $i + 1;
}
//return view('/home', compact('recipe_ingredient'))->with('id', $recipe_id);
return redirect()->back();
}
?>
`

Meilisearch Laravel Livewire component - unchecked checkboxes disappearing when using wire:model

I'm building a full page Livewire component that filters Meilisearch results.
When I check one of the filters the remaning checkboxes disappear, and I am struggling to understand why. I followed a tutorial to figure out how to build the bulk of the component and in the tutorial the behaviour didn't occur. I've deconstructed the component to the bare minimum and the behaviour happens as soon as I wire up the checkboxes.
Here is the component:
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\CourseDate;
use App\Models\Course;
class CourseBrowser extends Component
{
public $queryFilters = [];
public $priceRange = [
'min' => null,
'max' => null
];
public function mount()
{
$this->queryFilters = [
'venue' => [],
'type' => [],
'category' => [],
'days' => [],
'supplier' => []
];
}
public function render()
{
$search = CourseDate::search(
'',
function ($meilisearch, string $query, array $options) {
$filters = collect($this->queryFilters)
->filter(fn ($filter) => !empty($filter))
->recursive()
->map(function ($value, $key) {
return $value->map(fn ($value) => $key . ' = "' . $value . '"');
})
->flatten()
->join(' AND ');
$options['facets'] = ['venue', 'category', 'type', 'supplier', 'days'];
$options['filter'] = null;
if ($filters) {
$options['filter'] = $filters;
}
if ($this->priceRange['max']) {
$options['filter'] .= (isset($options['filter']) ? ' AND ' : '') . 'price <= ' . $this->priceRange['max'];
}
return $meilisearch->search($query, $options);
}
)->raw();
$coursedates = CourseDate::find(collect($search['hits'])->pluck('id'));
$minPrice = Course::all()->min('price');
$maxPrice = Course::all()->max('price');
$this->priceRange['min'] = $this->priceRange['min'] ?: $minPrice;
$this->priceRange['max'] = $this->priceRange['max'] ?: $maxPrice;
return view('livewire.course-browser')
->with(
[
'coursedates' => $coursedates,
'filters' => $search['facetDistribution'],
'minPrice' => $minPrice,
'maxPrice' => $maxPrice,
]
);
}
}
Here is the Blade view. I had originally posted an edited section showing just the checkboxes but I have edited this question to show the view in its entirety.
<div>
<div class="mb-4 text-gray-500">
Home / Courses
</div>
<div class="align-center mt-8 mb-10 flex items-center justify-between">
<div class="text-3xl font-bold">
All Courses
</div>
</div>
<div class="grid w-full grid-cols-4 gap-6">
<div class="col-span-1 rounded-sm bg-white shadow">
<div class="space-y-2 px-6 py-6">
<h6 class="text-sm font-bold uppercase">Filters</h6>
<hr class="mt-4 mb-6 border-gray-200 pb-2">
<h6 class="text-sm font-bold uppercase">Price </h6>
<div class="flex gap-1">
<input type="range"
min="{{ $minPrice }}"
max="{{ $maxPrice }}"
step="25"
class="accent-pink-700"
wire:model="priceRange.max" />
<span class="font-sm">
(£{{ $priceRange['max'] }})
</span>
</div>
#foreach ($filters as $title => $filter)
<h6 class="pt-2 text-sm font-bold uppercase">{{ Str::title($title) }}</h6>
#foreach ($filter as $option => $count)
<div wire:ignore.self
class="flex items-center space-x-2">
<input type="checkbox"
class="h-4 w-4 rounded border-gray-300 text-sm text-pink-600 focus:ring-0 focus:ring-offset-0"
wire:model="queryFilters.{{ $title }}"
id="{{ $title }}_{{ strtolower($option) }}"
value="{{ $option }}">
<label class="text-sm"
for="{{ $title }}_{{ strtolower($option) }}">{{ $option }} ({{ $count }})</label>
</div>
#endforeach
#endforeach
</div>
</div>
<div class="col-span-3">
<h6 class="text-md my-4 mb-4">{{ $coursedates->count() }} {{ Str::plural('course', $coursedates) }} matching your filters</h6>
#forelse ($coursedates as $coursedate)
<div wire:loading.class="opacity-50"
class="relative mb-6 w-full rounded-sm bg-white shadow">
<span class="absolute top-2 left-2 rounded bg-pink-800 px-3 py-1 font-bold text-white">{{ $coursedate->course->category->name }}</span>
<span class="absolute top-2 right-2 rounded bg-pink-600 px-3 py-1 font-bold text-white">{{ $coursedate->course->supplier->name }}</span>
<a href="{{ route('course.show', $coursedate->course->slug) }}">
<img src="{{ asset('storage/courses/images/' . $coursedate->course->title_image) }}"
alt="{{ $coursedate->course->slug }}"
class="h-40 w-full object-cover">
</a>
<div class="px-6 py-4">
<h6 class="mb-2 text-lg font-bold">{{ $coursedate->course->name }} </h6>
<p class="mb-6 text-sm leading-tight">{{ $coursedate->course->tagline }}</p>
{{-- Pills Container --}}
<div class="flex">
<div class="mr-1 flex items-center rounded bg-pink-300 font-bold text-pink-800">
<div class="flex h-full items-center rounded-l bg-pink-400 px-2 py-1">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
class="h-4 w-4 text-pink-200">
<path d="M4.5 3.75a3 3 0 00-3 3v.75h21v-.75a3 3 0 00-3-3h-15z" />
<path fill-rule="evenodd"
d="M22.5 9.75h-21v7.5a3 3 0 003 3h15a3 3 0 003-3v-7.5zm-18 3.75a.75.75 0 01.75-.75h6a.75.75 0 010 1.5h-6a.75.75 0 01-.75-.75zm.75 2.25a.75.75 0 000 1.5h3a.75.75 0 000-1.5h-3z"
clip-rule="evenodd" />
</svg>
</div>
<div class="flex h-full items-center rounded bg-pink-300 px-3 py-1">
£{{ $coursedate->course->price }}
</div>
</div>
<div class="mr-1 flex items-center rounded bg-pink-500 font-bold text-white">
<div class="flex h-full items-center rounded-l bg-pink-600 px-2 py-1">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
class="h-4 w-4">
<path fill-rule="evenodd"
d="M11.54 22.351l.07.04.028.016a.76.76 0 00.723 0l.028-.015.071-.041a16.975 16.975 0 001.144-.742 19.58 19.58 0 002.683-2.282c1.944-1.99 3.963-4.98 3.963-8.827a8.25 8.25 0 00-16.5 0c0 3.846 2.02 6.837 3.963 8.827a19.58 19.58 0 002.682 2.282 16.975 16.975 0 001.145.742zM12 13.5a3 3 0 100-6 3 3 0 000 6z"
clip-rule="evenodd" />
</svg>
</div>
<div class="flex h-full items-center rounded bg-pink-500 px-3 py-1">
{{ $coursedate->venue->city }}
</div>
</div>
#foreach ($coursedate->actualdates as $dates)
#if ($coursedate->course->number_of_days > 1)
<div class="mr-1 flex items-center rounded bg-pink-200 font-bold text-pink-800">
<div class="flex h-full items-center gap-1 rounded-l bg-pink-200 px-2 py-1 text-xs">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
class="inline-flex h-5 w-5">
<path d="M12.75 12.75a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM7.5 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM8.25 17.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM9.75 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM10.5 17.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM12 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM12.75 17.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM14.25 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM15 17.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM16.5 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM15 12.75a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM16.5 13.5a.75.75 0 100-1.5.75.75 0 000 1.5z" />
<path fill-rule="evenodd"
d="M6.75 2.25A.75.75 0 017.5 3v1.5h9V3A.75.75 0 0118 3v1.5h.75a3 3 0 013 3v11.25a3 3 0 01-3 3H5.25a3 3 0 01-3-3V7.5a3 3 0 013-3H6V3a.75.75 0 01.75-.75zm13.5 9a1.5 1.5 0 00-1.5-1.5H5.25a1.5 1.5 0 00-1.5 1.5v7.5a1.5 1.5 0 001.5 1.5h13.5a1.5 1.5 0 001.5-1.5v-7.5z"
clip-rule="evenodd" />
</svg>
{{ $dates->label }}
</div>
<div class="flex h-full items-center rounded-r bg-pink-700 px-2 py-1 text-pink-100">
{{ $dates->date->format('jS M Y') }}
</div>
</div>
#else
<div class="mr-1 flex items-center rounded bg-pink-200 text-sm font-bold text-pink-800">
<div class="flex h-full rounded-l bg-pink-200 py-1 px-2 text-sm font-bold text-pink-800">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
class="inline-flex h-5 w-5">
<path d="M12.75 12.75a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM7.5 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM8.25 17.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM9.75 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM10.5 17.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM12 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM12.75 17.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM14.25 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM15 17.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM16.5 15.75a.75.75 0 100-1.5.75.75 0 000 1.5zM15 12.75a.75.75 0 11-1.5 0 .75.75 0 011.5 0zM16.5 13.5a.75.75 0 100-1.5.75.75 0 000 1.5z" />
<path fill-rule="evenodd"
d="M6.75 2.25A.75.75 0 017.5 3v1.5h9V3A.75.75 0 0118 3v1.5h.75a3 3 0 013 3v11.25a3 3 0 01-3 3H5.25a3 3 0 01-3-3V7.5a3 3 0 013-3H6V3a.75.75 0 01.75-.75zm13.5 9a1.5 1.5 0 00-1.5-1.5H5.25a1.5 1.5 0 00-1.5 1.5v7.5a1.5 1.5 0 001.5 1.5h13.5a1.5 1.5 0 001.5-1.5v-7.5z"
clip-rule="evenodd" />
</svg>
</div>
<div class="flex h-full items-center rounded-r bg-pink-700 px-2 py-1 text-pink-100"> {{ $dates->date->format('jS M Y') }} </div>
</div>
#endif
#endforeach
</div>
<div class="mt-6 mb-4">
</div>
</div>
</div>
#empty
No Course Dates to show
#endforelse
</div>
</div>
</div>
The component in itself works fine. But as you can see in the gif below, the non selected checkboxes disappear, so I cannot apply multiple filters.
I suspect the problem lies in the way I'm storing the filters in the array but I can't see the woods for the trees at the moment. The filtering itself currently works, and any attempt to change the format of the array results in it not working at all.
Thanks in advance.
After some perusal of the Meilisearch docs:
facetDistribution contains an object for every given facet. For each of these facets, there is another object containing all the different values and the count of matching documents. Note that zero values will not be returned: if there are no romance movies matching the query, romance is not displayed.
That suggests that the behaviour is normal 😳
On updating the search results, the facetDistribution array is updated with the filters belonging to the results that have been returned, and doesn't retain the ones with a zero value.
Before filtering:
"facetDistribution":{
"category":{
"Hair":8
},
"days":{
"1":4,
"2":4
},
"supplier":{
"Matrix":8
},
"type":{
"In Person":8
},
"venue":{
"Colchester":2,
"Ipswich":1,
"Norwich":1,
"Peterborough":1,
"Romford":3
}
},
After filtering:
"facetDistribution":{
"category":{
"Hair":2
},
"days":{
"1":1,
"2":1
},
"supplier":{
"Matrix":2
},
"type":{
"In Person":2
},
"venue":{
"Colchester":2
}
},
To be honest, I'd expected to retain the filters with a value of zero so I could re-filter and find courses that were present in Colchester OR Norwich, for example. In fact the tutorial I learned the process from appeared to show this behaviour.
I'm going to rewatch it again in case I misunderstood something!
Or perhaps the older version of Meilisearch being used in the tutorial allowed this?
There are certain bugs in Livewire, and this is also one of them.
Try enclosing the each checkbox in a div.
<div class="">
<input type="checkbox" wire:model="queryFilters.{{ $title }}"
id="{{ $title }}_{{ strtolower($option) }}"
value="{{ $option }}">
</div>
If the issue still presist, add wire:ignore.self to container
<div class="" wire:ignore.self>
......
</div>
I've dumped the contents of the array as it's being filled up on the page
Each time you check a box it adds the value into the array. Obviously In the case of the Venue group of checkboxes I can't add a second value because the unselected ones disappear. But what I'd expect is an array that looks like this for example:
$this->queryFilters = [
'venue' => ['Norwich', 'Peterborough'],
'type' => ['In Person'],
'category' => ['Hair'],
'days' => [],
'supplier' => []
];
I'd then map through the values and build the filter string for Mielesearch, which happens in the closure futher down.
"venue = "Norwich" AND venue = "Peterborough" AND type = "In Person" AND category = "Hair""

Laravel 8 Undefined variable: comments

I am trying to build a commenting system where users are able to leave comments behind on posts.
I can save and display the post and on a page i display all post I have a form where users can leave comments behind. I am able to save the comments in the database but when I try to show the comments I get this error Undefined variable: comments (View: /var/www/resources/views/projects/comment.blade.php).
My files: Comment Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Comment extends Model
{
use HasFactory, SoftDeletes;
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['user_id', 'post_id', 'parent_id', 'description'];
/**
* Write Your Code..
*
* #return string
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Write Your Code..
*
* #return string
*/
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
}
Post Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class post extends Model
{
use HasFactory, SoftDeletes;
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [ 'titre',
'description',
'image',
'tag',
'video',
'status',
'user_id'];
/**
* Write Your Code..
*
* #return string
*/
public function comments()
{
return $this->hasMany(Comment::class)->whereNull('parent_id');
}
}
My CommentController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Comment;
class CommentController extends Controller
{
/**
* Write Your Code..
*
* #return string
*/
public function store(Request $request)
{
$input = $request->all();
$request->validate([
'body'=>'required',
]);
$input['user_id'] = auth()->user()->id;
Comment::create($input);
return back();
}
}
post display controller
public function post_display()
{
if(Auth::id()){
$userid=Auth::user()->id;
$profile=user::find($userid);
$post=post::where('user_id',$userid)->get();
return view('user.post_display',compact('post','profile'));
}
else{
return redirect()->back();
}
}
My post display view
#include('user.head')
#include('user.create_post')
<section class="ftco-section">
<div class="container" align="center" >
#foreach($post as $posts)
#if(session()->has('message2'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">
x
</button>
{{session()->get('message2')}}
</div>
#endif
<div class="col-md-8 col-xl-6 middle-wrapper">
<div class="row">
<div class="col-md-12 grid-margin">
<div class="card rounded">
<div class="card-header">
<div class="d-flex align-items-center justify-content-between">
<div class="d-flex align-items-center">
<img class="img-xs rounded-circle" src="https://bootdey.com/img/Content/avatar/avatar6.png" alt="">
<div class="ml-2">
<p>{{$profile->username}}</p>
<p class="tx-11 text-muted">{{$posts->created_at}}</p>
</div>
</div>
<div class="dropdown">
#include('user.update_post')
</div>
</div>
</div>
<div class="card-body">
<h5> {{$posts->tag}}
</h5>
<h4 class="mb-3 tx-14"> {{$posts->status}}</h4>
<p class="mb-3 tx-14"> {{$posts->description}}</p>
<img class="img-fluid" src="postimage/{{$posts->image}}" alt="">
</div>
<div class="card-footer">
<div class="d-flex post-actions">
<a href="javascript:;" class="d-flex align-items-center text-muted mr-4">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-heart icon-md">
<path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"></path>
</svg>
<p class="d-none d-md-block ml-2">Like</p>
</a>
<h4>Display Comments</h4>
<hr />
#include('user.comment', ['comments' => $posts->comments, 'post_id' => $posts->id])
<h4>Add comment</h4>
<form method="post" action="{{ route('comments.store') }}">
#csrf
<div class="form-group">
<textarea class="form-control" name="description"></textarea>
<input type="hidden" name="post_id" value="{{ $posts->id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Add Comment" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endforeach
</div>
</section>
#include('user.footer')
<!-- loader -->
<div id="ftco-loader" class="show fullscreen"><svg class="circular" width="48px" height="48px"><circle class="path-bg" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke="#eeeeee"/><circle class="path" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke-miterlimit="10" stroke="#F96D00"/></svg></div>
#include('user.script')
</body>
my comment dispaly view
#foreach($comments as $comments)
<div class="display-comment" #if($comments->parent_id != null) style="margin-left:40px;" #endif>
<strong>{{ $comments->user->name }}</strong>
<p>{{ $comments->body }}</p>
<form method="post" action="{{ route('comments.store') }}">
#csrf
<div class="form-group">
<input type="text" name="body" class="form-control" />
<input type="hidden" name="post_id" value="{{ $post_id }}" />
<input type="hidden" name="parent_id" value="{{ $comments->id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" value="Reply" />
</div>
<hr />
</form>
#include('user.comment', ['comments' => $comments->replies])
</div>
#endforeach
My comment routes:
Route::post('comment',[CommentController::class,'comment'])->name('comments.store');
whene i try to create a new laravel project and put nothing other then post and comment it work.so i don't know where is the error.

SQLSTATE[HY000]: General error: 1364 Field 'banner_photo' doesn't have a default value

N.B: I'm using laravel 7.x,
I want to insert photo to my database but the error says, General error: 1364 Field 'banner_photo' doesn't have a default value
Here is my index.blade.php file
#extends('admin.layout.master')
#section('content')
<div class="container-scroller">
<!-- partial:partials/_navbar.html -->
#include('admin.layout.nav')
<!-- partial -->
<div class="container-fluid page-body-wrapper">
<!-- partial:partials/_sidebar.html -->
#include('admin.layout.sidebar')
<!-- partial -->
<div class="main-panel">
<div class="content-wrapper">
<!-- Page Title Header Starts-->
<div class="row page-title-header">
<div class="col-12">
<div class="page-header">
<h4 class="page-title">Dashboard</h4>
</div>
</div>
</div>
<!-- Page Title Header Ends-->
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12 grid-margin">
<div class="card">
<div class="card-body">
<div class="d-flex justify-content-between">
<h4 class="card-title mb-0">Banner</h4>
<small>Show All</small>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est quod cupiditate esse fuga</p>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>H4 Title</th>
<th>H2 Title</th>
<th>Paragraph</th>
<th>Image</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach($banners as $row)
<tr>
<td>{{$row->id}}</td>
<td>{{$row->h4_title}}</td>
<td>{{$row->h2_title}}</td>
<td>{{$row->banner_paragraph}}</td>
<td>{{$row->banner_image}}</td>
<td><button type="submit" class="btn btn-primary"><i class="fas fa-edit"></i>EDIT</button></td>
<td><button type="submit" class="btn btn-danger"><i class="far fa-trash-alt"></i>DELETE</button></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
<!-- partial:partials/_footer.html -->
#include('admin.layout.footer')
<!-- partial -->
</div>
<!-- main-panel ends -->
</div>
<!-- page-body-wrapper ends -->
</div>
<!-- container-scroller -->
#endsection
Here is admin panel bannercustomize.blade.php
#extends('admin.layout.master')
#section('content')
<div class="container-scroller">
#include('admin.layout.nav')
<div class="container-fluid page-body-wrapper">
#include('admin.layout.sidebar')
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-md-12 d-flex align-items-stretch grid-margin">
<div class="row flex-grow">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="card-title">Banner Customize</h4>
#if(count($errors) >0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
#if(\Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<p>{{\Session::get('success')}}</p>
</div>
#endif
<form method="post" action="{{route('store.banner')}}" class="form-sample" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="exampleInputEmail1">H4 Title</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter H4 Title" name="h4_title">
</div>
<div class="form-group">
<label for="exampleInputEmail1">H2 Title</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter H2 Title" name="h2_title">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Paragraph</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter Paragraph" name="banner_paragraph">
</div>
<div class="form-group">
<input type="file" id="exampleInputEmail1" name="banner_photo">
</div>
<button type="submit" class="btn btn-success">Submit</button>
<button type="submit" class="btn btn-light">Cancle</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#include('admin.layout.footer')
</div>
</div>
</div>
#endsection
Here is my route web.php file
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('index');
});
Route::get('/contact-us', function () {
return view('contactus');
});
Route::get('/tours', function () {
return view('tours');
});
// Admin panel Pages Routes
Route::get('/admin', function () {
return view('admin/index');
});
Route::get('/admin/bannercustomize', function () {
return view('admin/layout/bannercustomize');
});
// Controller routes
// Route::resource('banners', 'BannerController#index');
// // Route::get('/', 'BannerController#index');
Route::post('store/banner','BannerController#store')->name('store.banner');
Route::get('/admin','BannerController#index')->name('admin.index');
Here is my controller BannerController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Banner;
class BannerController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// $banners = Banner::all()->toArrary();
// return view('admin.index', ['banners' =>$banners]));
$banners = Banner::all();
return view('admin.index',compact('banners'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.layout.bannercustomize');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'h4_title' => 'required',
'h2_title' => 'required',
'banner_paragraph' => 'required',
]);
$banner = new Banner([
'h4_title' => $request->get('h4_title'),
'h2_title' => $request->get('h2_title'),
'banner_paragraph' => $request->get('banner_paragraph'),
]);
if ($request->has('banner_image')) {
$image = $request->file('banner_image');
$name = hexdec(uniqid()).'_'.time();
$folder = '/image/banner_image/';
$filePath = $folder . $name. '.' . $image->getClientOriginalExtension();
$this->uploadOne($image, $folder, 'public', $name);
$banner->banner_image = $filePath;
}
$banner->save();
return redirect()->back()->with('success', 'Data Added');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Please help me to find the error & the solution of this error
You have to change
banner_image
to
banner_photo
Change
if ($request->has('banner_photo')) {
$image = $request->file('banner_photo');
....
}

Condition in my view

I have a another problem in my view.
This condition #if doesn't work:
#if( ! empty($news))
<div class="container">
<div class="row justify-content-center">
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
</div>
</div>
#else
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Last News :</h1>
<div class="col-md-8">
<div class="row">
#foreach($news as $new)
<div class="card" style="width: 18rem; margin-left:30px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
#if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
#else
{{$new['content']}}
#endif
</p>
More.
#if(#admin)
Edit</td>
<form action="{{action('NewsController#destroy', $new['id'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
#endif
</div>
</div>
#endforeach
</div>
</div>
#endif
I want to show Any News ! when I don't have news and Last News ! when I have news
Can you help me?
Additional Info :
Model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
//
}
Controller :
<?php
namespace App\Http\Controllers;
use App\News;
use Illuminate\Http\Request;
class NewsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$news=News::all();
return view('home',compact('news'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('news.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$news = new News();
$news->title=$request->get('title');
$news->content=$request->get('content');
$news->picture=$request->get('picture');
$news->save();
return redirect('news')->with('success', 'Les Informations ont bien été ajoutées.');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$news = News::findOrFail($id);
return view('news');
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$news = News::find($id);
return view('news.edit',compact('news','id'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$news= News::find($id);
$news->title=$request->get('title');
$news->content=$request->get('content');
$news->picture=$request->get('picture');
$news->save();
return redirect('news');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$news = News::find($id);
$news->delete();
return redirect('news')->with('success','La News à bien été supprimée.');
}
}
View :
#extends('layouts.app')
#section('content')
#if(empty($news))
{{--<div class="col-md-12">--}}
{{--<div class="row">--}}
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
{{--</div>--}}
{{--</div>--}}
#else
<h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
<div class="col-md-12">
<div class="row">
#foreach($news->sortByDesc('created_at') as $new)
<div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
#if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
#else
{{$new['content']}}
#endif
</p>
More.
#admin
Editer
<form action="{{action('NewsController#destroy', $new['id'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
#endadmin
</div>
</div>
#endforeach
</div>
</div>
#endif
#endsection
Routes :
<?php
/*
* Route Resource
*/
Route::resource('news', 'NewsController');
/*Route HOME*/
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::get('/news', 'NewsController#index')->name('news');
You can try:
#if($news->count())
//enter your code here if you have news
<h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
<div class="col-md-12">
<div class="row">
#foreach($news->sortByDesc('created_at') as $new)
<div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
#if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
#else
{{$new['content']}}
#endif
</p>
More.
#admin
Editer
<form action="{{action('NewsController#destroy', $new['id'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
#endadmin
</div>
</div>
#endforeach
</div>
</div>
#else
//enter your code here if you don't have news
{{--<div class="col-md-12">--}}
{{--<div class="row">--}}
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
{{--</div>--}}
{{--</div>--}}
#endif
I hope it works for you

Categories