Sorting and searching a foreign entity field on a Laravel Livewire table - laravel

I’ve just implemented a Livewire data table based on the Caleb’s tutorial on Laracasts. Everything works as expected when working with just one model. However I’m stuck trying to apply sorting and searching to a foreign model field.
User model:
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get the user's email verified status.
*
* #param string $value
* #return string
*/
public function getEmailVerifiedAtAttribute($value)
{
if ($value == null) {
return "No";
}
return $value;
}
public function getRoleAttribute()
{
if($this->roles()->count() > 0) {
return $this->roles()->first()->name;
} else {
return "Usuario registrado";
}
}
public static function search($query)
{
return empty($query) ? static::query()
: static::where('name', 'like', '%'.$query.'%')
->orWhere('email', 'like', '%'.$query.'%');
}
}
Tried adding another orWhere() clause to the search() method in some ways. None worked. Now I left just the default ones.
Livewire controller:
namespace App\Http\Livewire\Users;
use Livewire\Component;
use Livewire\WithPagination;
class Table extends Component
{
use WithPagination;
public $perPage;
public $sortField;
public $sortAsc;
public $search;
public function mount()
{
$this->perPage = 10;
$this->sortField = 'name';
$this->sortAsc = true;
$this->search = '';
}
public function sortBy($field)
{
if ($this->sortField === $field) {
$this->sortAsc = ! $this->sortAsc;
} else {
$this->sortAsc = true;
}
$this->sortField = $field;
}
public function updatingPerPage()
{
$this->resetPage();
}
public function render()
{
return view('livewire.users.table', [
'users' => \App\User::search($this->search)
->with('roles')
->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
->paginate($this->perPage),
]);
}
}
Livewire view:
<div>
<div class="row mb-4">
<div class="col form-inline">
Mostrar
<select wire:model="perPage" class="form-control form-control-sm custom-select custom-select-sm">
<option>10</option>
<option>100</option>
<option>1000</option>
</select>
registros
</div>
<div class="col-sm-3">
<input wire:model="search" class="form-control form-control-sm" type="text" placeholder="Buscar usuarios...">
</div>
</div>
<div class="table-responsive mb-4" >
<div class="table-header">
<table class="table table-sm text-nowrap" role="grid">
<thead>
<tr>
<th width="30%">
<a wire:click.prevent="sortBy('name')" role="button" href="#">
Nombre
#include('partials._sort-icon', ['field' => 'name'])
</a>
</th>
<th width="30%">
<a wire:click.prevent="sortBy('email')" role="button" href="#">
Correo electrónico
#include('partials._sort-icon', ['field' => 'email'])
</a>
</th>
<th width="30%">
<a wire:click.prevent="sortBy('')" role="button" href="#">
Rol
#include('partials._sort-icon', ['field' => ''])
</a>
</th>
<th></th>
</tr>
</thead>
</table>
</div>
<div class="table-body">
<table class="table table-sm table-hover text-nowrap" role="grid">
<tbody>
#foreach ($users as $user)
<tr>
<td width="30%">{{ $user->name }}</td>
<td width="30%">{{ $user->email }}</td>
<td width="30%">{{ $user->role }}</td>
<td>
<form method="POST" action="{!! route('backend.users.user.destroy', $user->id) !!}" accept-charset="UTF-8">
<input name="_method" value="DELETE" type="hidden">
{{ csrf_field() }}
<div class="btn-group btn-group-xs float-right" role="group">
#can('users.show')
<a href="{{ route('backend.users.user.show', $user->id ) }}" class="btn btn-outline-default btn-xs" title="{{ trans('users.show') }}">
<i class=" fas fa-fw fa-eye" aria-hidden="true"></i>
</a>
#endcan
#can('users.edit')
<a href="{{ route('backend.users.user.edit', $user->id ) }}" class="btn btn-outline-default btn-xs" title="{{ trans('users.edit') }}">
<i class=" fas fa-fw fa-pencil-alt" aria-hidden="true"></i>
</a>
#endcan
#can('users.destroy')
<button type="submit" class="btn btn-outline-default btn-xs" title="{{ trans('users.delete') }}" onclick="return confirm("{{ trans('users.confirm_delete') }}")">
<i class=" fas fa-fw fa-trash-alt" aria-hidden="true"></i>
</button>
#endcan
</div>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
<div class="table-footer">
<div class="text-muted">
Showing {{ $users->firstItem() }} to {{ $users->lastItem() }} out of {{ $users->total() }} results
</div>
<div>
{{ $users->links() }}
</div>
</div>
</div>
Tried also some ways to pass an argument to the sortBy() and [‘field’ => ‘’] on the role column. Now I left them with empty strings.
I know, this issue probably is more related to Laravel than Livewire, but I’ll really appreciate any help.
Update:
Solved using the Laravel Query Builder instead of Eloquent.

Related

Pre-Populating Parent Child Form from DB

I have tried prepopulating the forms from DB records, not happening for child form, I have use all conventions for Laravel table, pivot table etc., still nothing. The array display with the dd('$allTariffs') but doesn't show on view file, please help am stuck with
ErrorException
foreach() argument must be of type array|object, null given
Edit Component Code
<?php
namespace App\Http\Livewire\Admin;
use App\Models\Products;
use App\Models\Tariffs;
use Livewire\Component;
class AdminEditTariffsComponent extends Component
{
// public $productId;
public $tariffId;
public $allTariffs = [];
public $rowProducts = [];
public $tariffName;
public function mount ($tariffId)
{
$this->rowProducts = Products::all();
$tariff = Tariffs::where('id', $tariffId)->first();
$this->tariffName = $tariff->tariff_name;
// $allTariffs = $tariff->products()->where('tariffs_id', $tariffId)->get()->toArray();
$allTariffs = $tariff->products->find($tariffId);
// var_dump($allTariffs) ;
// dd( $allTariffs);
foreach ($allTariffs->products as $product)
{
['productId' => $product->pivot->products_id, 'basicCharge' => $product->pivot->basic_charge, 'additionalCharge' => $product->pivot->additional_charge];
}
}
public function updateStatus()
{
$tariff = Tariffs::find($this->tariffId);
$tariff->tariff_name = $this->tariffName;
$tariff->created_by = auth()->user()->id;
$tariff->save();
foreach ($this->allTariffs as $product) {
$tariff->products()->sync($product['productId'],
['basic_charge' => $product['basicCharge'],
'additional_charge' => $product['additionalCharge']]);
}
session()->flash('message', 'Tariff added successfully!');
return redirect('/admin/tariffs/');
}
public function addProduct()
{
$this->allTariffs[] = ['productId' => '', 'basicCharge' => '', 'additionalCharge' => ''];
}
public function removeProduct($index)
{
unset($this->allTariffs[$index]);
$this->allTariffs = array_values($this->allTariffs);
}
public function render()
{
$rowProducts = Products::all();
return view('livewire.admin.admin-edit-tariffs-component',['rowProducts'=>$rowProducts])->layout('layouts.admin.base');
}
}
Edit View
<div class="container-fluid">
<!-- Begin Page Content -->
<!-- Page Heading -->
<div class="row">
<div class="col-lg-8">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Edit Tariff</h6>
</div>
<div class="card-body">
<form wire:submit.prevent="editTariff">
#csrf
<div class="form-row">
<!-- Default input -->
<div class="form-group col-md-8">
<input type="text" class="form-control" placeholder="Enter Tariff Name"
wire:model="tariffName" required>
</div>
</div>
<hr>
<div class="card">
<div class="card-header">
<h6 class="text-primary">Products, Basic and Weight Charges</h6>
</div>
<div class="card-body">
<table class="table" id="products_table">
<thead>
<tr>
<th>Product</th>
<th>Basic Charge</th>
<th>Weight Charge</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach ($allTariffs as $index => $allTariff)
<tr wire:key="tariff-{{ $value->wireKey }}">
<td>
<select
wire:model="allTariffs.{{$index}}.productId"
class="custom-select custom-select-sm form-control form-control-sm">
<option value="">Select Product</option>
#foreach ($rowProducts as $product)
<option value="{{ $product->id }}">
{{ $product->product_name }}
</option>
#endforeach
</select>
</td>
<td>
<input type="text"
class="form-control custom-select-sm form-control form-control-sm"
placeholder="Basic Charge"
wire:model="allTariffs.{{$index}}.basicCharge" required>
</td>
<td>
<input type="text"
class="form-control custom-select-sm form-control form-control-sm"
placeholder="Weight Charge"
wire:model="allTariffs.{{$index}}.additionalCharge" required>
</td>
<td>
<a href="#" wire:click.prevent="removeProduct({{$index}})" class="btn btn-danger btn-circle btn-sm">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<a href="#" wire:click.prevent="addProduct" class="btn btn-success btn-circle btn-sm">
<i class="fas fa-plus"></i>
</a>
</div>
</div>
</div>
</div>
<hr>
<div class="form-row">
<div class="form-group col-md-3">
<button type="submit" class="form-control btn btn-small btn-primary">Edit
Tariff</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
Model File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Tariffs extends Model
{
use HasFactory;
protected $table = "tariffs";
public function products()
{
return $this->belongsToMany(Products::class, 'products_tariffs',
'products_id', 'tariffs_id')
->withPivot('basic_charge', 'additional_charge');
}
public function productstariffs()
{
return $this->hasMany(ProductsTariffs::class);
}
}
I have tried prepopulating the forms from DB records, not happening for child form, I have use all conventions for Laravel table, pivot table etc., still nothing. The array display with the dd('$allTariffs') but doesn't show on view file, please help am stuck with
ErrorException
foreach() argument must be of type array|object, null given .thankyou
In your foreach in your mount function, you are creating an array but you aren't assigning it to anywhere. Therefore, $allTariffs is empty.
Also, you're casting your variables by default as an array. You are setting $rowProducts as a Collection, however. But then in your render you are also getting the $rowProducts again. I'd suggest removing the = [] and removing the fetching of the $rowProducts each render.
public $allTariffs = [];
public $rowProducts;
public $tariffName;
public function mount ($tariffId)
{
$this->rowProducts = Products::all();
// This assumes the $tarrifId always exists. If this isn't the case, perhaps
// wrap the related code in an "if ($tariff instanceof Tariff)"
$tariff = Tariffs::find($tariffId);
$this->tariffName = $tariff->tariff_name;
foreach ($tariff->products as $product) {
$this->allTariffs[] = ['productId' => $product->id, 'basicCharge' => $product->pivot->basic_charge, 'additionalCharge' => $product->pivot->additional_charge];
}
}
public function render()
{
return view('livewire.admin.admin-edit-tariffs-component')->layout('layouts.admin.base');
}

How to add "not found" result on search data on laravel 5.8

Is there a way to add result "not found" if my data is not in the database ?
In my search button I want to add a result that: if the search doesn't conclude the data inside the defined form, the search must output data not found...
The search is working...
Here is my controller
pegawaicontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Pegawai;
class pegawaicontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$pegawais = Pegawai::orderBy('id', 'ASC')->paginate(5);
return view('pegawai.index', compact('pegawais'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('pegawai.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'kategori' => 'required',
'jabatan' => 'required',
'nama_pegawai' => 'required',
'alamat' => 'required',
'gambar_pegawai' => 'required'
]);
// upload file
$filename = time() . '.png';
$request->file('gambar_pegawai')->storeAs('public/images', $filename);
$pegawai = new Pegawai;
$pegawai->kategori = $request->input('kategori');
$pegawai->jabatan = $request->input('jabatan');
$pegawai->nama_pegawai = $request->input('nama_pegawai');
$pegawai->alamat = $request->input('alamat');
$pegawai->gambar_pegawai = $filename;
$pegawai->save();
// $pegawai = Pegawai::create($request->all());
return redirect()->route('pegawai.index')->with('message','Data berhasil dibuat!');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$pegawai = Pegawai::findOrFail($id);
return view('pegawai.show', compact('pegawai'));
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$pegawai = Pegawai::findOrFail($id);
return view('pegawai.edit', compact('pegawai'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'kategori' => 'required',
'jabatan' => 'required',
'nama_pegawai' => 'required',
'alamat' => 'required',
'gambar_pegawai' => 'required'
]);
// script delete tapii gagal
// if ($request->produk()->gambar_pegawai){
// Storage::delete('images/' . $request->produk()->gambar_pegawai);
// }
// update file
$filename = time() . '.png';
$request->file('gambar_pegawai')->storeAs('public/images', $filename);
$pegawai = Pegawai::find($id);
$pegawai->kategori = $request->input('kategori');
$pegawai->jabatan = $request->input('jabatan');
$pegawai->nama_pegawai = $request->input('nama_pegawai');
$pegawai->alamat = $request->input('alamat');
$pegawai->gambar_pegawai = $filename;
$pegawai->save();
// $pegawai = Pegawai::findOrFail($id)->update($request->all());
return redirect()->route('pegawai.index')->with('message', 'Data berhasil diubah!');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$pegawai = Pegawai::findOrFail($id)->delete();
return redirect()->route('pegawai.index')->with('message', 'Data berhasil dihapus!');
}
public function search(Request $request)
{
$q = $request->input('searchpegawai');
$pegawais = Pegawai::where('nama_pegawai', 'LIKE', '%'.$q.'%')
->orWhere('alamat', 'LIKE', '%'.$q.'%')
->paginate(5);
return view('pegawai.index')->with('pegawais', $pegawais)->with('query', $q);
}
}
Here Is The Index blade where the search function are...
index.blade.php
#extends('template.adminlte')
#section('content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark">Menu Pegawai</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item active">pegawai</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<a href="{{ route('pegawai.create') }}" class="btn btn-info btnsm float-left">
<i class="fas fa-plus-circle"> Tambah Pegawai</i>
</a><br><br>
<form class="form-inline ml-3 float-sm-right">
<form action="searchpegawai" method="GET" role="Search" class="form-inline ml-3">
<div class="input-group input-group-sm float-sm-right">
<input class="form-control form-control-navbar float-sm-right" type="search" placeholder="Search" aria-label="Search" name=searchpegawai>
<div class="input-group-append float-sm-right">
<button class="btn btn-navbar" type="submit">
<i class="fas fa-search"></i>
</button>
</div>
</div>
</form>
#if ($message = Session::get('message'))
<div class="alert alert-success martop-sm">
<p>{{ $message }}</p>
</div>
#endif
<table class="table martop-sm">
<thead>
<th>No</th>
<!-- <th>ID</th> -->
<th>Kategori</th>
<th>Jabatan Pegawai</th>
<th>Nama pegawai</th>
<th>Alamat</th>
<th>Foto Pegawai</th>
<th>Aksi</th>
</thead>
<tbody>
#foreach ($pegawais as $key => $pegawai)
<tr>
<td>{{ $pegawais->firstItem() + $key }}</td>
<!-- <td>{{ $pegawai->id }}</td> -->
<td>{{ $pegawai->kategori }}</td>
<td>{{ $pegawai->jabatan }}</td>
<td>{{ $pegawai->nama_pegawai }}</td>
<td>{{ $pegawai->alamat }}</td>
<td><img src="{{ asset('storage/images/' . $pegawai->gambar_pegawai) }}" alt="" width="100"></td>
<td>
<form action="{{ route('pegawai.destroy', $pegawai->id) }}" method="post">
{{csrf_field()}} {{ method_field('DELETE') }}
Ubah
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah anda yakin ingin menghapusnya?');">Hapus</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
{{ $pegawais -> links() }}
</div>
</section>
</div>
#endsection
use forelse in blade
#forelse($pegawais as $key => $pegawai)
<tr>
<td>{{ $pegawais->firstItem() + $key }}</td>
<!-- <td>{{ $pegawai->id }}</td> -->
<td>{{ $pegawai->kategori }}</td>
<td>{{ $pegawai->jabatan }}</td>
<td>{{ $pegawai->nama_pegawai }}</td>
<td>{{ $pegawai->alamat }}</td>
<td><img src="{{ asset('storage/images/' . $pegawai->gambar_pegawai) }}" alt="" width="100"></td>
<td>
<form action="{{ route('pegawai.destroy', $pegawai->id) }}" method="post">
{{csrf_field()}} {{ method_field('DELETE') }}
Ubah
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah anda yakin ingin menghapusnya?');">Hapus</button>
</form>
</td>
</tr>
#empty
<tr><td align="center" >Result not found.</td></tr>
#endforelse
ref link https://laravel.com/docs/8.x/blade#loops

Trying to get property 'nom_matiere' of non-object laravel shows me this error

i want to display the name of matiere in this page but he shows me this error Trying to get property 'nom_matiere' of non-object
i make a selection in the note page to select the matiere then give a note to here it work but when i display the name of matiere in my table it give me error
model note
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
protected $fillable = ['note'];
public function matieres()
{
return $this->belongsToMany(Matiere::class);
}
}
model matiere
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Matiere extends Model
{
protected $fillable = ['nom_matiere','coef'];
public function notes() {
return $this->belongsToMany(Note::class);
}
}
my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Note;
use App\Matiere;
class NoteController extends Controller
{
public function index()
{
$notes = Note::paginate(5);
$matieres = Matiere::all();
return view('admin.notes',compact('notes','matieres'));
}
public function store(Request $request)
{
Note::create($request->all());
session()->flash('success',' cette note a été enregistré avec succés');
return redirect()->back();
}
public function update(Request $request, $id)
{
$note = Note::findOrFail($request->note_id);
$note = Matiere::findOrFail($request->note_id);
$note->update($request->all());
session()->flash('success','cette note a été modifié avec succés');
return redirect()->back();
}
public function destroy(Request $request)
{
$note = Note::findOrFail($request->note_id);
$note->delete();
session()->flash('success','cette note a été supprimé avec succés');
return redirect()->back();
}
}
my view
<section id="no-more-tables">
<table class="table table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>id-note</th>
<th>La note</th>
<th>nom matiere</th>
<th>les actions</th>
</tr>
</thead>
<tbody>
#foreach($notes as $note)
<tr>
<td class="numeric" data-title="id-note" >{{$note->id}}</td>
<td class="numeric" data-title="Nom">{{$note->note}}</td>
<td class="numeric" data-title="Nom">{{$note->matiere->nom_matiere}}</td>
<td>
<button href="#editEmployeeModal" class="btn btn-theme" data-target="#editEmployeeModal "data-mynote="{{$note->note}}" "data-mymatiere="{{$note->nom_matiere}}" data-catid={{$note->id}} class="edit" data-toggle="modal" ><i class="material-icons" data-toggle="tooltip" title="Edit"></i> </button>
<button href="#deleteEmployeeModal" class="btn btn-theme" data-target="#deleteEmployeeModal" data-catid={{$note->id}} class="delete" data-toggle="modal" > <i class="material-icons" data-toggle="tooltip" title="Delete"></i> </button>
</td>
</tr>
</tbody>
#endforeach
</table>
<div class="text-center">
{{ $notes->links() }}
</div>
<div class="clearfix">
<div class="hint-text">Affichage de <b>5</b> sur <b>25</b> entrées</div>
<div id="addEmployeeModal" href="create" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('notes.store')}}" method="post">
{{csrf_field()}}
<div class="modal-header">
<h4 class="modal-title">Ajouter note</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>La note</label>
<input type="text" id="note" name="note" class="form-control" required>
</div>
</div>
<div class="form-group select">
<select name="matiere_id">
<option value="">--selectionner la mtiére svp --</option>
#foreach($matieres as $matiere)
<option value="{{ $matiere->id }}">{{ $matiere->nom_matiere }}</option>
#endforeach
</select>
</select>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Annuler">
<input type="submit" class="btn btn-success" value="Ajouter">
</div>
</form>
</div>
</div>
You're calling $note->matiere in your view but the relationship is called matieres, which would be a collection.
Instead of:
<td class="numeric" data-title="Nom">{{ $note->matiere->nom_matiere }}</td>
You'd need:
<td class="numeric" data-title="Nom">
#foreach ($note->matieres as $matiere)
{{ $matiere->nom_matiere }}
#endforeach
</td>
I do however suspect that you have set your relationship up wrong. You've got the Note -> Matiere relationship set to belongsToMany, which means that you need a pivot table to contain the FK relationships. This would only be used if your Note is the child of many Matiere.
It does sound like you have your relationships set up wrong, but without really understanding what you're trying to do it's hard to tell you what exactly needs to be done.
Separate issue, but in your class update() method you've also got:
$note = Note::findOrFail($request->note_id);
$note = Matiere::findOrFail($request->note_id);
$note->update($request->all());
This means that you actually update the Matiere model no the Note model.

Laravel foreign key reference

I'm trying to reference the description of a foreign key via a relation, like follows:
My model.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Evento extends Model
{
//
protected $fillable = [
'idEvento',
'strNombreEvento',
'strDireccion',
'strCiudad',
'strCorreo',
'strTelefono',
'strEncargadoEvento',
'strNotas',
'idEscuela',
];
protected $primaryKey = 'idEvento';
public function escuela()
{
return $this->belongsTo('App\Escuela','idEscuela');
}
public function diaevento()
{
return $this->hasMany('App\diaEvento');
}
}
The relation
namespace App;
use Illuminate\Database\Eloquent\Model;
class Escuela extends Model
{
//
protected $fillable = [
'idEscuela',
'strNombreEscuela',
'bolPrincipal',
'strLogo',
'sitDiasUsuarioInactivo',
'sitDiasToleranciaCobro',
];
protected $primaryKey = 'idEscuela';
public function grupos()
{
return $this->hasMany('app\grupo');
}
public function eventos()
{
return $this->hasMany('app\eventos');
}
}
My view
#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="#">Eventos</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/eventos/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>Evento</th>
<th>Escuela</th>
<th>Dirección</th>
<th>Ciudad</th>
<th>Correo</th>
<th>Teléfono</th>
<th>Contacto</th>
</tr>
</thead>
<tbody>
#foreach($eventos as $evento)
<tr>
<td> {{ $evento->strNombreEvento }} </td>
<td>{{ $evento->escuela->strNombreEscuela }}</td>
<td>{{ $evento->strDireccion }} </td>
<td>{{ $evento->strCiudad }} </td>
<td>{{ $evento->strCorreo }} </td>
<td>{{ $evento->strTelefono }} </td>
<td>{{ $evento->strEncargadoEvento }} </td>
</tr>
#endforeach
</tbody>
</table>
</main>
</div>
</div>
#endguest
#endsection
My controller
public function index()
{
//
$eventos = evento::all();
return view('eventos.index', ['eventos'=>$eventos]);
}
The result.
The thing is that the same mechanics is working with another model, which I post:
namespace App;
use Illuminate\Database\Eloquent\Model;
class grupo extends Model
{
//
protected $fillable = [
'idGrupo',
'idEscuela',
'strNombreGrupo',
];
protected $primaryKey = 'idGrupo';
public function escuela()
{
return $this->belongsTo('App\Escuela','idEscuela');
}
public function horarioGrupo()
{
return $this->hasMany('App\horario_Periodicos');
}
}
The relation is the same as with evento above.
My view
#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="#">Grupos</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/grupos/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>Nombre del grupo</th>
<th>Escuela</th>
</tr>
</thead>
<tbody>
#foreach($grupos as $grupo)
<tr>
<td> {{ $grupo->strNombreGrupo }} ></td>
<td>{{ $grupo->escuela->strNombreEscuela }}</td>
</tr>
#endforeach
</tbody>
</table>
</main>
</div>
</div>
#endguest
#endsection
The controller
public function index()
{
//
$grupos = grupo::all();
return view('grupos.index', ['grupos'=>$grupos]);
}
I would appreciate your help.
Thanks.
I post my migrations, they where added today
The original Evento
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEventosTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('eventos', function (Blueprint $table) {
$table->increments('idEvento');
$table->string('strNombreEvento', 200);
$table->string('strDireccion', 200);
$table->string('strCiudad', 200);
$table->string('strCorreo', 200);
$table->string('strTelefono', 20);
$table->string('strEncargadoEvento', 60);
$table->string('strNotas', 300);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('eventos');
}
}
These one was dump, thinking in a direct referent from the foreign table
From Evento to Escuela, no intermediate.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DeleteDiasEventoTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::dropIfExists('dias_eventos');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
Schema::create('dias_eventos', function (Blueprint $table) {
$table->increments('idDiasEventos');
$table->integer('idEvento')->unsigned();
$table->foreign('idEvento')->references('idEvento')->on('eventos');
$table->integer('idDiaEventos')->unsigned();
$table->foreign('idDiaEventos')->references('id')->on('dia_eventos');
$table->unique(['idEvento','idDiaEventos']);
$table->timestamps();
});
}
}
Then I modified Evento, including the foreign key by itself.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ModifyEventoTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::table('eventos', function($table) {
$table->integer('idEscuela')->unsigned();
$table->foreign('idEscuela')->references('idEscuela')->on('escuelas');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('eventos', function (Blueprint $table) {
$table->dropForeign('eventos_idescuela_foreign');
$table->dropColumn('idEscuela');
});
}
}
Is your query returning array or object? If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).

Laravel 5. Error: my model with trashed and one relation.

Problem exists because transhed added, conflicted with tablerelation.
Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
public function category() {
return $this->hasOne('App\Models\Category', 'id', 'category_id');
}
}
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Category;
use App\Models\Post;
use Session;
use Auth;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$posts = new Post();
// $allPosts = $posts::onlyTrashed()->get();
// $allPosts = $posts::withTrashed()->get();
$allPosts = $posts::all();
$postDeleted = $posts::onlyTrashed()->count();
return view('admin.post.index', ['posts' => $allPosts, 'postDeleted' => $postDeleted]);
}
...
View:
#foreach ($posts as $post)
<tr class="table-pages-list-item">
<td><input type="checkbox"></td>
<td>{{ $post->h1 }}</td>
<td>{{ $post->url }}</td>
<td>{{ $post->category->h1 }}</td>
<td>
#if ($post->published)
<span class="label label-success">Да</span>
#else
<span class="label label-danger">Нет</span>
#endif
</td>
<td>{{$post->updated_at}}</td>
<td>
<a href="#"type="button" class="btn btn-primary" data-toggle="tooltip" data-placement="left" title="Редактировать">
<i class="fa fa-edit"></i>
</a>
<a href="#" target="_blank" type="button" class="btn btn-success" data-toggle="tooltip" data-placement="top" title="Просмотреть">
<i class="fa fa-external-link"></i>
</a>
<form action="{{ action('PostController#destroy', ['id' => $post->id]) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-danger" data-toggle="tooltip" data-placement="right" title="В корзину">
<i class="fa fa-trash-o"></i>
</button>
</form>
</td>
</tr>
#endforeach
Error here <td>{{ $post->category->h1 }}</td>
Trying to get property of non-object (View: C:\OpenServer\domains\laravel\resources\views\admin\post\index.blade.php)
because you are naming your foreign key as category_id like the convention,i think you should define eloquent relationship like this
public function category() {
return $this->hasOne('App\Models\Category');
}
you should see hasOne method on Model.php file how to define one to one relationship.
Please check what fields your category table has!
Does it contains 'h1' field, you are accessing 'h1' key, but may be its not there at table

Resources