No matter what I do this error keeps popping up, I´ve searched for a solution but Im too stupid to comprehend what is going on, please help me
preguntas.blade.php
@extends('app')
@section('title', 'Pregunta:'.$nombrePregunta->titulo)
@section('description', $nombrePregunta->categorias->nombre)
@section('content')
<div class="leading-loose max-w-4xl mb-4">
{{$nombrePregunta->body}}
</div>
There´s more but its non important
Routes/web.php
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllerspaginaControlador;
Route::get('/', [paginaControlador::Class, 'gestionHome'])->name('home');
Route::get('/categorias/{nombreCategoria:slug}', [paginaControlador::class, 'gestionCategoria'])->name('categoria');
Route::get('/etiquetas/{nombreEtiqueta:slug}', [paginaControlador::class, 'gestionEtiqueta'])->name('etiqueta');
Route::get('/preguntas/{nombrePregunta:slug}', [paginaControlador::class, 'gestionPregunta'])->name('pregunta');
Controllers/paginaControlador.php
<?php
namespace AppHttpControllers;
use AppModelsPreguntas;
use AppModelsCategorias;
use AppModelsEtiquetas;
use IlluminateHttpRequest;
class paginaControlador extends Controller
{
public function gestionHome(){
$preguntas = Preguntas::orderBy('id','DESC')->paginate();
return view("home", compact('preguntas'));
}
public function gestionCategoria($nombreCategoria){
$preguntas = $nombreCategoria->preguntas()->orderBy('id','DESC')->paginate();
return view('categoria', compact('nombreCategoria','preguntas'));
}
public function gestionEtiqueta($nombreEtiqueta){
$preguntas = $nombreEtiqueta->preguntas()->orderBy('id','DESC')->paginate();
return view('etiqueta', compact('nombreEtiqueta','preguntas'));
}
public function gestionPregunta($nombrePregunta){
return view('preguntas', compact('nombrePregunta'));
}
}
Models/preguntas.php
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Preguntas extends Model
{
use HasFactory;
// 1:N
public function comentarios(){
return $this -> hasMany(Comentarios::class);
}
// N:N
public function etiquetas(){
return $this -> belongsToMany(Etiquetas::class);
}
//N1
public function categorias(){
return $this -> belongsTo(Categorias::class);
}
//N1
public function user(){
return $this -> belongsTo(User::class);
}
}
I dont know if this is useful but ill put it here just in case
PreguntasFactory
namespace DatabaseFactories;
use IlluminateDatabaseEloquentFactoriesFactory;
use AppModelsUser;
use AppModelsCategorias;
use IlluminateSupportStr;
/**
* @extends IlluminateDatabaseEloquentFactoriesFactory<AppModelsPreguntas>
*/
class PreguntasFactory extends Factory
{
public function definition(): array
{
return [
'categorias_id' => Categorias::factory(),
'user_id' => User::factory(),
'titulo' => $title = fake()->unique()->sentence(),
'slug' => Str::slug($title),
'body' => fake()->text(1300)
];
}
}
I´ve tried using this live.ERROR: Trying to get property 'title' of non-object (View: C:xampphtdocscoreresourcesviewsusersignal-all.blade.php), but i can´t comprehend what i need to do
2
Answers
Found the solution in Controllers/paginaControlador.php
Without "Pregunta" in there it didnt make an object
your problem is that you want to use the "magic" of Route Model Binding, but you do not define a model in the input parameter
$nombrePregunta
of the controller methodgestionPregunta
(and same problem is in other controller methods).Edit based on your comment:
If you are using other routeKeyName (in your case slug), you have to also set it in your model (Preguntas).
If you dont want to change routeKeyName, than you have to manually find in controller method the coresponding model for slug: