Laravel 8 Components - laravel

I am trying to create a component, and send it some data.
<div class="card">
<div class="card-wrapper">
<div class="card-header">
<div class="card-title">
<i class="fa fa-pie-{{ $icon ?? '' }} fa-fw"></i>
<span>{{ $title ?? '' }}</span>
</div>
<div class="card-version">
<i class="fa fa-question-circle fa-fw"></i>
</div>
</div>
<div class="card-body">
{{ $content ?? '' }}
</div>
</div>
</div>
I am then calling that component in Blade using
<x-com-card icon="globe" title="Test"> </x-com-card>
But I am getting the following error
Undefined variable $icon
My component controller looks like this
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class ComCard extends Component
{
private $icon;
private $title;
/**
* Create a new component instance.
*
* #param $icon
* #param $title
*/
public function __construct($icon, $title)
{
$this->icon = $icon;
$this->title = $title;
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('com.card');
}
}
Any idea why this is happening?

It is because you set your properties as private, and view can not access them.
When your component is rendered, you may display the contents of your
component's public variables by echoing the variables by name
public $icon;
public $title;
More you can find here, in official documentation

Related

Laravel 9. Blade components

is it possible to pass collections to a component like this:
class projectAccordion extends Component
{
/**
* Create a new component instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
$projects = Project::with('subservice', 'partner')->orderByDesc('id')->limit(4)->get();
return view('components.front.project-accordion', compact('projects'));
}
}
the component itself looks like this:
#foreach($projects as $project)
<div class="info-list-wrapper">
<div class="row">
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-title">
<div class="date">
<span>{{ date('d F, Y', strtotime($project->created_at)) }}</span>
</div>
<h4>
{{ $project->title }}
</h4>
</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-txt">
<p>
{{ $truncated = Str::of($project->descr)->limit(100) }}
</p>
</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-img">
<img src="{{ $project->partner->file_url }}" alt="">
</div>
</div>
</div>
</div>
#endforeach
this is how i call the component in homeage.blade.php
<x-front.project-accordion></x-front.project-accordion>
This method does not work on hosting, but everything works on the local version.Is this the right approach?

Laravel 8: Attempt to read property "id" on null

I am facing this error 'Attempt to read property "id" on null' in Laravel 8. It was working fine before but in my view I changed $user->id to $user->profile->id and now this is happening. I am logged in the app and I have changed my route accordingly to match profile id, I have also tried clearing cache etc.
Here is my Code:
User Model:
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var string[]
*/
// protected $fillable = [
// 'name',
// 'email',
// 'password',
// ];
protected $table = 'users';
protected $guarded = [];
/**
* The attributes that should be hidden for serialization.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
public function posts ()
{
return $this->hasMany(Post::class);
}
public function profile()
{
return $this->hasOne(Profile::class);
}
}
Profile Model:
class Profile extends Model
{
use HasFactory;
protected $table = 'profiles';
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
}
ProfilesController:
class ProfilesController extends Controller
{
public function show(User $user)
{
return view ('profiles.index', compact('user'));
}
public function edit(User $user)
{
return view ('profiles.index', compact('user'));
}
}
Route:
Route::get('profile/{profile}', [ProfilesController::class, 'show'])->middleware('auth');
Route::get('profile/{profile}/edit', [ProfilesController::class, 'edit'])->middleware('auth');
View:
<x-layout>
<section class="py-8 max-w-4xl mx-auto">
<h1 class="text-lg font-bold mb-8 pb-2 border-b">
#if ($user->id == auth()->user()->id)
Hello {{ $user->name }}, welcome to your profile.
#else
{{ $user->name }}'s Profile.
#endif
</h1>
<div class="flex">
<aside class="w-48 flex-shrink-0">
<h4 class="font-semibold mb-4">
Navigation
</h4>
<ul style="max-width: 75%">
<li>
View Profile
</li>
<li>
#if ($user->id == auth()->user()->id)
Edit Profile
#endif
</li>
</ul>
</aside>
<main class="flex-1">
<x-panel>
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
<div class="flex flex-row grid-cols-12">
<div class="flex flex-col col-span-4 row-span-full justify-items-start flex-grow-0 flex-shrink-0 grid-cols-4">
<div class="flex flex-row">
<img src="http://i.pravatar.cc/60?u={{ $user->profile->id }}" alt="" width="" height="" class="rounded-full h-24 w-24 flex m-2">
</div>
</div>
<div class="flex flex-col col-span-8 justify-right grid-cols-8 text-sm">
<div class="flex flex-row">
<div class="flex flex-col col-span-2 font-semibold">
<div class="pt-3">Name:</div>
<div class="pt-3">Email:</div>
<div class="pt-3">About:</div>
</div>
<div class="flex flex-col col-span-10">
<div class="pt-3 pl-4 text-justify">{{ $user->profile->name }}</div>
<div class="pt-3 pl-4 text-justify">{{ $user->profile->email }}</div>
<div class="pt-3 pl-4 text-justify"><p>{{ $user->profile->description }}</p></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</x-panel>
</main>
</div>
</section>
I think the problem is that you are passing a Profile model as indicated by your routes, but the method is looking for a User model.
Show and Edit have a User Model as a parameter. If you pass the Profile id, the method is gonna find the User model by id but with the id of the Profile model and not the user_id of the Profile model.
You will need to change the methods to:
public function show(Profile $profile)
{
$user = $profile->user;
return view ('profiles.index', compact('user'));
}
public function edit(Profile $profile)
{
$user = $profile->user;
return view ('profiles.index', compact('user'));
}
With this code the Profile model is found and via the relationship the User is obtained and passed to the view.
public function edit($id)
{
$table = Table::find($id);
return view('tables.edit', compact('table'));
}
public function update(Request $request, $id)
{
$table = Table::find($id);
$table->update($request->all());
return redirect('/tables');
}

Is there any way to get an id in a create method in laravel

I am trying to pass a foreign directly to my create method
In the project I am working on, I have two different users. The first one is a farm who can create an animal
The second one is the clinic who can attach to an animal some clinic details showing if the animal was vaccinated or not. In my clinic details table, I have the animal as a foreign key but I do not know how I will pass that key to the create method
Here is my Clinic controller
<?php
namespace App\Http\Controllers;
use App\Animal;
use App\Clinic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ClinicController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user = Auth::user();
$animal = Animal::all();
return view('clinic.index', compact('user', 'animal'));
}
/**
* Show the form for creating a new resource.
*
* #param $id
* #return void
*/
public function create($id)
{
$user = Auth::user();
$animal = Animal::query()->findOrFail($id);
$clinic = new Clinic();
return view('clinic/create', compact('user', 'animal', 'clinic'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$animal = Animal::query()->findOrFail($id);
return view('clinic.show', compact('animal'));
}
/**
* 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)
{
//
}
}
My clinic index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header"><h3>Clinic Details Dashboard</h3></div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
You are logged in! {{ $user->name}}
<hr>
<center><h3>Animals</h3></center>
<hr>
#foreach($animal as $animal)
<div class="row">
<div class="col-2">{{ $animal->id }}</div>
<div class="col-4">{{ $animal->type->category }}</div>
<div class="col-2">{{ $animal->user->name }}</div>
<div class="col-4">{{ $animal->created_at }}</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
my clinic show.blade.php
This is where I would like to pass the animal id to the create method but I do not know how.
Even my button link is not going to the create view
#extends('layouts.app')
#section('title', 'Details for animal ')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<center><h3>Details for the animal</h3></center>
</div>
<div class="card-body">
<div class="col-12">
<p><strong>Id: </strong>{{ $animal->id }}</p>
<p><strong>Animal: </strong>{{ $animal->type->category }}</p>
<p><strong>Farm: </strong>{{ $animal->user->name }}</p>
<p><strong>Gender: </strong>{{ $animal->gender }}</p>
<p><strong>Place Of Birth: </strong>{{ $animal->placeOfBirth }}</p>
<p><strong>Date: </strong>{{ $animal->created_at }}</p>
</div>
<div class="col-md-8 offset-md-4">
<a href="create">
<button type="button" class="btn btn-primary">
Attach Clinic Detail
</button>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
clinic create.blade.php
This view is still somehow empty
#extends('layouts.app')
#section('title', 'Add Clinical Details')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">New Animal</div>
<div class="card-body">
<form action="{{ url('clinic') }}" method="POST">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
You could simply add a parameter to your method like this:
public function create($id, $foreignKeyId) {
// ... to something
}
For that you would need to pass id as a hidden input.
You could also try to create a relation, that way you do not need to add the foreignKey as a parameter. You could simply access the foreign_key using the specified ORM Model.
You can use insertGetId() method for retrieving last created id after store method.
https://laravel.com/api/5.0/Illuminate/Database/Query/Builder.html#method_insertGetId
$id = DB::table('posts')->insertGetId(
array('title' => 'my post title', 'body' => 'my post body')
);

Laravel page not found at create function

Recently I started programming using Laravel as a framework. Everything goes fine, but I tried to write a create script with the following message at runtime:
Sorry, the page you are looking for could not be found.
That's after posting Accept in the Create form, I already have created Index, Show, Update and delete functions successfully. So the route to my controller is correct, the file exist ... I'm totally stuck and can not see what incorrect.
Please help.
I didn't redirect the public folder so I'm still using the full path with no problem at the other modules (/gymmgr/public/lockers).
I send you the code:
index.blade.php
#extends('layouts.app')
#section('content')
#guest
#else
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Lockers</a>
</nav>
<div class="container-fluid">
<div class="row">
<nav class="col-sm-1 d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Acciones</span>
<a class="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle"></span>
</a>
</h6>
<ul class="nav flex-column">
<li class="nav-item">
<span data-feather="home"></span>
<span class="sr-only"></span>
</li>
<li class="nav-item">
<a class="nav-link" href="/gymmgr/public/lockers/create">
<span data-feather="file"></span>
Nuevo
</a>
</ul>
</div>
</nav>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
<h1 class="h5">Catálogo</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group mr-2">
<button class="btn btn-sm btn-outline-secondary">Exportar</button>
</div>
</div>
</div>
<table class="table">
<thead class="thead-light">
<tr>
<th>Clave del locker</th>
<th>Ubicación</th>
</tr>
</thead>
<tbody>
#foreach($lockers as $locker)
<tr>
<td> {{ $locker->strClaveLocker }} ></td>
<td>{{ $locker->strUbicacion }}</td>
</tr>
#endforeach
</tbody>
</table>
</main>
</div>
</div>
#endguest
#endsection
LockersController.php
<?php
namespace App\Http\Controllers;
use App\Locker;
use Illuminate\Http\Request;
class LockersController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$lockers = Locker::all();
return view('lockers.index', ['lockers'=>$lockers]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
return view('lockers.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
/*
if (Auth::check())
{*/
$locker = Locker::create(
['strClaveLocker'=>$request->input('strClaveLocker'),
'strUbicacion'=>$request->input('strUbicacion')
]
);
if($locker)
{
return redirect()->route('lockers.index')->with('success','Locker creado con éxito');
}
// }*/
}
/**
* Display the specified resource.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function show(Locker $locker)
{
//
$locker = Locker::find($locker->idLocker);
//$locker = Locker::where('idLocker', $locker->idLocker)->first();
//echo e($locker->idLocker);
return view('lockers.show', ['locker'=>$locker]);
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function edit(Locker $locker)
{
//
$locker = Locker::find($locker->idLocker);
if ($locker)
{
return view('lockers.edit', ['locker'=>$locker])->with('success', 'Locker encontrado');
};
return view('lockers.edit', ['locker'=>$locker]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Locker $locker)
{
//
$lockerUpdate = Locker::where('idLocker', $locker->idLocker)->update([
'strClaveLocker'=>$request->input('strClaveLocker'),
'strUbicacion'=>$request->input('strUbicacion')]);
if ($lockerUpdate)
{
return redirect()->route('lockers.show',['lockers'=>$locker->idLocker])
->with('success', 'Locker actualizado correctamente');
}
return back()->withInput();
}
/**
* Remove the specified resource from storage.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function destroy(Locker $locker)
{
//
$findLocker = Locker::find($locker->idLocker);
if($findLocker->delete())
{
return redirect()->route('lockers.index')
->with('success','Locker borrado exitosamente');
}
return back()->withInput()->with('error','El locker no pudo borrarse');
}
}
create.blade.php
#extends('layouts.app')
#section('content')
#guest
#else
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Locker') }}</div>
<div class="card-body">
</form>
<form method="post" action="{{ route('lockers.create') }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="put">
<div class="form-group">
<label for="locker-clave">Clave del locker<span class="required">*</span></label>
<input placeholder="Clave del locker"
id="locker-clave"
required
name="strClaveLocker"
spellcheck="false"
class="form-control"
/>
</div>
<div class="form-group">
<label for="locker-ubicacion">Ubicación del locker</label>
<input placeholder="Ubicación del locker"
id="locker-ubicacion"
required
name="strUbicacion"
class="form-control"
/>
</div>
<div class="form-group">
<input type="submit"
class="btn btn-primary"
value="Aceptar"
/>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endguest
#endsection
The route
web.php
/*
|--------------------------------------------------------------------------
| 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('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('lockers','LockersController');
Your create form has:
<form method="post" action="{{ route('lockers.create') }}">
...
<input type="hidden" name="_method" value="put">
So you are sending a PUT request to your create route. As the docs describe, the .create route the resource declaration sets up is expecting a GET (check the table of actions it sets up). So that request will fail with a 404 as there is no matching route set up.
Also, though confusingly named, the create route is for displaying the create form, not saving data you've entered into that form. So you should not be submitting data to your create route.
You need to:
Change your form action to point to your store route: ... action="{{ route('lockers.store') }}
Remove the form method spoofing, store expects POST which is what you'll get by default without any spoofing.

How to retrive data from DB and display it in a responding navigation menu?

I am using laravel 5.3
I have a form that helps me create new projects. I can also display all the projects I have in DB with all the attributes.
Now, I need to display responding navigation menu in the left on the page that contains the following :
Domaines :
Domaine 1 :
project 1
project 2
Domaine 2 :
project A
project B
I want that once I click on project there's table that display all the information of that project.
this is "index.blade.php" when I only was trying to display the projects without any ordre :
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Projects CRUD</h2>
</div>
<div class="pull-right">
#permission('pro-create')
<a class="btn btn-success" href="{{ route('pros.create') }}"> Create New Project</a>
#endpermission
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>title</th>
<th>code</th>
<th>domain_id</th>
<th width="280px">Action</th>
</tr>
#foreach ($pros as $key => $pro)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $pro->title }}</td>
<td>{{ $pro->code }}</td>
<td>{{ $pro->domain_id}}</td>
<td>
<a class="btn btn-info" href="{{ route('pros.show',$pro->id) }}">Show</a>
#permission('pro-edit')
<a class="btn btn-primary" href="{{ route('pros.edit',$pro->id) }}">Edit</a>
#endpermission
#permission('pro-delete')
{!! Form::open(['method' => 'DELETE','route' => ['pros.destroy', $pro->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
#endpermission
</td>
</tr>
#endforeach
</table>
{!! $pros->render() !!}
#endsection
and this is what I was trying to add to have the "responding navigation menu" :
<div class="nav-side-menu">
<div class="brand">Menu</div>
<i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
<div class="menu-list">
<ul id="menu-content" class="menu-content collapse out">
<li>
<a href="#">
<i class="fa fa-dashboard fa-lg"></i> Dashboard
</a>
</li>
<li data-toggle="collapse" data-target="#domains" class="collapsed active">
<i class="fa fa-gift fa-lg"></i> Domains <span class="arrow"></span>
</li>
<ul class="sub-menu collapse" id="domains">
<li class="active">Domain 1</li>
<li>Domain 2</li>
<li>Domain 3</li>
<li>Domain 4</li>
<li>Domain 5</li>
</ul>
<li data-toggle="collapse" data-target="#responsible" class="collapsed">
<i class="fa fa-globe fa-lg"></i> Responsibles <span class="arrow"></span>
</li>
<ul class="sub-menu collapse" id="responsible">
<li>Mr.name1</li>
<li>Mr.name2</li>
<li>Mr.name3</li>
</ul>
</div>
</div>
My question is how to build a relation between my DB and the view? how can I retrieve data from DB (projects table) and display it in the view ?
You create a model for your table "Example"
Model
class Example extends Model {
protected $table = 'Example';
protected $fillable = ['column1', 'column2'];
protected $primaryKey = 'id';
}
Don't forget to specify your connection in /config/database.php
Now you have your model you can create a Controller, I would suggest a resource controller.
use App\Model\Example;
class ExampleController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
// This is the one we need, because it will be shown on GET domain.com/example
$examples = Example::query()->get();
return view('exampleview', ['passed_data' => $examples]);
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
Define your route. Because you want to display frontend you choose routes/web.php and define
Route::resource('example', 'ExampleController');
Now you also have to make your blade under resources\views called exampleview.blade.php (as defined in the controller)
there you can use the passed data in blade syntax:
exampleview.blade.php
#foreach ($passed_data as $single_example)
{{ $example }}
#endforeach
To make your menu change based on the view you are in you should define a
#stack('menustack') inside your menu blade
and push new element from the "lower" blade with
#push('menustack')
<div> new menu div </div>
#endpush
You need to create nav.php in app/Http directory like:
app/Http/nav.php
<?php
//pass your view file path like *index*
View::composer('index', function($view){
//For navigation menu items...
$navigation = DB::table('navigation')
->select('Domain1', 'Domain2', 'Domain3', 'Domain4', 'Domain5')
->get();
return $view->with('navigations' => $navigations);
});
?>
And define nav.php route in routes/web like:
// For navigation menu route
**require 'nav.php';**
Hope this works for you!

Resources