samedi 1 août 2020

How to send message to selected user with their name laravel

I'm trying to send a message to selected users with their names. So let's say I have two users Ethan and Calvin then the message should start like User1:(Hi, Ethan), User2:(Hi, Calvin). So far the message is like (Hi, Ethan,Calvin) for every user. How can I fix this?

Blade

<form  action="" method="POST">
@foreach($users as $user)
<textarea   name="message" ></textarea>
<input type="checkbox" name="phone[]" @if(!old() || old('phone') == 'true')  @endif   value=" 
">
<input type="checkbox" name="name[]" value=""/>
<button type="submit" class="btn btn-primary">
send Message
</button>
@endforeach
</form>

Controller

    public function message(Request $request)
    {
     $message = $request->input('message');
     $postData = $request->all();

     foreach ($postData['phone'] as $index => $value) {
        $postData['phone'][$index] = Str::replaceFirst('1','965',$value);
    }
    foreach($postData['name'] as $index => $names){
        $postData['name'][$index] = $names;
    }
     $phone_number = implode(',', $postData['phone']);
     $name = implode(',', $postData['name']);


     $send_message = new MyHelper();
     $message = "Hi, $name $message";
     $send_message->sendMessage($phone_number,$message);
     return 'success'; 
    }


via Chebli Mohamed

Laravel 5.4: controller method is called twice on a redirect to it

I'm encountering a problem where a redirect from one route to another is calling the targeted controller method twice. This question addresses a similar issue, but the OP passing a 301 status code was deemed to be the issue in the accepted answer, and I'm not specifying any status code. I'm also using the session state for parameters. The relevant code looks something like this:

public function origin(Request $request) {
  // Assume I have set variables $user and $cvId
  return redirect()
    ->action('SampleController@confirmUser')
    ->with([
      'cvId' => $cvId,
      'userId' => $user->id,
     ]);
}

public function confirmUser(Request $request) {
  $cvId = session()->get('cvId');
  $userId = session()->get('userId');

  if (is_null($cvId) || is_null($userId)) {
    // This is reached on the second time this is called, as 
    // the session variables aren't set the second time
    return redirect('/home');
  }

  // We only see the view for fractions of a second before we are redirected home
  return view('sample.confirmUser', compact('user', 'cvId'));
}

Any ideas what could be causing this? I don't have any next middleware or any of the other possible causes that are suggested in related questions where controllers are executed twice.

Thanks for any help!



via Chebli Mohamed

PHP - Add new object to every array of objects

Consider this array of objects in PHP:

 array:2 [
      0 => array:4 [
        "Row_Id" => 256
        "Start_Date" => "2020-05-16"
        "account_code" => ""
        "caller_number" => "452"
        ]
    
      1 => array:4 [
        "Row_Id" => 257
        "Start_Date" => "2020-05-16"
        "account_code" => ""
        "caller_number" => "42"
        ]

      2 => array:4 [
        "Row_Id" => 258
        "Start_Date" => "2020-05-16"
        "account_code" => ""
        "caller_number" => "428"
        ]
    ]

I want to add "callee_number:100" in every array so my output should look like these:

     array:2 [
          0 => array:5 [
            "Row_Id" => 256
            "Start_Date" => "2020-05-16"
            "account_code" => ""
            "caller_number" => "452"
            "callee_number" => "100"
            ]
        
          1 => array:5 [
            "Row_Id" => 257
            "Start_Date" => "2020-05-16"
            "account_code" => ""
            "caller_number" => "42"
            "callee_number" => "100"

            ]

          2 => array:5 [
            "Row_Id" => 258
            "Start_Date" => "2020-05-16"
            "account_code" => ""
            "caller_number" => "428"
            "callee_number" => "100"
            ]
        ]

I have taken the above input array in $get variable. Now I am calling array_push to append callee_number to every array:

  array_push($get,[
   'callee_number':'100'
    ]);

Also tried using array_merge but callee_number is not getting appended. How can I achieve that ?



via Chebli Mohamed

Can any help out Passing Multiple drop down values in php

I have drop down list which populate rows such as primary, high school, PUC. If user selected as primary i need to show all the students which are in class-1 to class-7 students from the table "student_info".

Similarly if it's high school,I need to display students who are in class-7 to class-10.

Now from Table "student_info". How I have to filter students which comes under primary through "Class" column only..



via Chebli Mohamed

how to display video per category in laravel eloquent

model video

protected $table = 'lokit_video';


protected $fillable = 
[
    'title',
    'cover_img',
    'trailer',
    'url',
    'order_',
    'active',
    'description',
    'lokit_category_id',
    'duration'
];
public function lokit_category(): BelongsTo
{
    return $this->belongsTo(Category::class);
}

model category

protected $table = 'lokit_category';
    protected $fillable = ['name'];

in controller

public function index(){
       $dataCategory = Category::all();
        $dataVideo = Video::all();
        $video = Video::where('lokit_category_id', $dataCategory)->get();
        dd($video);
        return View('bnpt.content.home',compact('dataCategory','dataVideo'));
    }

when I try the code above what happens with the code is null, how to fix it?



via Chebli Mohamed

How to retrieve the data from table that are indirectly connected in laravel?

How can I get the room data from booking through roomAvailabilities table??

I tried using :

    $booking = Booking::all();
    $booking-> RoomAvailability;   //it works
    $booking -> Room;              //doesnot work

In booking model:

public function room()
{
    return $this->hasManyThrough('App\Room', 'App\RoomAvailability');
}

Sql Server query that might be similar to what I want:

SELECT * FROM dbo.Booking as b

INNER JOIN dbo.RoomAvailability as ra on b.bookingId = ra.BookingId

INNER JOIN dbo.Room as r on r.roomId = ra.roomId

Below is the sample of my table structure.

Booking:

id 
check_in_date
check_out_date
number_of_rooms

RoomAvailabilities:

id 
booking_id
room_id

Room:

id
room_number

Booking Model

public function roomAvailability()
{
    return $this->hasMany(RoomAvailability::class);
}

RoomAvailability Model

public function booking()
{
    return $this->belongsTo(Booking::class);
}


via Chebli Mohamed

how to update two tables with db: transaction laravel

in my code controller

    public function update(Request $request,$id) {
    $data = $request->all();
    $validator = Validator::make($data, [
        'title'                  => 'required',
        'order_'  => 'required',
        'active'               => 'required',
        'url'               => 'required',
        'category'           => 'required'
    ]);
    $errors=$validator->errors();
    if ($validator->fails()) {
        return Redirect::to('admin/video/video/'.$id.'/edit')
            ->withErrors($errors)
            ->withInput();
    }
    $data['lokit_category_id'] = $data['category'];
    unset($data['category']);
    DB::beginTransaction();
    try {
        if($request->hasFile('cover_img')) {
            $uniquename='cvr_'.md5($id);
            $filename=$uniquename.'.'.$request->file('cover_img')->getClientOriginalExtension();
            $path=public_path('storage/media');
            $request->cover_img->move($path, $filename);
            $data['cover_img']=$filename;
            
            $this->_resizeimg($path,$filename,$uniquename);  
        }
        $isi = Video::find($id);
        if($isi->url != $data['url']) {
            $name = str_replace("_", '',$data['url']);
            $manager = new MediaManager('/uploads');
            $manager->newFolder($name);
            $data['duration'] = $this->getDuration($data['url']);
        }
        
        $isi->update($data);
        if (isset($request->series) && $request->series) {
            $video_series = new VideoSeries();
            $video_series->lokit_video_id =$id;
            $video_series->lokit_series_id = (int)$request->series;
            $video_series->series = (int)$request->episode;
            $isi->video_series()->update($video_series);
        }
        DB::commit();
        return Redirect::to('admin/video/video');
    } catch (\Exception $ex) {
        DB::rollback();
        throw $ex;
    }
}

When I tried the code above I even experienced an error like this Argument 1 passed to Illuminate \ Database \ Eloquent \ Builder :: update () must be of the type array, object given, how to fix the error



via Chebli Mohamed