Multiple select query in same Laravel view - php

I got a problem when I want to show my data in view from same table, but I just need one of field in that table..
through controller, there were function index() and setID()
This is my controller
public function index(){
$wo = DB::select('select max(nowo)+1 as nowo, CURRENT_DATE as tgl from workorder');
return view('entri_wo')->with(['workorder'=>$wo]);
}
public function setID()
{
$wo = DB::select('select idmontir from workorder');
return view('entri_wo',['wo'=>$wo]);
}
This is my view
Also, I want to show this
And this is my route
Route::get('/entri_wo', 'WoController#index')->name('entri_wo');

in your controller:
public function index(){
$wo = DB::select('select idmontir from workorder');
$wo2 = DB::select('select max(nowo)+1 as nowo, CURRENT_DATE as tgl from workorder');
return view('entri_wo',['wo'=>$wo,'workorder'=>$wo2]);
}
and in your view:
#foreach($wo as $item)
<option value="{{ $item->idmontir }}" >{{ $item->idmontir }}</option>
#endforeach
and :
#foreach($workorder as $item)
<tr>
<td>{{ $item->nowo }}</td>
<td>{{ $item->tgl }}</td>
</tr>
#endforeach

Related

How to solve this in Laravel 6? Trying to get property 'name' of non-object

My Model
class Purchase extends Model
{
public function supplier()
{
return $this->belongsTo(Supplier::class,'supplier_id','id');
}
public function unit()
{
return $this->belongsTo(Unit::class,'unit_id','id');
}
public function category()
{
return $this->belongsTo(Category::class,'category_id','id');
}
public function product()
{
return $this->belongsTo(Product::class,'product_id','id');
}
}
My Controller
class PurchaseController extends Controller
{
public function view()
{
$data['allData'] = Purchase::orderBy('id','desc')->get();
return view('backend.purchase.view-purchases', $data);
}
My View Page
#foreach($allData as $key => $purchase)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $purchase->purchase_no }}</td>
<td>{{ $purchase->product->name }}</td>
<td>{{ $purchase->unit->name }}</td>
<td>{{ $purchase->date }}</td>
and 2 more td for edit and delete
#endforeach
But there's still errors in all {{ $purchase-> example}}. I can't find the errors. If I check the dd() method right after the tr tag it works but it doesn't get the {{ $purchase-> example }}. I have the following error message:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\POS\resources\views\backend\purchase\view-purchases.blade.php).
enter image description here
Try loading the relations in the controller e.g.
$data['allData'] = Purchase::with('supplier', 'unit', 'category', 'product')->orderBy('id','desc')->get();
I would also advise you to change your controller view method body to the following.
$purchases = Purchase::with('supplier', 'unit', 'category', 'product')->orderBy('id','desc')->get();
return view('backend.purchase.view-purchases')->with(['purchases' => $purchases]);
So then in your view try to access it like the following.
#foreach($purchases as $purchase)
...
<td>{{ $purchase->purchase_no }}</td>
...
#endforeach

Call to undefined method Illuminate\Database\Query\Builder::with()

I'm trying to display the name of the category, but I get this error:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::with()
AdminController.php
public function gamelist()
{
$categories = Category::all();
$home = DB::table('products')->with(['categories']);
return view('admin.gamelist', ['categories' => $categories, 'home' => $home, 'mode' => 'admin']);
}
Product.php
class Product extends Model
{
public function categories()
{
return $this->belongsTo('App\Category', 'category_id');
}
}
gamelist.blade.php
#foreach ($homes as $home)
<tr>
<td>{{ $home->id }}</td>
<td>{{ $home->name }}</td>
<td>{{ $home->categories->name}}</td>
<td>{{ $home->price }} €</td>
Can someone help me? Thank you
with is used to eager-load Eloquent relationships. By calling DB::table, you decided to use the query builder instead, and this one can't use Eloquent relationships.
You should replace
$home = DB::table('products')->with(['categories']);
by
$home = Product::with('categories')->get();

How to calculate sum for belongsTo model laravel

I need to get sum of amount for belongsTo model on laravel
Here is my code
Controller:
$users = AffiliationUser::whereAffiliationId(Auth::user()->id)
->with(['user' => function ($q){
$q->withCount(['billings'])->with(['affiliation_transactions']);
}])
->paginate(3);
//dd($users);
return view('affiliation.users', [
'users' => $users,
'current_balance' => 0,
]);
AffiliationUser model:
public function user() {
return $this->belongsTo('App\Models\Users');
}
User model:
public function billings() {
return $this->hasMany('App\Models\UserBillingSchedules', 'user_id');
}
public function affiliation_transactions() {
return $this->hasMany('App\Models\AffiliationTransaction', 'user_id');
}
View:
#foreach ($users as $user)
<tr class="even pointer">
<td class="a-center ">{{ $user->user->id }}</td>
<td class=" ">{{ $user->user->email }}</td>
<td class=" ">{{ $user->user->card_holder }}</td>
<td class=" ">{{ $user->user->billings_count }}</td>
<td class=" ">{{ $user->user->affiliation_transactions->sum('amount') }}</td>
</tr>
#endforeach
It is working good for me, but i don't like idea to get it on view.
{{ $user->user->affiliation_transactions->sum('amount') }}
Which one solution i can use also?
You can calculate the sum with withCount():
$users = AffiliationUser::whereAffiliationId(Auth::user()->id)
->with(['user' => function ($q) {
$q->withCount([
'billings',
'affiliation_transactions as amount' => function ($q) {
$q->select(DB::raw('coalesce(sum(amount), 0)'));
}
]);
}])
->paginate(3);
{{ $user->user->amount }}
Laravel allows you to create custom attributes (using the get[attributename]Attribute method in your Models easily which can be used to reduce the amount of code you need to write. In this case, you can do this:
User Model:
public function getTransactionCountAttribute() {
return $this->affiliation_transactions->sum('amount');
}
Now this can be acccessed with the following method in your code:
$user->user->transaction_count
Also, here's a little more information about Mutators, as these are named. Hope they can be useful to you :)
In User Model create a method like:
public function sum_amount() {
return this.affiliation_transactions->sum('amount');
}
then in your view...
{{ $user->user->sum_amount }}

Database data is not an array or object

I want to print '$post' as an array from database.
<tbody>
#if (is_array($posts) || is_object($posts)){
#foreach ($posts as $post)
<tr>
<th>{{$post->id }}</th>
<td>{{$post->title }}</td>
<td>{{$post->body }}</td>
<td>{{$post->created_at }}</td>
<td>ViewEdit</td>
</tr>
#endforeach
#else
{{'no!'}}
#endif
</tbody>
it just prints no! this is the main function:
public function index()
{
$posts = Post::all();
return view('posts.index')->withPosts('$posts');
}
You should below way to pass the data from view file.
public function index()
{
$posts = Post::all();
return view('posts.index',compact('posts'));
}
You can use get method , get will return all of the results in the model's table. After that you can pass the result to your view.
public function index(){
$posts = Post::get();
return view('posts.index',compact('posts'));
}

Laravel 4 TDD Test failing

I All, I am trying really had to get in proper TDD but it is not smooth sailing.
This test keeps on failing and I can't figure out how to make it pass.
#>phpunit
PHPUnit 3.7.20 by Sebastian Bergmann.
Configuration read from /Users/ghall/Sites/laravel.hyundianet.hyundai.co.nz/phpunit.xml
<h2>Charts</h2>
<table class="table table-striped">
{"error":{"type":"ErrorException","message":"Invalid argument supplied for foreach()","file":"\/Users\/ghall\/Sites\/laravel.dev\/app\/storage\/views\/9fe7adcadbe887c8bce1c18e06defac2","line":8}}
I appears it's because the view it trying to render out something that does not exits from them mock
Route
App::bind('App\Models\Interfaces\ChartInterface', 'App\Models\Repositories\EloquentChartRepository');
Route::resource('chart', 'ChartController');
Controller
use App\Models\Interfaces\ChartInterface;
use App\Models\Repositories\EloquentChartRepository;
class ChartController extends BaseController
{
protected $layout = 'layouts.application';
protected $chart;
function __construct(ChartInterface $chart)
{
$this->chart = $chart;
// var_dump($chart);
}
public function index()
{
$charts = $this->chart->all();
$this->layout->content = View::make('chart.index')
->with('charts', $charts);
}
}
Repository
namespace App\Models\Repositories;
use App\Models\Interfaces\ChartInterface;
use Chart;
class EloquentChartRepository implements ChartInterface
{
public function all()
{
return Chart::all();
}
public function find($id)
{
return Chart::find($id);
}
}
Interface
namespace App\Models\Interfaces;
interface ChartInterface
{
public function all();
public function find($id);
}
Test
class ChartControllerTest extends TestCase
{
public function __construct()
{
$this->mock = Mockery::mock('App\Models\Interfaces\ChartInterface');
}
public function tearDown()
{
Mockery::close();
}
public function testIndex()
{
$this->mock
->shouldReceive('all')
->once()
->andReturn('charts');
$this->app->instance('App\Models\Interfaces\ChartInterface', $this->mock);
$this->call('GET', 'chart');
$this->assertViewHas('charts');
}
}
View
#if (!empty($charts))
#section('content')
<h2>Charts</h2>
<table class="table table-striped">
#foreach ($charts as $chart)
<tr>
<td>{{ $chart->id }}</td>
<td>{{ $chart->report_name }}</td>
<td>{{ $chart->description }}</td>
<td>{{ $chart->graphtype }}</td>
<td>{{ $chart->date_range }}</td>
<td>{{ $chart->user }}</td>
</tr>
#endforeach
</table>
#stop
#else
#section('content')
<h2>Nothing to display</h2>
#stop
#endif
NEW
I found the problem and it's in the Controller / view. When the test call the controller the controller returns the view with markup.
If I change the controller to then it works but this is not a good solution.
public function index()
{
$charts = $this->chart->all();
if(is_object($charts)){
$this->layout->content = View::make('chart.index')
->with('charts', $charts);
return;
}
return View::make('chart.test')->with('charts', $charts);
}
Inside of ChartControllerTest, in the test testIndex() the mock is returning a String and the view needs a array. Try with something like this:
$this->mock
->shouldReceive('all')
->once()
->andReturn(array());
You can check if variable supplied to view is array by using is_array function e.g in your view do
#if(is_array($charts))
#foreach ($charts as $chart)
<tr>
<td>{{ $chart->id }}</td>
<td>{{ $chart->report_name }}</td>
<td>{{ $chart->description }}</td>
<td>{{ $chart->graphtype }}</td>
<td>{{ $chart->date_range }}</td>
<td>{{ $chart->user }}</td>
</tr>
#endforeach
#endif
Regards

Categories