I'm trying to create a Tag/Content structure. A content object is assigned to a Tag object and Tag objects can be assigned to many Contents. I'm getting an error:
Trying to get property 'name' of non-object (View: D:\laragon\www\project1\resources\views\contents\show.blade.php)
These are my Models:
Content:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Content extends Model {
public function tag() {
return $this->belongsTo('App\Tag');
}
}
Tag:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
public function contents() {
return $this->hasMany('App\Content');
}
}
ContentController:
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id) {
$content = Content::find($id);
return View('contents.show')
->with('content', $content);
}
show.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
<h1> {{ $content->tag->name }} - {{ $content->title }} </h1>
<div class="row">
<div class="col-lg-12">
Title: {{ $content->title }}
</div>
<div class="col-lg-12">
Body: {{ $content->body }}
</div>
<div class="col-lg-12">
Tag: {{ $content->tag }}
</div>
<hr />
<div class="col-lg-12">
{!! link_to('contents', 'Back', ['class' => 'btn btn-danger']) !!}
</div>
</div>
</div>
#endsection
The error I'm getting is from h1 tag: {{ $content->tag->name }}
Any Ideas? Thanks in advance :)
You have to check "$content->tag" is valid before call "$content->tag->name".
The problem is that in the table contents should have tag_id, but you can solve it in this way in the Content model
class Content extends Model {
public function tag() {
return $this->belongsTo('App\Tag');
}
}
class Tag extends Model {
public function contents() {
return $this->hasMany('App\Content', 'tag');
}
}
This was the solution:
Change column name tag to tag_id and change return $this->belongsTo('App\Tag'); to return $this->belongsTo('App\Tag','tag_id');
The final code was:
Content Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Content extends Model {
public function tag() {
return $this->belongsTo('App\Tag','tag_id');
}
}
Tag Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
public function contents() {
return $this->hasMany('App\Content');
}
}
Thank you all for the help.
Refer the table name inside model
protected $table ="<table_name>"
Related
How to slove Undefined variable error exception for variable declared in another controller.
I have OrderController and CustomerController.
In CustomerController i pass $customerTypes[] to view.
Then that view i try to import in antoher view of OrderController and than i get undefended variable. So OrderController does not know about $customerTypes[].
How is possible to slove that dependency?
If i declare $customerTypes[] in OrderController that not be clean code!No point for that var in order.
CustomerController
namespace App\Http\Controllers;
class CustomerController extends BaseController
{
/**
* Index
*/
public function index()
{
$orders = Order::orderBy('created_at', 'desc')->paginate(20)->withQueryString();
$customerTypes = CustomerType::orderBy('name')->get();
return view('customer.index', [
'customerTypes'=> $customerTypes
]);
}
}
/resources/views/customer/create_modal.blade.php
<!-- Modal -->
<div class="modal fade">
#if($customerTypes)
#foreach($customerTypes as $type)
<div class="form-check">
<input type="checkbox" value="{{ $type->id }}">
<label>{{ $type->name }}</label>
</div>
#endforeach
#endif
</div>
OrderController
namespace App\Http\Controllers;
class OrderController extends BaseController
{
/**
* Index
*/
public function index()
{
//....
return view('customer.index', [
'orders'=> $orders
]);
}
}
/resources/views/order/index.blade.php
here i include modal and get error.
#include('customer.create_modal')
I created a Model, Controller, Factory and dataTable files, but laravel keeps showing me the error $dataTable is undefined
How come? Is it a routing issue or I'm not linking the controller to the blade correctly?
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\DataTables\DataEntry\ReportsDataTable;
class ReportsController extends Controller
{
public function index(ReportsDataTable $dataTable)
{
return $dataTable->render("pages.dataEntry.reports.index");
}
}
The Route:
// Data Entry pages
Route::prefix('dataEntry')->name('dataEntry.')->group(function () {
Route::resource('reports', ReportsController::class)->only(['index']);
});
index.blade:
<x-base-layout>
<!--begin::Card-->
<div class="card">
<!--begin::Card body-->
<div class="card-body pt-6">
<!--begin::Table-->
{{ $dataTable->table() }}
<!--end::Table-->
{{-- Inject Scripts --}}
{{ $dataTable->scripts() }}
</div>
<!--end::Card body-->
</div>
<!--end::Card-->
</x-base-layout>
public function index()
{
return ReportsDataTable::all()->render("pages.dataEntry.reports.index");
}
For our Task attributes we have the following: task_id as primary key, user_id, stage_id and project_id as foreign keys, completed as boolean and a description. Our goal is to display the tasks under a project and by checking the checkbox right next to them, it should mark them as complete. The problem is in our database the 'complete' status doesnt change. We are using PhpMyAdmin. We have a separate controller called ProjectTasksController for handling the logic and a form in our show.blade.php view for sending the request. Any help would be greatly appreciated.
#extends('layouts.app')
#section('content')
<div class="display-3">{{$project->name}}</div>
<a class="nav-link" href="/projects/{{$project->project_id}}/edit"><i class="material-icons">edit</i></a>
#if ($project->image)
<div class="row">
<div class="col-12">
<img src="{{ asset('storage/' . $project->image) }}" alt="...." class="img-thumbnail">
</div>
</div>
#elseif(!$project->image)
no image
#endif
#if ($project->tasks->count())
<div>
#foreach ($project->tasks as $task)
<div>
<form method="POST" action="/tasks/{{$task->task_id}}">
{{method_field('PATCH')}} {{-- #method('PATCH') --}}
#csrf
<label class="checkbox {{$task->completed ? 'is_complete' : ''}} " for="completed">
<input type="checkbox" name="completed" onChange="this.form.submit()" {{$task->completed ? 'checked' : ''}} >
{{$task->description}}
</label>
</form>
</div>
#endforeach
</div>
#endif
#endsection
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Task;
class ProjectTasksController extends Controller{
public function update(Task $task)
{
$task->update([
'completed' => request()->has('completed')
]);
return back();
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
protected $guarded = [];
protected $primarykey = ['task_id'];
protected $fillable = ['user_id','stage_id','project_id','completed','description'];
public function stage(){
return $this->belongsTo(Stage::class);
}
public function user(){
return $this->belongsTo(User::class);
}
public function project(){
return $this->belongsTo(Project::class);
}
}
{
_method: "PATCH",
_token: "ljiwu8bEtAkRqSUOXllmaRbSujavHNYNRJR5TMcy",
completed: "on"
}
Route::patch('/tasks/{task_id}', 'ProjectTasksController#update');
Your controller method was not correct, hint of Task $task is just a instance of Task not the collection or a single Model.And you have not specify your Request $request to get this work request()->has('completed') in method arguments.You need to edit your method in following way:
public function update(Request $request,$task_id)
{
Task::find($task_id)->update([
'completed' => $request->has('completed')
]);
return back();
}
Note: $request->has('completed') will return Boolean; if you want exact value,then you need to retrieve as $request->get('completed')
If you want to use route model binding the name of your parameter in the update function should match the route parameter:
Route::patch('/tasks/{task}', 'ProjectTasksController#update');
Replace protected $primaryKey = ['task_id]'; with protected $primaryKey ='task_id' in the Task model. It should be a string, not an array.
I am using Laravel 5.8 with Laravel-nestedset for my category model and I have a product model.
I am successfully creating nested categories and displaying them properly, but when I get to a category that has a product, I want to product(s) that belong to the current category to display--the code below with show products, but not in the category that they belong to. This is pretty standard stuff I imagine, but I can't seem to figure it out.
Category.php
<?php
namespace App;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;
use Kalnoy\Nestedset\NodeTrait;
class Category extends Model
{
use \Spatie\Tags\HasTags;
use HasSlug;
use NodeTrait;
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('name')
->saveSlugsTo('slug');
}
public function getRouteKeyName()
{
return 'slug';
}
}
Product.php
<?php
namespace App;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use \Spatie\Tags\HasTags;
use HasSlug;
protected $dates = [
'published_on',
];
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('title')
->saveSlugsTo('slug');
}
public function getRouteKeyName()
{
return 'slug';
}
public function category()
{
return $this->belongsTo(Category::class);
}
}
CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Product;
class CategoryController extends Controller
{
public function index()
{
$categories = Category::get()->toTree();;
return view('categories.index', compact('categories'));
}
public function show(Category $category)
{
$products = Product::with('category')->whereIn('category_id', $category)->get();
return view('categories.show', compact('category', 'products'));
}
}
show.blade.php (this is the category show template and where I want the products to show)
...
#if (count($category['children']) > 0)
#foreach($category['children']->chunk(3) as $chunk)
<div class="row">
#foreach($chunk as $category)
<div class="col-md-4" style="margin-bottom: 2rem">
<div class="feature-box center media-box fbox-bg">
<div class="fbox-media">
<a href="/{{$category->slug}}">
<img class="image_fade"
src="https://*****.s3-us-west-1.amazonaws.com/{{ $category->photo }}"
alt="Featured Box Image"
style="opacity: 1;"></a>
</div>
<div class="fbox-desc">
<h3>{{$category->name}}</h3>
<span>Learn More</span>
</div>
</div>
</div>
#endforeach
</div>
#endforeach
#endif
#if(count($products) > 0)
#foreach($products as $product)
{{$product->title}}
#endforeach
#endif
...
Define the products() relationship in the Category model
public function products()
{
return $this->hasMany(Product::class);
}
And you access the relationship from an instantiated category like so:
#if (count($category['children']) > 0)
#foreach($category['children']->chunk(3) as $chunk)
<div class="row">
#foreach($chunk as $category)
// ...
#foreach($category->products as $product)
#endforeach
// ...
#endforeach
</div>
#endforeach
#endif
I am aiming on retrieving the user image (from profile table column 'profile_img') and display on each post's footer.
I can retrieve the name of the author using $post->author->name and the profile picture using $post->author->profile->profile_image but it only works when i have a single record (post). when there is more than one record i get an error Trying to get property 'profile' of non-object (View: xampp/........../home.blade.php)
Can somebody show me where do i go wrong??
Models:
User
public function post()
{
return $this->hasMany('App\Post');
}
public function profile()
{
return $this->hasOne('App\Profile');
}
Profile
public function user()
{
return $this->belongsTo('App\User');
}
Post
public function author()
{
return $this->belongsTo('App\User', 'id');
}
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class MainController extends Controller
{
public function index() {
$posts = Post::paginate(9);
return view('pages.home')->with('posts', $posts);
}
home view
#if(count($posts) > 0)
#foreach($posts as$posts)
<div>
<div class="post-title"><h3>{{$post->title}}</h3></div>
<div class="post-description">
{!!mb_substr($post>body,10,rand(35,40)) !!} ....
</div>
<div class="featured-details">
<div class="p-clearfix">
<img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
<div class="author-title lite">{{ $post->author->name }}</div>
<div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
</div>
</div>
</div>
<hr>
#endforeach
#else
no post yet
#endif
There appears to be some issues in your home view. Try it again with this code and see.
Home View
#if(count($posts) > 0)
#foreach($posts as $post)
<div>
<div class="post-title"><h3>{{$post->title}}</h3></div>
<div class="post-description">
{!!mb_substr($post>body,10,rand(35,40)) !!} ....
</div>
<div class="featured-details">
<div class="p-clearfix">
<img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
<div class="author-title lite">{{ $post->author->name }}</div>
<div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
</div>
</div>
</div>
<hr>
#endforeach
#else
no post yet
#endif
You can even go further and avoid the n+1 problem for authors by running your eloquent model query like this om your controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class MainController extends Controller
{
public function index() {
$posts = Post::with("author")->paginate(9);
return view('pages.home')->with('posts', $posts);
}
I hope this helps.
In your controller index method try with :
public function index() {
$posts = Post::with("author.profile")->paginate(9);
return view('pages.home')->with('posts', $posts);
}
And update post model relationship:
public function author()
{
return $this->belongsTo('App\User', 'user_id'); // update to users table foreign key
}