mercredi 1 mars 2017

How to get average of column values in laravel

Lets say I have this column

star
----
1    
3    
3    
1    
2    
5    
3

It has seven rows and there are integer values! I want to have it added and divided by the rows there are.

How do I do it in laravel. I can do it in plain php but I want to learn it in laravel.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lpbrQw
via IFTTT

Laravel DB query gives a set of results different from direct MySQL query

I am trying to execute the following query in MySQL

SELECT * FROM categories AS subcategory JOIN categories AS parent ON parent.category_id = subcategory.parent_category_id WHERE subcategory.category_display_type = 'sidebar' AND subcategory.category_visibility = 1

like so:

class SidebarnavComposer
{
    public function compose(View $view)
    {
        $params = DB::table('categories as subs')->join('categories as cats', function($join){
                $join->on('subs.parent_category_id', '=', 'cats.category_id')
                ->where('subs.category_display_type', '=', 'sidebar')
                ->where('subs.category_visibility', '=', 1);
    })->get();


        $view->with('subcategories', $params);

    }

the problem is that I get different set of results.

When I use the first query directly in MySQL I get the following:

category_id
category_id1
category_title
category_title1
category_slug
category_slug1
.......

when I execute the second query in my laravel application I get the following set of results:

category_id
category_title
category_slug
.......

this basically means that I cannot access all necessary columns.

I am not sure what I am doing wrong as I am new to laravel and at the moment do not really know much, so please help me if you can



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mcLZlB
via IFTTT

Laravel: Select DB error Undefined index or Trying to get property of non-object

First Laravel project. I have a DBController what selects a row from the database after the url (for example the URL is /product/123 then it search for the product which barcode is 123). And I want to make a table about the productdetails on the next page.

Controller:

class ProductDetailsController extends Controller
{
    public function product($id){
    $product = DB::select('select * FROM inventory WHERE barcode = ?', [$id]);;
    return view('productdetails',['product'=>$product]);
    }
}

print_r($product) output:

Array ( [0] => stdClass Object ( [ID] => 1 [barcode] => 01233 [brand] => Ismeretlen [supplier] => 1 [type] => Építésianyag [name] => Homok [img] => homok.jpg [wholeprice] => 3000 [price] => 5500 [VAT] => 3 [whereis] => U [whereis2] => 1 [count] => 8 [dimension] => m3 [threshold] => 1 [lastsell] => [lastbuy] => [lastchange] => 2017-02-23 20:56:46 [register] => ) )

If I try I got:

ErrorException in 20d205ed160f36c734b8252e44ac81bfa0977988.php line 6: Trying to get property of non-object (View: /var/www/html/project/laravel/leltar/resources/views/productdetails.blade.php)

And if I try I got:

ErrorException in 20d205ed160f36c734b8252e44ac81bfa0977988.php line 6: Undefined index: brand (View: /var/www/html/project/laravel/leltar/resources/views/productdetails.blade.php)

What's going wrong?

I learn Laravel from this PDF



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lp993N
via IFTTT

Accessing multiple images stored in database as a filename but stored in public folder of laravel

I needed to upload multiple image as an array and for that i set up my controller like this:

   namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Detail;

    class DetailController extends Controller
    {
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    //
    $data=Detail::all();
    return view('details.index',compact('data'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
    $data=Detail::all();
    return view('details.create',compact('data'));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */

  public function store(request $request) {
       $input=$request->all();
    if($files=$request->file('images')){
        foreach($files as $file){
        $name=$file->getClientOriginalName();
        $file->move('image',$name);

        $input['images']=$name;
    }
    }

  Detail::create($input);

    return redirect('/details');


    }

And my route is:

     Route::resource('/details','DetailController');

Doing so I have become to able store the selected images for upload form form in public folder ie, Public/image. But only one image is stored in database like this.Database image . Is it okay if only one among the selected images stored in DB and rest are stored in public. If okay then how can i access/display all the images in the show page. Thanks.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mrTGF8
via IFTTT

Laravel relationships setup

I'm working on a Laravel installation where I have (among other things)

  • Teachers (table:users (cols: id, name, type), model:User, users.type = 2)
  • Students (table:users (id, name, type), model:User, users.type = 1)
  • Schools (table:schools (id, name), model:School)
  • Collections of students (table:student_collection (id, name), model:StudentCollection)
  • & table students_collections (id, user_id, collection_id)

I have managed to setup some relationships, but can't get my head around to get

  • Which teachers teach a student
  • Which collections does a student belong to

Currently I'm trying to get the collections with

//User.php
public function collections() {
    return $this->belongsToMany('\App\StudentCollection', 'student_collections_users', 'collection_id', 'user_id');
}

but it returns an empty result.

I'm using Laravel 5.3.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lVikLe
via IFTTT

How to handle a very large JSon content from URL

I use an API that show products's information. But I want to get all of them. It's about 75000 products. When I open URL that shows the json content, my browser just loading..... So, Could you help me to get all of them as fast as possible, I'm using Laravel 5.2 Thank you!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lxEG4k
via IFTTT

Checkbox not working laravel backpack CRUD

$this->crud->addField([
        'name' => 'status',
        'label' => 'Featured item',
        'type' => 'checkbox',
    ]);

The generated code is following:

<div class="form-group col-md-12">
  <div class="checkbox">
    <label>
      <input type="hidden" value="0" name="status">
      <input type="checkbox" name="status" value="1"> Featured item
    </label>
  </div>
</div>

Using the above code for checkbox, Everything works fine, value is saving correctly in database and fetched correctly. But checkbox is not checked.

The checkbox is not checked even if the value is 1.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2m6JWPR
via IFTTT