I am trying to do a search, I have verified that the data is received but not to make it show the filter.
Sending the data
<div class="panel-body">
{!! Form::open (['route' => 'report.index', 'method'=>'GET', 'class' => 'navbar-form navbar-left pull-right']) !!}
<div class="form-group">
<input type="text" name= "name" class="form-control" placeholder="Buscar por Servicio">
</div>
<button type="submit" class="btn btn-default">Buscar</button>
At the controller:
class ControllerReport extends Controller
{
public function index(Request $request)
{
$servicio = mivista::name($request->get('name'));
return view('report.buscar_pagos', compact('servicio'));
}
}
and in the model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Builder;
class mivista extends Model
{
protected $table = 'mivista';
public function scopeName($query,$name)
{
if ( ! is_null($name)) {
return $query->where('name', 'like', '%'.$name.'%');
}
}
}
I clarify that leaving $service = mivista::all(); In the controller shows me all the data, but I can not make the search work with the name parameter.
Waiting for your help, thank you in advance
You're using query scope to modify the query so the returned value is Builder object. You've to call get on it to fetch the data.
Try this
$servicio = mivista::name($request->get('name'))->get();
Related
Employees.php file
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* #method static find($id)
*/
class Employees extends Model
{
use HasFactory;
protected $table = 'employees';
public $timestamps = false;
}
EmployeesController.php
<?php
namespace App\Http\Controllers;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function employees_salaries()
{
return view('director.employees_salaries');
}
public function employees()
{
$employees = Employees::all();
return view('director.employees', ['employees'=>$employees]);
}
public function destroy($id)
{
$employees = Employees::find($id);
$employees->delete();
return redirect('/director.employees')->with('status', 'Your Data is Deleted');
}
}
employees.blade.php file
<from action="/delete/{{$employee->id}}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-delete btn-form me-3">
Delete
</button>
</from>
route.php file
Route::match(['get', 'post'], '/employees', array(EmployeesController::class, 'employees'))->name('employees');
Route::delete('/delete/{id}', array(EmployeesController::class, 'destroy'))->name('delete');
I cleared the cache but have no idea what the problem is. it looks like I wrote the function correctly
p.s version Laravel 9
mySQL 8
phpMyAdmin
Welcome to SO, i think youre not using the variable you assigned the value into,from controller in your blade view. maybe try to make sure you have the right variable or maybe try to use var dump.
try to put this in your controller b4 parsing value to view, to check whether you get the data you wanted or not
dd('$employees');
make sure you use the variable you assigned in your controller to your view
<?php
namespace App\Http\Controllers;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function employees_salaries()
{
return view('director.employees_salaries');
}
public function employees()
{
$employees = Employees::all();
return view('director.employees', ['employees'=>$employees]); //<< here you name the variable 'employees'
}
public function destroy($id)
{
$employees = Employees::find($id);
$employees->delete();
return redirect('/director.employees')->with('status', 'Your Data is Deleted');
}
}
change this variable in view
<from action="/delete/{{$employee->id}}" method="POST"> //<< this one you use '$employee' instead of '$employees'
#csrf
#method('DELETE')
<button type="submit" class="btn btn-delete btn-form me-3">
Delete
</button>
</from>
there might also be other problem, but for now thats what i can point out.
since im also learning.
I am trying to integrate recaptcha to a query form, where the user enters only one piece of data and it returns the result that is extracted from the database.
I'm using
https://github.com/anhskohbo/no-captcha
But a validation is needed from the controller
Something like this appears in your documentation
$validate = Validator::make(Input::all(), [
'g-recaptcha-response' => 'required|captcha'
]);
Adjusted it for Laravel 8
But since I have my form in a GET method, I cannot use the validations, so I use your wisdom to be able to achieve the integration with recaptcha
This is my ListadoRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ListadoRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'texto' => 'required',
'g-recaptcha-response' => 'required|captcha',
];
}
}
This is my controller:
namespace App\Http\Controllers;
use App\Models\Listado;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Requests\ListadoRequest;
class ListadoController extends Controller
{
public function index(ListadoRequest $request){
$validated = $request->validated();
//other code
}
This is my web.php
Route::get('/busqueda', [ListadoController::class, 'index'])->name('busqueda');
When I try to enter the form it does not enter and stays on the main page
If I remove the validation line and change ListadoRequest to Request I access my form but obviously the captcha is a lie because it is not required
Update
My view
<form action="{{route('busqueda')}}" method="get">
<input type = "text" class="form-control" name="texto" value="{{$texto}}" >
<br>
{!! NoCaptcha::display() !!}
<input type=submit class="btn btn-primary" value= "Buscar">
</form>
Help pls :(
Can Anybody help me with Laravel Relationship ?
i have data base structure
poliklinik Table
===========
id
nama_poliklinik
****************
jadwal_poliklinik Table
=======================
id
poliklinik_id
hari
***********************
the relationship is "One" polikinik "have many" jadwal_poliklinik
my model poliklinik is
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\jadwal_poliklinik;
class poliklinik extends Model
{
protected $table = 'poliklinik';
public function jadwal_poliklinik()
{
return $this->hasMany(jadwal_poliklinik::class);
}
}
my jadwal_poliklinik model
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\poliklinik;
class jadwal_poliklinik extends Model
{
protected $table = 'jadwal_poliklinik';
public function poliklinik()
{
return $this->belongsTo(poliklinik::class);
}
}
my controller is
public function jadwal()
{
$poliklinik = poliklinik::all();
return view('jadwal', compact('poliklinik'));
}
and the view
<div class="row">
#foreach($poliklinik as $p)
<div class="col-md-4 animate-box">
<div class="blog-entry">
<div class="desc">
<h3>{{$p->nama_poliklinik}}</h3>
<hr/>
#foreach($poliklinik->jadwal_poliklinik as $jp)
{{$jp->hari}}<br/>
#endforeach
</div>
</div>
</div>
#endforeach
</div>
but result is
Exception Property [jadwal_poliklinik] does not exist on this
collection instance.
can someone help me with this??
It must be $p->jadwal_poliklinik
#foreach($p->jadwal_poliklinik as $jp)
{{$jp->hari}}<br/>
#endforeach
But also, Its better if you can use CamelCase for class names. That is the standard. You can simply use artisan command to create classes. See the bellow link for more details.
https://laravel.com/docs/7.x/eloquent#defining-models
I have two tables in my database, an Articles table and a Users table. An Article is posted by a user and a foreign key exists called 'created_by' referring to the 'user_id' field in the Users table.
My Laravel models are as follows:
User model
class User extends Model
{
use Notifiable;
protected $primaryKey = 'user_id';
public function articles()
{
return $this->hasMany('App\Article', 'created_by', 'user_id');
}
}
Article Model
class Article extends Model
{
protected $primaryKey = 'article_id';
public function user()
{
return $this->belongsTo('App\User', 'created_by' 'user_id');
}
}
I have a view dashboard which displays all the articles and the name of the person who posted it and their image.
Dashboard Controller
<?php
namespace App\Http\Controllers;
use App\User;
use App\Article;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index (Request $request)
{
if($request->session()->get('username')) {
$articles = Article::all();
return view('dashboard', compact('articles'));
}
else
return redirect('login');
}
}
And finally the section of my view I'm getting the 'Trying to get property of non-object' error for.
#foreach($articles as $article)
<div class="row">
<div class="col-lg-1"></div>
<div class="col-lg-9">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">{{ $article->title }} </h3>
</div>
<div class="panel-body">
<p>
{{ $article->content }}
<br><br> - {{ $article->user->first_name }}
</p>
</div>
</div>
</div>
<div class="col-lg-1"><center><br><br>
<img src="{{ asset('images/people/$article->user->image') }}" class="img-circle" alt="Person"></center>
</div>
</div><br><br>
#endforeach
It seems to be the $article->user->first_name that I am having trouble with. If I type $article->user alone, the page will load but nothing appears in that area.
Any ideas, I have been searching for solutions for the last two hours.
I think you need to specify that you want users to load with with the articles, like $articles = Article::with('user')->all();
Drop a dd($articles) right before the return statement and you'll see what you're actually dealing with.
$articles = Article::with('user')->get();
Edit
Try to check also if $article->user is null in the view because if there is no user assigned for an article then you will get "trying to get property of non object" error
so give an extra check when you access user's data.
{{ $article->user !== null ? $article->user->first_name : 'User Not Found' }}
Do this for user photo also
Please edit your controller, pls mark as answered thx.
<?php
namespace App\Http\Controllers;
use App\User;
use App\Article;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index (Request $request, Article $articles)
{
if($request->session()->get('username')) {
return view('dashboard')->with('articles', $articles->all() );
}
else
return redirect('login');
}
}
I am getting error on this code so I start it from blade, then controller then modle kindly give me solution why this problem happened.
blade:
#extends ('lay')
#section('content')
<h1>Write a New Article</h1>
<hr>
{!!Form::open(['url'=>'articles'])!!}
<div class="form-group">
{!!Form::label('title','Title:')!!}
{!!Form::text('title','',['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::label('body','Body:')!!}
{!!Form::textarea('body','',['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::label('published_at','Published on:')!!}
{!!Form::input('date','published_at',date('Y-m-d'),['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::submit('Add Article',['class'=>'btn btn-primary form-control','name'=>'submit'])!!}
</div>
{!!Form::close()!!}
#stop
controller: this is my controller of the program all this is made in laravel
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Articles;
use Request;
use Carbon\Carbon;
class ArticlesController extends Controller {
public function index()
{
/*$art=[
'title'=>'ashwani',
'body'=>'rathi',
'published_at'=>'Carbon\Carbon::now()'
];
Articles::create($art);*/
$articles=Articles::latest()->get();
return view('articles.index', compact('articles'));
}
public function show($id)
{
$article=Articles::findorFail($id);
return view('articles.show',compact('article'));
}
public function create(){
return view('articles.create');
}
public function store()
{
//$input=Request::all();
//$input['published_at']=Carbon::now();
//Articles::create($input);
Articles::create(Request::all());
return redirect('articles');
}
}
model: this is model where i using date and its not working
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Articles extends Model {
protected $fillable= [ 'title', 'body', 'published_at' ];
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFormFormat('Y-m-d|', $date);
}
}
It looks like this is stemming from a small typo.
You have put createFormFormat, where you have spelt Form instead of From.
You just need to correct this in your function:
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
You can use
public function getPublishedAtAttribute($date){
return Carbon::parse($date)->format('Y-m-d');
}
public function setPublishedAtAttribute($date){
$this->attributes['published_at'] = Carbon::parse($date);
}
in place of
public function setPublishedAtAttribute($date){
//$this->attributes['published_at'] = Carbon::createFormFormat('Y-m-d', $date);
return $this->attributes['published_at']->format('Y-m-d');
}