Livewire x-slot tag in component - php

I search to use x-slot in Livewire component to populate my sidebar with wire:model form inputs.
My edit.blade.php file:
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Header') }}
</h2>
</x-slot>
<div>{{ $sidebar }}</div>
<div class="bg-white">
<livewire:editor-component :post="$post" />
</div>
</x-app-layout>
My Livewire component:
<div>
<x-slot name="sidebar">
<div class="bg-gray-100">
Some text... Some inputs...
</div>
</x-slot>
<div class="p-5">
<livewire:other-component />
</div>
</div>
And my component:
<?php
namespace App\Http\Livewire;
use App\Models\Post;
use Livewire\Component;
class EditorComponent extends Component
{
/** #var Post */
public $post;
}
I want access to $post in sidebar x-slot and I should be able to write in input and manage values of component from sidebar.

You can access $post inside your Livewire view directly as it is a public property.
As example you can output the id of your $post, given that there is an id property on your $post, like this:
<div>
<x-slot name="sidebar">
<div class="bg-gray-100">
Some text... Some inputs...
</div>
</x-slot>
Post id: {{ $post->id }}
<div class="p-5">
<livewire:other-component />
</div>
</div>
How to access properties is written down in the Livewire docs
Access properties across components, can be done by utilizing Livewire Events

Related

SOLVED! Call to undefined method App\Category::posts() Laravel

i want to create some CMS project with the laravel, when the user trying to click the categories, the app will show the post that only shows the category that user clicked. When the user click the categories, the app says error Bad Method Call. What should i do? Any help will be appreciated.
Here's the code from web.php
<?php
use App\Http\Controllers\Blog\PostsController;
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('/', 'WelcomeController#index')->name('welcome');
Route::get('blog/posts/{post}', [PostsController::class, 'show'])->name('blog.show');
Route::get('blog/categories/{category}', [PostsController::class, 'category'])->name('blog.category');
Route::get('blog/tags/{tag}', [PostsController::class, 'tag'])->name('blog.tag');
Auth::routes();
Route::middleware(['auth'])->group(function () {
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('categories', 'CategoriesController');
Route::resource('posts','PostsController');
Route::resource('tags','TagsController');
Route::get('trashed-posts','PostsController#trashed')->name('trashed-posts.index');
Route::put('restore-post/{post}', 'PostsController#restore')->name('restore-posts');
});
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('users/profile', 'UserController#edit')->name('users.edit-profile');
Route::put('users/profile', 'UserController#update')->name('users.update-profile');
Route::get('users','UserController#index')->name('users.index');
Route::post('users/{user}/make-admin', 'UserController#makeAdmin')->name('users.make-admin');
});
Here's the code from PostsController.php
<?php
namespace App\Http\Controllers\Blog;
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
use App\Tag;
use App\Category;
class PostsController extends Controller
{
public function show(Post $post) {
return view('blog.show')->with('post', $post);
}
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->posts()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
public function tag(Tag $tag) {
return view('blog.tag')
->with('tag', $tag)
->with('posts', $tag->posts()->simplePaginate(3));
}
}
Here's the code from App\Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function post() {
return $this->hasMany(Post::class);
}
}
Here's the code from category.blade.php
#extends('layouts.blog')
#section('title')
Category {{ $category->name }}
#endsection
#section('header')
<header class="header text-center text-white" style="background-image: linear-gradient(-225deg, #5D9FFF 0%, #B8DCFF 48%, #6BBBFF 100%);">
<div class="container">
<div class="row">
<div class="col-md-8 mx-auto">
<h1>{{ $category->name }}</h1>
<p class="lead-2 opacity-90 mt-6">Read and get updated on how we progress</p>
</div>
</div>
</div>
</header><!-- /.header -->
#endsection
#section('content')
<main class="main-content">
<div class="section bg-gray">
<div class="container">
<div class="row">
<div class="col-md-8 col-xl-9">
<div class="row gap-y">
#forelse($posts as $post)
<div class="col-md-6">
<div class="card border hover-shadow-6 mb-6 d-block">
<img class="card-img-top" src="{{ asset($post->image) }}" alt="Card image cap">
<div class="p-6 text-center">
<p>
<a class="small-5 text-lighter text-uppercase ls-2 fw-400" href="#"></a>
{{ $post->category->name }}
</p>
<h5 class="mb-0">
<a class="text-dark" href="{{ route('blog.show',$post->id) }}">
{{ $post->title }}
</a>
</h5>
</div>
</div>
</div>
#empty
<p class="text-center">
No Results found for query <strong>{{ request()->query('search') }} </strong>
</p>
#endforelse
</div>
{{-- <nav class="flexbox mt-30">
<a class="btn btn-white disabled"><i class="ti-arrow-left fs-9 mr-4"></i> Newer</a>
<a class="btn btn-white" href="#">Older <i class="ti-arrow-right fs-9 ml-4"></i></a>
</nav> --}}
{{ $posts->appends(['search' => request()->query('search') ])->links() }}
</div>
#include('partials.sidebar')
</div>
</div>
</div>
</main>
#endsection
If you guys didn't see which file that i don't show up, just ask,.
If you guys see any problems that confusing, i can use teamviewer.
Any Help will be appreciated.
Thank you.
The error told that you don't have posts. Indeed it is. What you have is post() method on Category class, as evidently seen in this line:
public function post() {
You should change this ->with('posts', $category->posts()->simplePaginate()) to be:
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->post()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
In your model, you defined a post relationship whereas you use a posts relationship then, they're not the same one. You should replace post by posts because it's a to-many relationship.
Your relationship is post and you have multiple ->with('posts', $category->posts()->simplePaginate()). Can you change those to ->with('post') and try again?

htmlspecialchars() expects parameter 1 to be string in Laravel 5.8

I'm setting up a map, and want to display the coordinates. What do I need to do?
Blade
#foreach($myArgans as $argan)
<div class="col-lg-12">
<h1>{{ $argan->titre }}</h2>
<img class="card-img-top" src="{{ asset('storage/'.$argan->image) }}">
<div style="text-align: justify">
{{ $argan->body }}
</div>
<div>
{{ $argan->getCoordinates() }}
</div>
</div>
#endforeach
Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Spatial;
class Argan extends Model
{
use Spatial;
protected $spatial = ['point'];
}

How to bind a route url for form tag in laravel

This is my form tag
<form method="POST" action="{{ url("/post/{$article->id}/comment") }}">>
This is my route
Route::post('/post/{article}/comment', 'CommentController#store');
This is my commentcontroller method
public function store(Article $article)
{
$comment = $article->comment->create([
'body' => request('body'),
]);
return back();
}
show.blade.php
`#extends('master')
#section('content')
<!-- Example row of columns -->
<div class="container">
<h1> {{ $articles->title }} </h1>
<p> {{ $articles->body }} </p>
<hr></hr>
<div class="comment">
<ul class="list-group">
#foreach($articles->comments as $comment)
<li class="list-group-item">
<strong>
{{ $comment->created_at->diffForHumans()}} :
</strong>
{{ $comment->body }}
</li>
#endforeach
</ul>
</div>
<!-- Here is comment form -->
<hr>
<div class="card">
<div class="card-block">
<form method="POST" action="{{ url ("/post/{$article->id}/comment") }}">>
{{ csrf_field() }}
<div class="form-group">
<textarea name="body" placeholder="your comment here." class="form-control"> </textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Comment</button>
</div>
</form>
</div>
#include('partials.errors')
</div>
</div>
#endsection`
When I'm trying to add comment on article I'm getting an error like this:
> Undefined variable: article (View: C:\xampp7\htdocs\Lara\resources\views\article\show.blade.php)
Anything wrong in this? help me out. Thanks in advance
It seems you are not passing $article to view. You haven't included the code but you have somewhere something like this:
return view('article.show');
and instead you should have:
return view('article.show', compact('article'));
so finally your show method in controller should look something like this:
public function show(Article $article)
{
return view('article.show', compact('article'));
}
Its because you are calling your article $articles (plural) when in fact it is a single article.
in your controller change $articles to $article and then change it in the view where you have articles, for instance $articles->body. It will then be correct when you use it in the form action.

Symfony2 accessing values in base html

I have a dynamic menu and in the top right corner I want to dinamically display the number of products I have in my cart. All my twigs extend base.html.twig in which I have this dynamic menu.
<div id="mainBody" class="container">
<header id="header">
<div class="row">
<div class="span12">
<img src="{{ asset('bundles/mpshop/img/logo_title.png') }}" alt="Bootsshop"/>
<div class="pull-right"> <br/>
<span class="btn btn-mini btn-warning"> <i class="icon-shopping-cart icon-white"></i> [ {{ cart|length }} ] </span>
<span class="btn btn-mini active">$155.00</span>
<span class="btn btn-mini">£</span>
<span class="btn btn-mini">€</span>
</div>
</div>
</div>
However when I add {{ cart|length }} I am getting variable cart does not exist. Does base.html.twig has its own controller? How to give the base twig the cart with session so it could see my array?
One way to do this is create a helper class and include a method from that class in the menu (navbar?)
To do this you can make a regular Controller called for example HelperController.php then create a method within that new controller, like shownumberofproductsAction and then create a corresponding view file: shownumberofproducts.html.twig
Then in your top right menu simply call:
{% render controller("MySomethingBundle:Helper:shownumberofproducts") %}

symfony, twig - default filter for all variables in template

Let's say I want to display a larger data set and it is possible that there are many null / empty values. I don't want to define default filter for every variable.
Is there a way to display a default value for all (empty, null) variables in a template (like: "not specified").
EDIT:
twig:
{# src/KuMiV/EmployeeBundle/Resources/views/Other/detailItem.html.twig #}
{% extends "base.html.twig" %}
...
{% block content %}
<h3 class="sub-header">Personenbezogene Angaben</h3>
<div class="row">
<div class="col-md-3">
<label> ID </label><p>{{ employee.id }} </p>
</div>
<div class="col-md-3">
<label> Titel </label><p>{{ employee.title|default('keine Angabe') }} </p>
</div>
<div class="col-md-3">
<label> Vorname </label><p>{{ employee.firstName }} </p>
</div>
<div class="col-md-3">
<label> Nachname </label><p>{{ employee.lastName }} </p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<label> Geschlecht </label><p>{{ employee.gender }} </p>
</div>
<div class="col-md-3">
<label> Email </label><p>{{ employee.email|default('keine Angabe') }} </p>
</div>
<div class="col-md-3">
<label> Telefon 1</label><p>{{ employee.phone1|default('keine Angabe') }} </p>
</div>
<div class="col-md-3">
<label> Telefon 2 </label><p>{{ employee.phone2|default('keine Angabe') }} </p>
</div>
</div>
...
{% endblock content %}
Controller:
class DefaultController extends Controller
{
public function detailAction($id)
{
$employee = $this->getDoctrine("Employee")
->getRepository("EmployeeBundle:Employee")
->find($id);
return $this->render('EmployeeBundle:Other:detail.html.twig', array(
'employee' => $employee,
));
}
}
You may need to create a custom twig extension for this.
namespace Employee\EmployeeBundle\Twig;
class EmployeeExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
new \Twig_SimpleFilter('setDefaults', array($this, 'setDefaultFilter')),
);
}
public function setDefaultFilter($employee)
{
$employee.title = !empty($employee.title) ? $employee.title : 'keine Angabe';
$employee.email = !empty($employee.email) ? $employee.email : 'keine Angabe';
//... So on; continue for others
return $employee;
}
public function getName()
{
return 'employee_extension';
}
}
Keep in mind to register the extension as a service:
services:
employee.twig.employee_extension:
class: Employee\EmployeeBundle\Twig\EmployeeExtension
tags:
- { name: twig.extension }
Now you can use it in your twig file as:
{% set employee = employee|setDefaults %}
Still I believe the default filter in twig is better.

Categories