lundi 2 mars 2020

Laravel replicate multiple rows didn't work

I want to replicate multiple relational rows to the same table with the diff job id. But it doesn't work.

Here is my code

$parentJobUnits = Unit::where('job_id',$jobId)->get();  //may be single or multiple rows for units.

$JobCopy = $job->replicate()->save();    //main job copied for new units.

$partsCopy = array_map(function(Unit $unit) use($JobCopy)
{

     $newUnit = $unit->replicate();
     $newUnit->job_id = $JobCopy->id;
     return $newUnit->save();

}, $parentJobUnits);

The above code is not working for multiple row replication.



via Chebli Mohamed

how can i make a request in laravel

i just my training job on an application that was developed by other company, and there is not comment on the apps and this is my first time i start to use the Laravel Framework, so i need to make a filter to look for the customers that they already subscribe on the apps, and i don't know how can i make the request on the apps and where i can show the result? thanks a lot guys for advance.

this is the function i found on the apps

public function ajaxsearch(Request $request){




 $client = $request->get('client',null) ;
 $date_depart = $request->get('date_depart',null) ;
 $date_arrivee = $request->get('date_arrivee',null) ;
 $chauffeur = $request->get('chauffeur',null) ;
 $status = $request->get('status',null) ;
 $query = Course::where('id','!=',0);

i add this variable to use the same function to add a search bar on the apps //$client_filter = $request->get('client',null);

// chercher for the customers from the Courses table
if($client){
   $courses = $query->Where('client_id', 'like', '%' . $client . '%');

}
// to search for customers they already have a courses with all information date time and adress of start and arrive on the filter i wanna to add
/*if($client_filter){
  $projets = $query->Where('client_id', '=' , $client . '%');
}
*/

if($date_depart){
   $courses = $query->Where('date_depart', 'like', '%' . $date_depart . '%');

}
if($date_arrivee){
   $courses = $query->Where('date_arrivee', 'like', '%' . $date_arrivee . '%');

}
if($chauffeur){
   $courses = $query->Where('user_id', 'like', '%' . $chauffeur . '%');

}
if($status){
   $courses = $query->Where('status', 'like', '%' . $status . '%');

}

// for the view of the customer
$courses = $query->where('id' -> input::get('client_id'),'nom_usage' -> input::get('nom_usage'))->get();
return view($this->views.'.search',compact('Courses'));


$courses = $query->orderBy('id','desc')->get();
    return view($this->views.'.search',compact('courses'));

}


via Chebli Mohamed

Laravel error : Integrity constraint violation

ROUTE

             Route::post('/review','RentalController@review');
CONTROLLER

             public function review(Request $request)
           {
             $review = new Reviews();
            $rpId = rand();
            $review->rvid=$rpId;
            $review->usid_fk = Auth::user()->uid;
            // $propId= $request->input('propId');
            $review->prId_fk = $request->input('propId');;
            $review->comment = $request->input('comment');
            $review->rating = $request->input('rating');
            $review->date = Carbon::now();
            $review->save();


                 }
MIGRATE
              public function up()
        {
            Schema::create('review', function (Blueprint $table) {

                 $table->integer('rvId')->primary();
                   $table->integer('usId_fk');
                   $table->foreign('usId_fk')->references('uid')->on('users');
                   $table->integer('prId_fk');
                   $table->foreign('prId_fk')->references('pId')->on('properties');
                   $table->date('date');
                   $table->integer('rating');
                   $table->string('comment');
            });

        }
VIEW

             <form action="" method="POST">
             

          <div class="modal-body">
          <input type="hidden" name="propid" value=""/>
          <input id="rating" name="rating" class="rating rating-loading" data-show-clear="false" data-min="0" 
          data-max="5" data-step="1" value="0">


          <div class="input-group">
          <div class="input-group-prepend">
          <span class="input-group-text">Comment</span>
          </div>
          <textarea name="comment" class="form-control" aria-label="Comment"></textarea>

            </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Submit</button>
          </div>
          </form>

and the error is integrity contraintthe prId_fk cannot be null been trying to fix this a coule of days alreadyYour reply would be highly appreciatedthank you the prId_fk cannot be null integrity constraint violation

i've been trying to rerwite my code over and over again still it didnt work



via Chebli Mohamed

dimanche 1 mars 2020

Laravel pagination with custom array

i am new to laravel.i am working with database:paginnation

$postData = DB::table('posts')->paginate(1);
return view('posts.index', ['users' => $users]);

Above code working fine with pagination and display correct pagination But now my blog app doesn't require this type

I pass data to view something like this

 $postData = DB::select('SELECT * FROM posts')->paginate(2);

 $pageData = array(
    "title"=>"Post Index",            
     "postData" => $postData
 );
 return view('posts.index')->with('pageData',$pageData);

which will cause

Call to a member function paginate() on array

My laravel version is 5.7



via Chebli Mohamed

What is meaning of "::" syntax in Laravel Blade (Ex: @include('googletagmanager::head') )?

When I used Laravel Package: spatie/laravel-googletagmanager , in my view included view include some view of package

 @include('googletagmanager::head')

What is meaning of :: syntax ?
Where can I find reference document about it ?
When ScriptViewCreator is called ( vendor/spatie/laravel-googletagmanager/src/ScriptViewCreator.php ) ?

Thank you!



via Chebli Mohamed

Cannot declare class App\User, because the name is already in use Laravel

I want to add user address in address table and want to update the address_id in user table for that i'm using user model and address model, data is being saved in address table but when i use User model in Address Repository

use App\Models\User;

i get

Cannot declare class App\User, because the name is already in use

Here is my code :

<?php 
namespace App\Repositories;

use App\Models\Addresses;
use App\Models\User;
use App\Contracts\AddressContract;
use Illuminate\Database\QueryException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Doctrine\Instantiator\Exception\InvalidArgumentException;

class AddressRepository extends BaseRepository implements AddressContract
{
    /**
     * AttributeRepository constructor.
     * @param Attribute $model
     */
    public function __construct(Addresses $model)
    {
        parent::__construct($model);
        $this->model = $model;
    }


    public function addAddress(array $params)
    {
        try {

            $Addresses = new Addresses($params);
            $Addresses->save();
            $addressId = $Addresses->id;
            $userID=auth()->user()->id;
            if($params['is_primary_address']==1)
            {   
                User::where('id',$userID)->update(['address_id'=>$addressId]);
            }
            return $Addresses;
            }
            catch (QueryException $exception) {
            throw new InvalidArgumentException($exception->getMessage());
        }
    }
}

ProductController.php

<?php

namespace App\Http\Controllers\Site;
use App\Contracts\AttributeContract;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Contracts\ProductContract;
use App\Contracts\AddressContract;
use Cart;
use Validator;

class ProductController extends Controller
{ 
    protected $productRepository;
    protected $attributeRepository;
    protected $addressRepository;
    public function __construct(ProductContract $productRepository, AttributeContract $attributeRepository, AddressContract $addressRepository)
{  
    $this->productRepository = $productRepository;
    $this->attributeRepository = $attributeRepository;
    $this->addressRepository = $addressRepository;
}

public function addUserAddress(Request $request)
{
    $customer_name=$request->customer_name;
    $customer_address=$request->customer_address; 
    $country=$request->country; 
    $city=$request->city;
    $zip_code=$request->zip_code;  
    $state=$request->state; 
    $address_type=$request->address_type; 
    $is_primary_address=$request->primary_address; 
    $userID=auth()->user()->id;
    $data=array('name'=>$customer_name,'address'=>$customer_address,'country'=>$country,'state'=>$state,'city'=>$city,'address_type'=>$address_type,'user_id'=>$userID,'is_primary_address'=>$is_primary_address);
    $userAddress  = $this->addressRepository->addAddress($data);
    return redirect()->back()->with('message', 'Address Added');
}
}


via Chebli Mohamed

Laravel 5.5 about Model

I want to have a Chapter model that contains a district column contained multiple District model. But I have no idea how to link it. I just have done with model and migration.

class Chapter extends Model
{
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
      'name',
  ];

  public function districts()
  {
    return $this->hasMany(District::class);
  }
}
class District extends Model
{
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
      'name',
  ];

  public function chapter()
  {
      return $this->belongsTo(Chapter::class);
  }
  
  public function blocks()
  {
    return $this->hasMany(Block::class);
  }
}

What should I do after this? Do I need to mention the district in the chapter migration file?

Here is my migration file.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateChaptersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('chapters', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->indexed();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('chapters');
    }
}
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateDistrictsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('districts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->indexed();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('districts');
    }
}

Do I need to link them in the $fillable side?



via Chebli Mohamed